@@ -58,23 +58,42 @@ pub(crate) use tokio_impl::TokioFs;
5858
5959#[ cfg( feature = "tokio" ) ]
6060mod tokio_impl {
61+ use core:: {
62+ pin:: Pin ,
63+ task:: { Context , Poll } ,
64+ } ;
65+
6166 use tokio:: {
6267 fs:: File ,
6368 io:: { AsyncReadExt , AsyncSeekExt } ,
6469 } ;
6570
6671 use super :: * ;
6772
73+ type BoxFuture < ' a , T > = Pin < Box < dyn Future < Output = T > + Send + ' a > > ;
74+
75+ pub struct OpenFuture < F > {
76+ handle : tokio:: task:: JoinHandle < F > ,
77+ }
78+
79+ impl < F > Future for OpenFuture < F > {
80+ type Output = F ;
81+
82+ fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
83+ Pin :: new ( & mut self . get_mut ( ) . handle ) . poll ( cx) . map ( |res| res. unwrap ( ) )
84+ }
85+ }
86+
6887 #[ derive( Clone ) ]
6988 pub struct TokioFs ;
7089
7190 impl AsyncFs for TokioFs {
7291 type File = TokioFile ;
73- type OpenFuture = impl Future < Output = io:: Result < Self :: File > > + Send ;
92+ type OpenFuture = OpenFuture < io:: Result < Self :: File > > ;
7493
7594 fn open ( & self , path : PathBuf ) -> Self :: OpenFuture {
76- async {
77- tokio:: task:: spawn_blocking ( move || {
95+ OpenFuture {
96+ handle : tokio:: task:: spawn_blocking ( move || {
7897 let file = std:: fs:: File :: open ( path) ?;
7998 let meta = file. metadata ( ) ?;
8099 let modified_time = meta. modified ( ) . ok ( ) ;
@@ -84,9 +103,7 @@ mod tokio_impl {
84103 modified_time,
85104 len,
86105 } )
87- } )
88- . await
89- . unwrap ( )
106+ } ) ,
90107 }
91108 }
92109 }
@@ -109,25 +126,25 @@ mod tokio_impl {
109126
110127 impl ChunkRead for TokioFile {
111128 type SeekFuture < ' f >
112- = impl Future < Output = io:: Result < ( ) > > + Send + ' f
129+ = BoxFuture < ' f , io:: Result < ( ) > >
113130 where
114131 Self : ' f ;
115132
116- type Future = impl Future < Output = io:: Result < Option < ( Self , BytesMut , usize ) > > > + Send ;
133+ type Future = BoxFuture < ' static , io:: Result < Option < ( Self , BytesMut , usize ) > > > ;
117134
118135 fn seek ( & mut self , pos : SeekFrom ) -> Self :: SeekFuture < ' _ > {
119- async move { self . file . seek ( pos) . await . map ( |_| ( ) ) }
136+ Box :: pin ( async move { self . file . seek ( pos) . await . map ( |_| ( ) ) } )
120137 }
121138
122139 fn next ( mut self , mut buf : BytesMut ) -> Self :: Future {
123- async {
140+ Box :: pin ( async {
124141 let n = self . file . read_buf ( & mut buf) . await ?;
125142 if n == 0 {
126143 Ok ( None )
127144 } else {
128145 Ok ( Some ( ( self , buf, n) ) )
129146 }
130- }
147+ } )
131148 }
132149 }
133150}
@@ -137,19 +154,26 @@ pub(crate) use tokio_uring_impl::TokioUringFs;
137154
138155#[ cfg( feature = "tokio-uring" ) ]
139156mod tokio_uring_impl {
157+ use core:: {
158+ future:: { ready, Ready } ,
159+ pin:: Pin ,
160+ } ;
161+
140162 use tokio_uring:: fs:: File ;
141163
142164 use super :: * ;
143165
166+ type BoxFuture < ' f , T > = Pin < Box < dyn Future < Output = T > + ' f > > ;
167+
144168 #[ derive( Clone ) ]
145169 pub struct TokioUringFs ;
146170
147171 impl AsyncFs for TokioUringFs {
148172 type File = TokioUringFile ;
149- type OpenFuture = impl Future < Output = io:: Result < Self :: File > > ;
173+ type OpenFuture = BoxFuture < ' static , io:: Result < Self :: File > > ;
150174
151175 fn open ( & self , path : PathBuf ) -> Self :: OpenFuture {
152- async {
176+ Box :: pin ( async {
153177 let file = File :: open ( path) . await ?;
154178
155179 // SAFETY: fd is borrowed and lives longer than the unsafe block
@@ -173,7 +197,7 @@ mod tokio_uring_impl {
173197 modified_time,
174198 len,
175199 } )
176- }
200+ } )
177201 }
178202 }
179203
@@ -196,22 +220,22 @@ mod tokio_uring_impl {
196220
197221 impl ChunkRead for TokioUringFile {
198222 type SeekFuture < ' f >
199- = impl Future < Output = io:: Result < ( ) > > + ' f
223+ = Ready < io:: Result < ( ) > >
200224 where
201225 Self : ' f ;
202226
203- type Future = impl Future < Output = io:: Result < Option < ( Self , BytesMut , usize ) > > > ;
227+ type Future = BoxFuture < ' static , io:: Result < Option < ( Self , BytesMut , usize ) > > > ;
204228
205229 fn seek ( & mut self , pos : SeekFrom ) -> Self :: SeekFuture < ' _ > {
206230 let SeekFrom :: Start ( pos) = pos else {
207231 unreachable ! ( "ChunkRead::seek only accept pos as SeekFrom::Start variant" )
208232 } ;
209233 self . pos += pos;
210- async { Ok ( ( ) ) }
234+ ready ( Ok ( ( ) ) )
211235 }
212236
213237 fn next ( mut self , buf : BytesMut ) -> Self :: Future {
214- async {
238+ Box :: pin ( async {
215239 let ( res, buf) = self . file . read_at ( buf, self . pos ) . await ;
216240 let n = res?;
217241 if n == 0 {
@@ -220,7 +244,7 @@ mod tokio_uring_impl {
220244 self . pos += n as u64 ;
221245 Ok ( Some ( ( self , buf, n) ) )
222246 }
223- }
247+ } )
224248 }
225249 }
226250}
0 commit comments