@@ -3,6 +3,7 @@ use std::io::{self, Read, Seek, Write};
3
3
use std:: path:: { Path , PathBuf } ;
4
4
5
5
use crate :: errors:: { Error , ErrorKind } ;
6
+ use crate :: OpenOptions ;
6
7
7
8
/// Wrapper around [`std::fs::File`][std::fs::File] which adds more helpful
8
9
/// information to all errors.
@@ -57,6 +58,33 @@ impl File {
57
58
}
58
59
}
59
60
61
+ /// Opens a file in read-write mode.
62
+ ///
63
+ /// Wrapper for [`File::create_new`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.create_new).
64
+ pub fn create_new < P > ( path : P ) -> Result < Self , io:: Error >
65
+ where
66
+ P : Into < PathBuf > ,
67
+ {
68
+ let path = path. into ( ) ;
69
+ // TODO: Use fs::File::create_new once MSRV is at least 1.77
70
+ match fs:: OpenOptions :: new ( )
71
+ . read ( true )
72
+ . write ( true )
73
+ . create_new ( true )
74
+ . open ( & path)
75
+ {
76
+ Ok ( file) => Ok ( File :: from_parts ( file, path) ) ,
77
+ Err ( err) => Err ( Error :: build ( err, ErrorKind :: CreateFile , path) ) ,
78
+ }
79
+ }
80
+
81
+ /// Returns a new `OpenOptions` object.
82
+ ///
83
+ /// Wrapper for [`File::options`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.options).
84
+ pub fn options ( ) -> OpenOptions {
85
+ OpenOptions :: new ( )
86
+ }
87
+
60
88
/// Attempts to sync all OS-internal metadata to disk.
61
89
///
62
90
/// Wrapper for [`File::sync_all`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.sync_all).
@@ -178,7 +206,7 @@ impl Read for File {
178
206
}
179
207
}
180
208
181
- impl < ' a > Read for & ' a File {
209
+ impl Read for & File {
182
210
fn read ( & mut self , buf : & mut [ u8 ] ) -> std:: io:: Result < usize > {
183
211
( & self . file )
184
212
. read ( buf)
@@ -206,7 +234,7 @@ impl Seek for File {
206
234
}
207
235
}
208
236
209
- impl < ' a > Seek for & ' a File {
237
+ impl Seek for & File {
210
238
fn seek ( & mut self , pos : std:: io:: SeekFrom ) -> std:: io:: Result < u64 > {
211
239
( & self . file )
212
240
. seek ( pos)
@@ -234,7 +262,7 @@ impl Write for File {
234
262
}
235
263
}
236
264
237
- impl < ' a > Write for & ' a File {
265
+ impl Write for & File {
238
266
fn write ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
239
267
( & self . file )
240
268
. write ( buf)
0 commit comments