@@ -3,6 +3,7 @@ use std::io::{self, Read, Seek, Write};
33use std:: path:: { Path , PathBuf } ;
44
55use crate :: errors:: { Error , ErrorKind } ;
6+ use crate :: OpenOptions ;
67
78/// Wrapper around [`std::fs::File`][std::fs::File] which adds more helpful
89/// information to all errors.
@@ -57,6 +58,33 @@ impl File {
5758 }
5859 }
5960
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+
6088 /// Attempts to sync all OS-internal metadata to disk.
6189 ///
6290 /// 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 {
178206 }
179207}
180208
181- impl < ' a > Read for & ' a File {
209+ impl Read for & File {
182210 fn read ( & mut self , buf : & mut [ u8 ] ) -> std:: io:: Result < usize > {
183211 ( & self . file )
184212 . read ( buf)
@@ -206,7 +234,7 @@ impl Seek for File {
206234 }
207235}
208236
209- impl < ' a > Seek for & ' a File {
237+ impl Seek for & File {
210238 fn seek ( & mut self , pos : std:: io:: SeekFrom ) -> std:: io:: Result < u64 > {
211239 ( & self . file )
212240 . seek ( pos)
@@ -234,7 +262,7 @@ impl Write for File {
234262 }
235263}
236264
237- impl < ' a > Write for & ' a File {
265+ impl Write for & File {
238266 fn write ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
239267 ( & self . file )
240268 . write ( buf)
0 commit comments