@@ -591,6 +591,104 @@ where
591591 }
592592}
593593
594+ /// A wrapper for NOR flash storage to collect usage statistics
595+ #[ derive( Clone , Copy , Debug ) ]
596+ pub struct NorFlashStats < S > {
597+ storage : S ,
598+ /// Number of read operations
599+ pub reads : usize ,
600+ /// Amount read chunks
601+ pub read : usize ,
602+ /// Number of write operations
603+ pub writes : usize ,
604+ /// Amount written chunks
605+ pub written : usize ,
606+ /// Number of erase operations
607+ pub erases : usize ,
608+ /// Amount of erased sectors
609+ pub erased : usize ,
610+ }
611+
612+ impl < S > From < S > for NorFlashStats < S > {
613+ fn from ( storage : S ) -> Self {
614+ Self {
615+ storage,
616+ reads : 0 ,
617+ read : 0 ,
618+ writes : 0 ,
619+ written : 0 ,
620+ erases : 0 ,
621+ erased : 0 ,
622+ }
623+ }
624+ }
625+
626+ impl < S > NorFlashStats < S > {
627+ /// Unwrap to get wrapped storage instance
628+ pub fn into_inner ( self ) -> S {
629+ self . storage
630+ }
631+ }
632+
633+ impl < S > core:: ops:: Deref for NorFlashStats < S > {
634+ type Target = S ;
635+
636+ fn deref ( & self ) -> & Self :: Target {
637+ & self . storage
638+ }
639+ }
640+
641+ impl < S > core:: ops:: DerefMut for NorFlashStats < S > {
642+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
643+ & mut self . storage
644+ }
645+ }
646+
647+ impl < S : ErrorType > ErrorType for NorFlashStats < S > {
648+ type Error = S :: Error ;
649+ }
650+
651+ impl < S : ReadNorFlash > ReadNorFlash for NorFlashStats < S > {
652+ const READ_SIZE : usize = S :: READ_SIZE ;
653+
654+ fn read ( & mut self , offset : u32 , bytes : & mut [ u8 ] ) -> Result < ( ) , S :: Error > {
655+ let res = self . storage . read ( offset, bytes) ;
656+ if res. is_ok ( ) {
657+ self . reads += 1 ;
658+ self . read += bytes. len ( ) / S :: READ_SIZE ;
659+ }
660+ res
661+ }
662+
663+ fn capacity ( & self ) -> usize {
664+ self . storage . capacity ( )
665+ }
666+ }
667+
668+ impl < S : NorFlash > NorFlash for NorFlashStats < S > {
669+ const WRITE_SIZE : usize = S :: WRITE_SIZE ;
670+ const ERASE_SIZE : usize = S :: ERASE_SIZE ;
671+ const ERASE_BYTE : u8 = S :: ERASE_BYTE ;
672+
673+ fn write ( & mut self , offset : u32 , bytes : & [ u8 ] ) -> Result < ( ) , S :: Error > {
674+ let res = self . storage . write ( offset, bytes) ;
675+ if res. is_ok ( ) {
676+ self . writes += 1 ;
677+ self . written += bytes. len ( ) / S :: WRITE_SIZE ;
678+ }
679+ res
680+ }
681+
682+ fn erase ( & mut self , from : u32 , to : u32 ) -> Result < ( ) , S :: Error > {
683+ let res = self . storage . erase ( from, to) ;
684+ if res. is_ok ( ) {
685+ self . erases += 1 ;
686+ self . erased += ( to - from) as usize / S :: ERASE_SIZE ;
687+ }
688+ res
689+ }
690+ }
691+
594692/// Simple RAM-backed flash storage implementation for tests
595693#[ derive( Clone , Copy , Debug ) ]
596694pub struct MockFlash <
0 commit comments