@@ -3,7 +3,7 @@ use core::convert::TryFrom;
33use  crate :: dir_entry:: DirEntryEditor ; 
44use  crate :: error:: Error ; 
55use  crate :: fs:: { FileSystem ,  ReadWriteSeek } ; 
6- use  crate :: io:: { IoBase ,  Read ,  Seek ,  SeekFrom ,  Write } ; 
6+ use  crate :: io:: { IoBase ,  Read ,  ReadFile ,   Seek ,  SeekFrom ,  Write ,   WriteFile } ; 
77use  crate :: time:: { Date ,  DateTime ,  TimeProvider } ; 
88
99const  MAX_FILE_SIZE :  u32  = u32:: MAX ; 
@@ -255,7 +255,7 @@ impl<IO: ReadWriteSeek, TP, OCC> IoBase for File<'_, IO, TP, OCC> {
255255    type  Error  = Error < IO :: Error > ; 
256256} 
257257
258- impl < IO :  ReadWriteSeek ,  TP :  TimeProvider ,  OCC >  Read  for  File < ' _ ,  IO ,  TP ,  OCC >  { 
258+ impl < IO :  ReadWriteSeek ,  TP :  TimeProvider ,  OCC >  ReadFile  for  File < ' _ ,  IO ,  TP ,  OCC >  { 
259259    fn  read ( & mut  self ,  buf :  & mut  [ u8 ] )  -> Result < usize ,  Self :: Error >  { 
260260        trace ! ( "File::read" ) ; 
261261        let  cluster_size = self . fs . cluster_size ( ) ; 
@@ -287,15 +287,10 @@ impl<IO: ReadWriteSeek, TP: TimeProvider, OCC> Read for File<'_, IO, TP, OCC> {
287287        } 
288288        trace ! ( "read {} bytes in cluster {}" ,  read_size,  current_cluster) ; 
289289        let  offset_in_fs = self . fs . offset_from_cluster ( current_cluster)  + u64:: from ( offset_in_cluster) ; 
290-         let  read_bytes = { 
291-             let  mut  disk = self . fs . disk . borrow_mut ( ) ; 
292-             disk. seek ( SeekFrom :: Start ( offset_in_fs) ) ?; 
293-             disk. read ( & mut  buf[ ..read_size] ) ?
294-         } ; 
295-         if  read_bytes == 0  { 
296-             return  Ok ( 0 ) ; 
297-         } 
298-         self . offset  += read_bytes as  u32 ; 
290+         let  mut  disk = self . fs . disk . borrow_mut ( ) ; 
291+         disk. seek ( SeekFrom :: Start ( offset_in_fs) ) ?; 
292+         disk. read_exact ( & mut  buf[ ..read_size] ) ?; 
293+         self . offset  += read_size as  u32 ; 
299294        self . current_cluster  = Some ( current_cluster) ; 
300295
301296        if  let  Some ( ref  mut  e)  = self . entry  { 
@@ -304,7 +299,14 @@ impl<IO: ReadWriteSeek, TP: TimeProvider, OCC> Read for File<'_, IO, TP, OCC> {
304299                e. set_accessed ( now) ; 
305300            } 
306301        } 
307-         Ok ( read_bytes) 
302+         Ok ( read_size) 
303+     } 
304+ } 
305+ 
306+ impl < IO :  ReadWriteSeek ,  TP :  TimeProvider ,  OCC >  Read  for  File < ' _ ,  IO ,  TP ,  OCC >  { 
307+     #[ inline]  
308+     fn  read_exact ( & mut  self ,  buf :  & mut  [ u8 ] )  -> Result < ( ) ,  Self :: Error >  { 
309+         <Self  as  ReadFile >:: read_exact ( self ,  buf) 
308310    } 
309311} 
310312
@@ -314,11 +316,11 @@ where
314316    std:: io:: Error :  From < Error < IO :: Error > > , 
315317{ 
316318    fn  read ( & mut  self ,  buf :  & mut  [ u8 ] )  -> std:: io:: Result < usize >  { 
317-         Ok ( Read :: read ( self ,  buf) ?) 
319+         Ok ( ReadFile :: read ( self ,  buf) ?) 
318320    } 
319321} 
320322
321- impl < IO :  ReadWriteSeek ,  TP :  TimeProvider ,  OCC >  Write  for  File < ' _ ,  IO ,  TP ,  OCC >  { 
323+ impl < IO :  ReadWriteSeek ,  TP :  TimeProvider ,  OCC >  WriteFile  for  File < ' _ ,  IO ,  TP ,  OCC >  { 
322324    fn  write ( & mut  self ,  buf :  & [ u8 ] )  -> Result < usize ,  Self :: Error >  { 
323325        trace ! ( "File::write" ) ; 
324326        let  cluster_size = self . fs . cluster_size ( ) ; 
@@ -366,33 +368,41 @@ impl<IO: ReadWriteSeek, TP: TimeProvider, OCC> Write for File<'_, IO, TP, OCC> {
366368        } ; 
367369        trace ! ( "write {} bytes in cluster {}" ,  write_size,  current_cluster) ; 
368370        let  offset_in_fs = self . fs . offset_from_cluster ( current_cluster)  + u64:: from ( offset_in_cluster) ; 
369-         let  written_bytes = { 
370-             let  mut  disk = self . fs . disk . borrow_mut ( ) ; 
371-             disk. seek ( SeekFrom :: Start ( offset_in_fs) ) ?; 
372-             disk. write ( & buf[ ..write_size] ) ?
373-         } ; 
374-         if  written_bytes == 0  { 
375-             return  Ok ( 0 ) ; 
376-         } 
371+         let  mut  disk = self . fs . disk . borrow_mut ( ) ; 
372+         disk. seek ( SeekFrom :: Start ( offset_in_fs) ) ?; 
373+         disk. write_all ( & buf[ ..write_size] ) ?; 
377374        // some bytes were writter - update position and optionally size 
378-         self . offset  += written_bytes  as  u32 ; 
375+         self . offset  += write_size  as  u32 ; 
379376        self . current_cluster  = Some ( current_cluster) ; 
380377        self . update_dir_entry_after_write ( ) ; 
381-         Ok ( written_bytes ) 
378+         Ok ( write_size ) 
382379    } 
383380
384381    fn  flush ( & mut  self )  -> Result < ( ) ,  Self :: Error >  { 
385382        Self :: flush ( self ) 
386383    } 
387384} 
388385
386+ impl < IO :  ReadWriteSeek ,  TP :  TimeProvider ,  OCC >  Write  for  File < ' _ ,  IO ,  TP ,  OCC >  { 
387+     #[ inline]  
388+     fn  write_all ( & mut  self ,  buf :  & [ u8 ] )  -> Result < ( ) ,  Self :: Error >  { 
389+         <Self  as  WriteFile >:: write_all ( self ,  buf) 
390+     } 
391+ 
392+     #[ inline]  
393+     fn  flush ( & mut  self )  -> Result < ( ) ,  Self :: Error >  { 
394+         <Self  as  WriteFile >:: flush ( self ) 
395+     } 
396+ } 
397+ 
389398#[ cfg( feature = "std" ) ]  
390399impl < IO :  ReadWriteSeek ,  TP :  TimeProvider ,  OCC >  std:: io:: Write  for  File < ' _ ,  IO ,  TP ,  OCC > 
391400where 
392401    std:: io:: Error :  From < Error < IO :: Error > > , 
393402{ 
394403    fn  write ( & mut  self ,  buf :  & [ u8 ] )  -> std:: io:: Result < usize >  { 
395-         Ok ( Write :: write ( self ,  buf) ?) 
404+         Write :: write_all ( self ,  buf) ?; 
405+         Ok ( buf. len ( ) ) 
396406    } 
397407
398408    fn  write_all ( & mut  self ,  buf :  & [ u8 ] )  -> std:: io:: Result < ( ) >  { 
@@ -476,3 +486,9 @@ where
476486        Ok ( Seek :: seek ( self ,  pos. into ( ) ) ?) 
477487    } 
478488} 
489+ 
490+ impl < IO :  ReadWriteSeek ,  TP :  TimeProvider ,  OCC >  core:: fmt:: Write  for  File < ' _ ,  IO ,  TP ,  OCC >  { 
491+     fn  write_str ( & mut  self ,  s :  & str )  -> core:: fmt:: Result  { 
492+         <Self  as  WriteFile >:: write_all ( self ,  s. as_bytes ( ) ) . map_err ( |_| core:: fmt:: Error ) 
493+     } 
494+ } 
0 commit comments