futures_task/
future_obj.rs1use core::{
2    fmt,
3    future::Future,
4    marker::PhantomData,
5    mem,
6    pin::Pin,
7    task::{Context, Poll},
8};
9
10pub struct LocalFutureObj<'a, T> {
17    future: *mut (dyn Future<Output = T> + 'static),
18    drop_fn: unsafe fn(*mut (dyn Future<Output = T> + 'static)),
19    _marker: PhantomData<&'a ()>,
20}
21
22impl<T> Unpin for LocalFutureObj<'_, T> {}
26
27#[allow(single_use_lifetimes)]
28#[allow(clippy::transmute_ptr_to_ptr)]
29unsafe fn remove_future_lifetime<'a, T>(
30    ptr: *mut (dyn Future<Output = T> + 'a),
31) -> *mut (dyn Future<Output = T> + 'static) {
32    unsafe { mem::transmute(ptr) }
33}
34
35#[allow(single_use_lifetimes)]
36unsafe fn remove_drop_lifetime<'a, T>(
37    ptr: unsafe fn(*mut (dyn Future<Output = T> + 'a)),
38) -> unsafe fn(*mut (dyn Future<Output = T> + 'static)) {
39    unsafe { mem::transmute(ptr) }
40}
41
42impl<'a, T> LocalFutureObj<'a, T> {
43    #[inline]
45    pub fn new<F: UnsafeFutureObj<'a, T> + 'a>(f: F) -> Self {
46        Self {
47            future: unsafe { remove_future_lifetime(f.into_raw()) },
48            drop_fn: unsafe { remove_drop_lifetime(F::drop) },
49            _marker: PhantomData,
50        }
51    }
52
53    #[inline]
61    pub unsafe fn into_future_obj(self) -> FutureObj<'a, T> {
62        FutureObj(self)
63    }
64}
65
66impl<T> fmt::Debug for LocalFutureObj<'_, T> {
67    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68        f.debug_struct("LocalFutureObj").finish()
69    }
70}
71
72impl<'a, T> From<FutureObj<'a, T>> for LocalFutureObj<'a, T> {
73    #[inline]
74    fn from(f: FutureObj<'a, T>) -> Self {
75        f.0
76    }
77}
78
79impl<T> Future for LocalFutureObj<'_, T> {
80    type Output = T;
81
82    #[inline]
83    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
84        unsafe { Pin::new_unchecked(&mut *self.future).poll(cx) }
85    }
86}
87
88impl<T> Drop for LocalFutureObj<'_, T> {
89    fn drop(&mut self) {
90        unsafe { (self.drop_fn)(self.future) }
91    }
92}
93
94pub struct FutureObj<'a, T>(LocalFutureObj<'a, T>);
104
105impl<T> Unpin for FutureObj<'_, T> {}
106unsafe impl<T> Send for FutureObj<'_, T> {}
107
108impl<'a, T> FutureObj<'a, T> {
109    #[inline]
111    pub fn new<F: UnsafeFutureObj<'a, T> + Send>(f: F) -> Self {
112        Self(LocalFutureObj::new(f))
113    }
114}
115
116impl<T> fmt::Debug for FutureObj<'_, T> {
117    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118        f.debug_struct("FutureObj").finish()
119    }
120}
121
122impl<T> Future for FutureObj<'_, T> {
123    type Output = T;
124
125    #[inline]
126    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
127        Pin::new(&mut self.0).poll(cx)
128    }
129}
130
131pub unsafe trait UnsafeFutureObj<'a, T>: 'a {
142    #[allow(clippy::unnecessary_safety_doc)]
153    fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a);
154
155    unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a));
174}
175
176unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for &'a mut F
177where
178    F: Future<Output = T> + Unpin + 'a,
179{
180    fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
181        self as *mut dyn Future<Output = T>
182    }
183
184    unsafe fn drop(_ptr: *mut (dyn Future<Output = T> + 'a)) {}
185}
186
187unsafe impl<'a, T> UnsafeFutureObj<'a, T> for &'a mut (dyn Future<Output = T> + Unpin + 'a) {
188    fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
189        self as *mut dyn Future<Output = T>
190    }
191
192    unsafe fn drop(_ptr: *mut (dyn Future<Output = T> + 'a)) {}
193}
194
195unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for Pin<&'a mut F>
196where
197    F: Future<Output = T> + 'a,
198{
199    fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
200        unsafe { self.get_unchecked_mut() as *mut dyn Future<Output = T> }
201    }
202
203    unsafe fn drop(_ptr: *mut (dyn Future<Output = T> + 'a)) {}
204}
205
206unsafe impl<'a, T> UnsafeFutureObj<'a, T> for Pin<&'a mut (dyn Future<Output = T> + 'a)> {
207    fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
208        unsafe { self.get_unchecked_mut() as *mut dyn Future<Output = T> }
209    }
210
211    unsafe fn drop(_ptr: *mut (dyn Future<Output = T> + 'a)) {}
212}
213
214#[cfg(feature = "alloc")]
215mod if_alloc {
216    use super::*;
217    use alloc::boxed::Box;
218
219    unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for Box<F>
220    where
221        F: Future<Output = T> + 'a,
222    {
223        fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
224            Box::into_raw(self)
225        }
226
227        unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a)) {
228            drop(unsafe { Box::from_raw(ptr.cast::<F>()) })
229        }
230    }
231
232    unsafe impl<'a, T: 'a> UnsafeFutureObj<'a, T> for Box<dyn Future<Output = T> + 'a> {
233        fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
234            Box::into_raw(self)
235        }
236
237        unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a)) {
238            drop(unsafe { Box::from_raw(ptr) })
239        }
240    }
241
242    unsafe impl<'a, T: 'a> UnsafeFutureObj<'a, T> for Box<dyn Future<Output = T> + Send + 'a> {
243        fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
244            Box::into_raw(self)
245        }
246
247        unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a)) {
248            drop(unsafe { Box::from_raw(ptr) })
249        }
250    }
251
252    unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for Pin<Box<F>>
253    where
254        F: Future<Output = T> + 'a,
255    {
256        fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
257            let mut this = mem::ManuallyDrop::new(self);
258            unsafe { this.as_mut().get_unchecked_mut() as *mut _ }
259        }
260
261        unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a)) {
262            drop(Pin::from(unsafe { Box::from_raw(ptr) }))
263        }
264    }
265
266    unsafe impl<'a, T: 'a> UnsafeFutureObj<'a, T> for Pin<Box<dyn Future<Output = T> + 'a>> {
267        fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
268            let mut this = mem::ManuallyDrop::new(self);
269            unsafe { this.as_mut().get_unchecked_mut() as *mut _ }
270        }
271
272        unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a)) {
273            drop(Pin::from(unsafe { Box::from_raw(ptr) }))
274        }
275    }
276
277    unsafe impl<'a, T: 'a> UnsafeFutureObj<'a, T> for Pin<Box<dyn Future<Output = T> + Send + 'a>> {
278        fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
279            let mut this = mem::ManuallyDrop::new(self);
280            unsafe { this.as_mut().get_unchecked_mut() as *mut _ }
281        }
282
283        unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a)) {
284            drop(Pin::from(unsafe { Box::from_raw(ptr) }))
285        }
286    }
287
288    impl<'a, F: Future<Output = ()> + Send + 'a> From<Box<F>> for FutureObj<'a, ()> {
289        fn from(boxed: Box<F>) -> Self {
290            Self::new(boxed)
291        }
292    }
293
294    impl<'a> From<Box<dyn Future<Output = ()> + Send + 'a>> for FutureObj<'a, ()> {
295        fn from(boxed: Box<dyn Future<Output = ()> + Send + 'a>) -> Self {
296            Self::new(boxed)
297        }
298    }
299
300    impl<'a, F: Future<Output = ()> + Send + 'a> From<Pin<Box<F>>> for FutureObj<'a, ()> {
301        fn from(boxed: Pin<Box<F>>) -> Self {
302            Self::new(boxed)
303        }
304    }
305
306    impl<'a> From<Pin<Box<dyn Future<Output = ()> + Send + 'a>>> for FutureObj<'a, ()> {
307        fn from(boxed: Pin<Box<dyn Future<Output = ()> + Send + 'a>>) -> Self {
308            Self::new(boxed)
309        }
310    }
311
312    impl<'a, F: Future<Output = ()> + 'a> From<Box<F>> for LocalFutureObj<'a, ()> {
313        fn from(boxed: Box<F>) -> Self {
314            Self::new(boxed)
315        }
316    }
317
318    impl<'a> From<Box<dyn Future<Output = ()> + 'a>> for LocalFutureObj<'a, ()> {
319        fn from(boxed: Box<dyn Future<Output = ()> + 'a>) -> Self {
320            Self::new(boxed)
321        }
322    }
323
324    impl<'a, F: Future<Output = ()> + 'a> From<Pin<Box<F>>> for LocalFutureObj<'a, ()> {
325        fn from(boxed: Pin<Box<F>>) -> Self {
326            Self::new(boxed)
327        }
328    }
329
330    impl<'a> From<Pin<Box<dyn Future<Output = ()> + 'a>>> for LocalFutureObj<'a, ()> {
331        fn from(boxed: Pin<Box<dyn Future<Output = ()> + 'a>>) -> Self {
332            Self::new(boxed)
333        }
334    }
335}