@@ -3,6 +3,7 @@ extern crate winapi;
3
3
4
4
use std:: fs;
5
5
use std:: path:: { Path , PathBuf } ;
6
+ use std:: io:: { BufWriter , Write } ;
6
7
7
8
#[ cfg( not( target_os = "windows" ) ) ]
8
9
use std:: os:: unix:: io:: AsRawFd ;
14
15
} ;
15
16
16
17
pub ( 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 > ;
18
19
}
19
20
20
21
pub ( 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
52
53
{
53
54
// Note that going by rust-lang/rust@d602a6b, on MacOS it is only safe to use
54
55
// 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 ( ) ?;
58
59
}
59
60
// Fsync the parent directory on Unix.
60
61
#[ cfg( not( target_os = "windows" ) ) ]
@@ -95,7 +96,7 @@ mod tests {
95
96
96
97
struct TestWriteable { }
97
98
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 > {
99
100
writer. write_all ( & [ 42 ; 1 ] )
100
101
}
101
102
}
@@ -145,7 +146,7 @@ mod tests {
145
146
fn test_diskwriteable_failure ( ) {
146
147
struct FailingWriteable { }
147
148
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 > {
149
150
Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , "expected failure" ) )
150
151
}
151
152
}
0 commit comments