1- use crate :: fs:: { asyncify , File } ;
1+ use crate :: fs:: File ;
22
33use std:: io;
44use std:: path:: Path ;
55
6+ cfg_tokio_unstable_uring ! {
7+ mod uring_open_options;
8+ use uring_open_options:: UringOpenOptions ;
9+ use crate :: runtime:: driver:: op:: Op ;
10+ }
11+
612#[ cfg( test) ]
713mod mock_open_options;
814#[ cfg( test) ]
915use mock_open_options:: MockOpenOptions as StdOpenOptions ;
1016#[ cfg( not( test) ) ]
1117use std:: fs:: OpenOptions as StdOpenOptions ;
1218
13- #[ cfg( unix) ]
14- use std:: os:: unix:: fs:: OpenOptionsExt ;
19+ cfg_not_tokio_unstable_uring ! {
20+ #[ cfg( unix) ]
21+ use std:: os:: unix:: fs:: OpenOptionsExt ;
22+ use crate :: fs:: asyncify;
23+ }
24+
1525#[ cfg( windows) ]
1626use std:: os:: windows:: fs:: OpenOptionsExt ;
1727
@@ -79,7 +89,22 @@ use std::os::windows::fs::OpenOptionsExt;
7989/// }
8090/// ```
8191#[ derive( Clone , Debug ) ]
82- pub struct OpenOptions ( StdOpenOptions ) ;
92+ pub struct OpenOptions (
93+ #[ cfg( not( all(
94+ tokio_unstable_uring,
95+ feature = "rt" ,
96+ feature = "fs" ,
97+ target_os = "linux" ,
98+ ) ) ) ]
99+ StdOpenOptions ,
100+ #[ cfg( all(
101+ tokio_unstable_uring,
102+ feature = "rt" ,
103+ feature = "fs" ,
104+ target_os = "linux" ,
105+ ) ) ]
106+ pub ( crate ) UringOpenOptions ,
107+ ) ;
83108
84109impl OpenOptions {
85110 /// Creates a blank new set of options ready for configuration.
@@ -99,7 +124,22 @@ impl OpenOptions {
99124 /// let future = options.read(true).open("foo.txt");
100125 /// ```
101126 pub fn new ( ) -> OpenOptions {
102- OpenOptions ( StdOpenOptions :: new ( ) )
127+ OpenOptions (
128+ #[ cfg( not( all(
129+ tokio_unstable_uring,
130+ feature = "rt" ,
131+ feature = "fs" ,
132+ target_os = "linux" ,
133+ ) ) ) ]
134+ StdOpenOptions :: new ( ) ,
135+ #[ cfg( all(
136+ tokio_unstable_uring,
137+ feature = "rt" ,
138+ feature = "fs" ,
139+ target_os = "linux" ,
140+ ) ) ]
141+ UringOpenOptions :: new ( ) ,
142+ )
103143 }
104144
105145 /// Sets the option for read access.
@@ -386,17 +426,35 @@ impl OpenOptions {
386426 /// [`Other`]: std::io::ErrorKind::Other
387427 /// [`PermissionDenied`]: std::io::ErrorKind::PermissionDenied
388428 pub async fn open ( & self , path : impl AsRef < Path > ) -> io:: Result < File > {
389- let path = path . as_ref ( ) . to_owned ( ) ;
390- let opts = self . 0 . clone ( ) ;
429+ self . open_inner ( path ) . await
430+ }
391431
392- let std = asyncify ( move || opts. open ( path) ) . await ?;
393- Ok ( File :: from_std ( std) )
432+ cfg_not_tokio_unstable_uring ! {
433+ async fn open_inner( & self , path: impl AsRef <Path >) -> io:: Result <File > {
434+ let path = path. as_ref( ) . to_owned( ) ;
435+ let opts = self . 0 . clone( ) ;
436+
437+ let std = asyncify( move || opts. open( path) ) . await ?;
438+ Ok ( File :: from_std( std) )
439+ }
440+
441+ /// Returns a mutable reference to the underlying `std::fs::OpenOptions`
442+ #[ cfg( any( windows, unix) ) ]
443+ pub ( super ) fn as_inner_mut( & mut self ) -> & mut StdOpenOptions {
444+ & mut self . 0
445+ }
394446 }
395447
396- /// Returns a mutable reference to the underlying `std::fs::OpenOptions`
397- #[ cfg( any( windows, unix) ) ]
398- pub ( super ) fn as_inner_mut ( & mut self ) -> & mut StdOpenOptions {
399- & mut self . 0
448+ cfg_tokio_unstable_uring ! {
449+ async fn open_inner( & self , path: impl AsRef <Path >) -> io:: Result <File > {
450+ Op :: open( path. as_ref( ) , self ) ?. await
451+ }
452+
453+ /// Returns a mutable reference to the underlying `std::fs::OpenOptions`
454+ #[ cfg( any( windows, unix) ) ]
455+ pub ( super ) fn as_inner_mut( & mut self ) -> & mut UringOpenOptions {
456+ & mut self . 0
457+ }
400458 }
401459}
402460
@@ -649,9 +707,23 @@ cfg_windows! {
649707 }
650708}
651709
652- impl From < StdOpenOptions > for OpenOptions {
653- fn from ( options : StdOpenOptions ) -> OpenOptions {
654- OpenOptions ( options)
710+ cfg_not_tokio_unstable_uring ! {
711+ impl From <StdOpenOptions > for OpenOptions {
712+ fn from( options: StdOpenOptions ) -> OpenOptions {
713+ OpenOptions ( options)
714+ }
715+ }
716+ }
717+
718+ cfg_tokio_unstable_uring ! {
719+ impl From <StdOpenOptions > for OpenOptions {
720+ fn from( _options: StdOpenOptions ) -> OpenOptions {
721+ // It's not straitforward to convert from std's OpenOptions to io_uring's one.
722+ // * https://github.com/rust-lang/rust/issues/74943
723+ // * https://github.com/rust-lang/rust/issues/76801
724+
725+ panic!( "Conversion from std's OpenOptions to io_uring's one is not supported" )
726+ }
655727 }
656728}
657729
0 commit comments