@@ -66,6 +66,23 @@ pub struct File {
6666}
6767
6868impl File {
69+ /// Creates an async file handle.
70+ pub ( crate ) fn new ( file : std:: fs:: File , is_flushed : bool ) -> File {
71+ let file = Arc :: new ( file) ;
72+
73+ File {
74+ file : file. clone ( ) ,
75+ lock : Lock :: new ( State {
76+ file,
77+ mode : Mode :: Idle ,
78+ cache : Vec :: new ( ) ,
79+ is_flushed,
80+ last_read_err : None ,
81+ last_write_err : None ,
82+ } ) ,
83+ }
84+ }
85+
6986 /// Opens a file in read-only mode.
7087 ///
7188 /// See the [`OpenOptions::open`] function for more options.
@@ -96,7 +113,7 @@ impl File {
96113 pub async fn open < P : AsRef < Path > > ( path : P ) -> io:: Result < File > {
97114 let path = path. as_ref ( ) . to_owned ( ) ;
98115 let file = blocking:: spawn ( move || std:: fs:: File :: open ( & path) ) . await ?;
99- Ok ( file . into ( ) )
116+ Ok ( File :: new ( file , true ) )
100117 }
101118
102119 /// Opens a file in write-only mode.
@@ -131,7 +148,7 @@ impl File {
131148 pub async fn create < P : AsRef < Path > > ( path : P ) -> io:: Result < File > {
132149 let path = path. as_ref ( ) . to_owned ( ) ;
133150 let file = blocking:: spawn ( move || std:: fs:: File :: create ( & path) ) . await ?;
134- Ok ( file . into ( ) )
151+ Ok ( File :: new ( file , true ) )
135152 }
136153
137154 /// Synchronizes OS-internal buffered contents and metadata to disk.
@@ -383,19 +400,7 @@ impl Seek for &File {
383400
384401impl From < std:: fs:: File > for File {
385402 fn from ( file : std:: fs:: File ) -> File {
386- let file = Arc :: new ( file) ;
387-
388- File {
389- file : file. clone ( ) ,
390- lock : Lock :: new ( State {
391- file,
392- mode : Mode :: Idle ,
393- cache : Vec :: new ( ) ,
394- is_flushed : false ,
395- last_read_err : None ,
396- last_write_err : None ,
397- } ) ,
398- }
403+ File :: new ( file, false )
399404 }
400405}
401406
0 commit comments