@@ -3,6 +3,7 @@ extern crate winapi;
33
44use std:: fs;
55use std:: path:: { Path , PathBuf } ;
6+ use std:: io:: { BufWriter , Write } ;
67
78#[ cfg( not( target_os = "windows" ) ) ]
89use std:: os:: unix:: io:: AsRawFd ;
1415} ;
1516
1617pub ( crate ) trait DiskWriteable {
17- fn write_to_file ( & self , writer : & mut fs :: File ) -> Result < ( ) , std:: io:: Error > ;
18+ fn write_to_file < W : Write > ( & self , writer : & mut W ) -> Result < ( ) , std:: io:: Error > ;
1819}
1920
2021pub ( crate ) fn get_full_filepath ( mut filepath : PathBuf , filename : String ) -> String {
@@ -52,9 +53,9 @@ pub(crate) fn write_to_file<D: DiskWriteable>(path: PathBuf, filename: String, d
5253 {
5354 // Note that going by rust-lang/rust@d602a6b, on MacOS it is only safe to use
5455 // rust stdlib 1.36 or higher.
55- let mut f = fs:: File :: create ( & tmp_filename) ?;
56- data. write_to_file ( & mut f ) ?;
57- f . sync_all ( ) ?;
56+ let mut buf = BufWriter :: new ( fs:: File :: create ( & tmp_filename) ?) ;
57+ data. write_to_file ( & mut buf ) ?;
58+ buf . into_inner ( ) ? . sync_all ( ) ?;
5859 }
5960 // Fsync the parent directory on Unix.
6061 #[ cfg( not( target_os = "windows" ) ) ]
@@ -95,7 +96,7 @@ mod tests {
9596
9697 struct TestWriteable { }
9798 impl DiskWriteable for TestWriteable {
98- fn write_to_file ( & self , writer : & mut fs :: File ) -> Result < ( ) , io:: Error > {
99+ fn write_to_file < W : Write > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
99100 writer. write_all ( & [ 42 ; 1 ] )
100101 }
101102 }
@@ -145,7 +146,7 @@ mod tests {
145146 fn test_diskwriteable_failure ( ) {
146147 struct FailingWriteable { }
147148 impl DiskWriteable for FailingWriteable {
148- fn write_to_file ( & self , _writer : & mut fs :: File ) -> Result < ( ) , std:: io:: Error > {
149+ fn write_to_file < W : Write > ( & self , _writer : & mut W ) -> Result < ( ) , std:: io:: Error > {
149150 Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , "expected failure" ) )
150151 }
151152 }
0 commit comments