76
76
//! [`RawWaker`]: https://doc.rust-lang.org/std/task/struct.RawWaker.html
77
77
78
78
use std:: {
79
+ future:: Future ,
79
80
marker:: PhantomData ,
80
81
ops:: Deref ,
81
82
panic,
@@ -87,29 +88,27 @@ use std::{
87
88
use super :: { RustFutureContinuationCallback , RustFuturePoll , Scheduler , UniffiCompatibleFuture } ;
88
89
use crate :: { rust_call_with_out_status, FfiDefault , LiftArgsError , LowerReturn , RustCallStatus } ;
89
90
91
+ type BoxedFuture < T > = Pin < Box < dyn UniffiCompatibleFuture < Result < T , LiftArgsError > > > > ;
92
+
90
93
/// Wraps the actual future we're polling
91
- struct WrappedFuture < F , T , UT >
94
+ struct WrappedFuture < T , UT >
92
95
where
93
- // See rust_future_new for an explanation of these trait bounds
94
- F : UniffiCompatibleFuture < Result < T , LiftArgsError > > + ' static ,
95
96
T : LowerReturn < UT > + Send + ' static ,
96
97
UT : Send + ' static ,
97
98
{
98
99
// Note: this could be a single enum, but that would make it easy to mess up the future pinning
99
100
// guarantee. For example you might want to call `std::mem::take()` to try to get the result,
100
101
// but if the future happened to be stored that would move and break all internal references.
101
- future : Option < F > ,
102
+ future : Option < BoxedFuture < T > > ,
102
103
result : Option < Result < T :: ReturnType , RustCallStatus > > ,
103
104
}
104
105
105
- impl < F , T , UT > WrappedFuture < F , T , UT >
106
+ impl < T , UT > WrappedFuture < T , UT >
106
107
where
107
- // See rust_future_new for an explanation of these trait bounds
108
- F : UniffiCompatibleFuture < Result < T , LiftArgsError > > + ' static ,
109
108
T : LowerReturn < UT > + Send + ' static ,
110
109
UT : Send + ' static ,
111
110
{
112
- fn new ( future : F ) -> Self {
111
+ fn new ( future : BoxedFuture < T > ) -> Self {
113
112
Self {
114
113
future : Some ( future) ,
115
114
result : None ,
@@ -182,40 +181,34 @@ where
182
181
//
183
182
// Rust will not mark it Send by default when T::ReturnType is a raw pointer. This is promising
184
183
// that we will treat the raw pointer properly, for example by not returning it twice.
185
- unsafe impl < F , T , UT > Send for WrappedFuture < F , T , UT >
184
+ unsafe impl < T , UT > Send for WrappedFuture < T , UT >
186
185
where
187
- // See rust_future_new for an explanation of these trait bounds
188
- F : UniffiCompatibleFuture < Result < T , LiftArgsError > > + ' static ,
189
186
T : LowerReturn < UT > + Send + ' static ,
190
187
UT : Send + ' static ,
191
188
{
192
189
}
193
190
194
191
/// Future that the foreign code is awaiting
195
- pub ( super ) struct RustFuture < F , T , UT >
192
+ pub ( super ) struct RustFuture < T , UT >
196
193
where
197
- // See rust_future_new for an explanation of these trait bounds
198
- F : UniffiCompatibleFuture < Result < T , LiftArgsError > > + ' static ,
199
194
T : LowerReturn < UT > + Send + ' static ,
200
195
UT : Send + ' static ,
201
196
{
202
197
// This Mutex should never block if our code is working correctly, since there should not be
203
198
// multiple threads calling [Self::poll] and/or [Self::complete] at the same time.
204
- future : Mutex < WrappedFuture < F , T , UT > > ,
199
+ future : Mutex < WrappedFuture < T , UT > > ,
205
200
scheduler : Mutex < Scheduler > ,
206
201
// UT is used as the generic parameter for [LowerReturn].
207
202
// Let's model this with PhantomData as a function that inputs a UT value.
208
203
_phantom : PhantomData < fn ( UT ) -> ( ) > ,
209
204
}
210
205
211
- impl < F , T , UT > RustFuture < F , T , UT >
206
+ impl < T , UT > RustFuture < T , UT >
212
207
where
213
- // See rust_future_new for an explanation of these trait bounds
214
- F : UniffiCompatibleFuture < Result < T , LiftArgsError > > + ' static ,
215
208
T : LowerReturn < UT > + Send + ' static ,
216
209
UT : Send + ' static ,
217
210
{
218
- pub ( super ) fn new ( future : F , _tag : UT ) -> Arc < Self > {
211
+ pub ( super ) fn new ( future : BoxedFuture < T > , _tag : UT ) -> Arc < Self > {
219
212
Arc :: new ( Self {
220
213
future : Mutex :: new ( WrappedFuture :: new ( future) ) ,
221
214
scheduler : Mutex :: new ( Scheduler :: new ( ) ) ,
@@ -263,10 +256,8 @@ where
263
256
}
264
257
}
265
258
266
- impl < F , T , UT > Wake for RustFuture < F , T , UT >
259
+ impl < T , UT > Wake for RustFuture < T , UT >
267
260
where
268
- // See rust_future_new for an explanation of these trait bounds
269
- F : UniffiCompatibleFuture < Result < T , LiftArgsError > > + ' static ,
270
261
T : LowerReturn < UT > + Send + ' static ,
271
262
UT : Send + ' static ,
272
263
{
@@ -298,10 +289,8 @@ pub trait RustFutureFfi<ReturnType>: Send + Sync {
298
289
fn ffi_free ( self : Arc < Self > ) ;
299
290
}
300
291
301
- impl < F , T , UT > RustFutureFfi < T :: ReturnType > for RustFuture < F , T , UT >
292
+ impl < T , UT > RustFutureFfi < T :: ReturnType > for RustFuture < T , UT >
302
293
where
303
- // See rust_future_new for an explanation of these trait bounds
304
- F : UniffiCompatibleFuture < Result < T , LiftArgsError > > + ' static ,
305
294
T : LowerReturn < UT > + Send + ' static ,
306
295
UT : Send + ' static ,
307
296
{
0 commit comments