@@ -18,33 +18,23 @@ bitflags! {
1818 }
1919}
2020
21- /// Represents a handle to an operating system resource, such as a file or socket .
21+ /// Represents a handle to a file.
2222#[ derive( Debug ) ]
23- pub ( crate ) enum Handle {
23+ pub ( crate ) struct FileHandle {
2424 #[ cfg( all( target_family = "wasm" , not( target_os = "wasi" ) ) ) ]
25- File {
26- file : String ,
27- append : bool ,
28- mode : FileModeFlags ,
29- } ,
25+ pub ( crate ) file : String ,
3026 #[ cfg( target_os = "wasi" ) ]
31- File {
32- file : std:: fs:: File ,
33- append : bool ,
34- mode : FileModeFlags ,
35- } ,
27+ pub ( crate ) file : std:: fs:: File ,
3628 #[ cfg( not( target_family = "wasm" ) ) ]
37- File {
38- file : tokio:: fs:: File ,
39- append : bool ,
40- mode : FileModeFlags ,
41- } ,
29+ pub ( crate ) file : tokio:: fs:: File ,
30+ pub ( crate ) append : bool ,
31+ pub ( crate ) mode : FileModeFlags ,
4232}
4333
4434#[ cfg( target_os = "wasi" ) ]
45- impl From < ( std:: fs:: File , FileModeFlags ) > for Handle {
35+ impl From < ( std:: fs:: File , FileModeFlags ) > for FileHandle {
4636 fn from ( ( file, mode) : ( std:: fs:: File , FileModeFlags ) ) -> Self {
47- Handle :: File {
37+ FileHandle {
4838 file,
4939 append : false ,
5040 mode,
@@ -53,9 +43,9 @@ impl From<(std::fs::File, FileModeFlags)> for Handle {
5343}
5444
5545#[ cfg( not( target_family = "wasm" ) ) ]
56- impl From < ( tokio:: fs:: File , FileModeFlags ) > for Handle {
46+ impl From < ( tokio:: fs:: File , FileModeFlags ) > for FileHandle {
5747 fn from ( ( file, mode) : ( tokio:: fs:: File , FileModeFlags ) ) -> Self {
58- Handle :: File {
48+ FileHandle {
5949 file,
6050 append : false ,
6151 mode,
@@ -64,9 +54,9 @@ impl From<(tokio::fs::File, FileModeFlags)> for Handle {
6454}
6555
6656#[ cfg( target_os = "wasi" ) ]
67- impl From < ( std:: fs:: File , bool ) > for Handle {
57+ impl From < ( std:: fs:: File , bool ) > for FileHandle {
6858 fn from ( ( file, append) : ( std:: fs:: File , bool ) ) -> Self {
69- Handle :: File {
59+ FileHandle {
7060 file,
7161 append,
7262 mode : FileModeFlags :: empty ( ) ,
@@ -75,9 +65,9 @@ impl From<(std::fs::File, bool)> for Handle {
7565}
7666
7767#[ cfg( not( target_family = "wasm" ) ) ]
78- impl From < ( tokio:: fs:: File , bool ) > for Handle {
68+ impl From < ( tokio:: fs:: File , bool ) > for FileHandle {
7969 fn from ( ( file, append) : ( tokio:: fs:: File , bool ) ) -> Self {
80- Handle :: File {
70+ FileHandle {
8171 file,
8272 append,
8373 mode : FileModeFlags :: empty ( ) ,
@@ -86,24 +76,22 @@ impl From<(tokio::fs::File, bool)> for Handle {
8676}
8777
8878#[ cfg( target_os = "wasi" ) ]
89- impl TryInto < std:: fs:: File > for Handle {
79+ impl TryInto < std:: fs:: File > for FileHandle {
9080 type Error = crate :: Error ;
9181
9282 fn try_into ( self ) -> Result < std:: fs:: File , Self :: Error > {
93- match self {
94- Handle :: File { file, .. } => Ok ( file) ,
95- }
83+ let FileHandle { file, .. } = self ;
84+ Ok ( file)
9685 }
9786}
9887
9988#[ cfg( not( target_family = "wasm" ) ) ]
100- impl TryInto < tokio:: fs:: File > for Handle {
89+ impl TryInto < tokio:: fs:: File > for FileHandle {
10190 type Error = crate :: Error ;
10291
10392 fn try_into ( self ) -> Result < tokio:: fs:: File , Self :: Error > {
104- match self {
105- Handle :: File { file, .. } => Ok ( file) ,
106- }
93+ let FileHandle { file, .. } = self ;
94+ Ok ( file)
10795 }
10896}
10997
@@ -114,13 +102,13 @@ mod tests {
114102 use tokio:: fs:: { File , remove_file} ;
115103
116104 #[ tokio:: test]
117- async fn test_handle_from_file_and_append ( ) -> Result < ( ) > {
105+ async fn test_file_handle_from_file_and_append ( ) -> Result < ( ) > {
118106 let file_name = "test_handle_from_file_and_append.txt" ;
119107 let file = File :: create ( file_name) . await ?;
120- let handle : Handle = ( file, false ) . into ( ) ;
108+ let file_handle : FileHandle = ( file, false ) . into ( ) ;
121109 assert ! ( matches!(
122- handle ,
123- Handle :: File {
110+ file_handle ,
111+ FileHandle {
124112 file: _,
125113 append: false ,
126114 mode,
@@ -131,14 +119,14 @@ mod tests {
131119 }
132120
133121 #[ tokio:: test]
134- async fn test_handle_from_file_and_mode ( ) -> Result < ( ) > {
122+ async fn test_file_handle_from_file_and_mode ( ) -> Result < ( ) > {
135123 let file_name = "test_handle_from_file_and_mode.txt" ;
136124 let file = File :: create ( file_name) . await ?;
137125 let expected_mode = FileModeFlags :: READ_WRITE ;
138- let handle : Handle = ( file, expected_mode) . into ( ) ;
126+ let file_handle : FileHandle = ( file, expected_mode) . into ( ) ;
139127 assert ! ( matches!(
140- handle ,
141- Handle :: File {
128+ file_handle ,
129+ FileHandle {
142130 file: _,
143131 append: false ,
144132 mode,
@@ -149,11 +137,11 @@ mod tests {
149137 }
150138
151139 #[ tokio:: test]
152- async fn test_handle_try_into_file ( ) -> Result < ( ) > {
140+ async fn test_file_handle_try_into_file ( ) -> Result < ( ) > {
153141 let file_name = "test_handle_try_into_file.txt" ;
154142 let file = File :: create ( file_name) . await ?;
155- let handle : Handle = ( file, false ) . into ( ) ;
156- let extracted_file: File = handle . try_into ( ) ?;
143+ let file_handle : FileHandle = ( file, false ) . into ( ) ;
144+ let extracted_file: File = file_handle . try_into ( ) ?;
157145 assert ! ( extracted_file. metadata( ) . await . is_ok( ) ) ;
158146 remove_file ( file_name) . await ?;
159147 Ok ( ( ) )
0 commit comments