|
| 1 | +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +#![unstable(feature = "futures_api", |
| 12 | + reason = "futures in libcore are unstable", |
| 13 | + issue = "50547")] |
| 14 | + |
| 15 | +use fmt; |
| 16 | +use future::Future; |
| 17 | +use marker::{PhantomData, Unpin}; |
| 18 | +use mem::PinMut; |
| 19 | +use task::{Context, Poll}; |
| 20 | + |
| 21 | +/// A custom trait object for polling futures, roughly akin to |
| 22 | +/// `Box<dyn Future<Output = T> + 'a>`. |
| 23 | +/// |
| 24 | +/// This custom trait object was introduced for two reasons: |
| 25 | +/// - Currently it is not possible to take `dyn Trait` by value and |
| 26 | +/// `Box<dyn Trait>` is not available in no_std contexts. |
| 27 | +/// - The `Future` trait is currently not object safe: The `Future::poll` |
| 28 | +/// method makes uses the arbitrary self types feature and traits in which |
| 29 | +/// this feature is used are currently not object safe due to current compiler |
| 30 | +/// limitations. (See tracking issue for arbitray self types for more |
| 31 | +/// information #44874) |
| 32 | +pub struct LocalFutureObj<'a, T> { |
| 33 | + ptr: *mut (), |
| 34 | + poll_fn: unsafe fn(*mut (), &mut Context) -> Poll<T>, |
| 35 | + drop_fn: unsafe fn(*mut ()), |
| 36 | + _marker: PhantomData<&'a ()>, |
| 37 | +} |
| 38 | + |
| 39 | +impl<'a, T> LocalFutureObj<'a, T> { |
| 40 | + /// Create a `LocalFutureObj` from a custom trait object representation. |
| 41 | + #[inline] |
| 42 | + pub fn new<F: UnsafeFutureObj<'a, T> + 'a>(f: F) -> LocalFutureObj<'a, T> { |
| 43 | + LocalFutureObj { |
| 44 | + ptr: f.into_raw(), |
| 45 | + poll_fn: F::poll, |
| 46 | + drop_fn: F::drop, |
| 47 | + _marker: PhantomData, |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /// Converts the `LocalFutureObj` into a `FutureObj` |
| 52 | + /// To make this operation safe one has to ensure that the `UnsafeFutureObj` |
| 53 | + /// instance from which this `LocalFutureObj` was created actually |
| 54 | + /// implements `Send`. |
| 55 | + #[inline] |
| 56 | + pub unsafe fn into_future_obj(self) -> FutureObj<'a, T> { |
| 57 | + FutureObj(self) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl<'a, T> fmt::Debug for LocalFutureObj<'a, T> { |
| 62 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 63 | + f.debug_struct("LocalFutureObj") |
| 64 | + .finish() |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl<'a, T> From<FutureObj<'a, T>> for LocalFutureObj<'a, T> { |
| 69 | + #[inline] |
| 70 | + fn from(f: FutureObj<'a, T>) -> LocalFutureObj<'a, T> { |
| 71 | + f.0 |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl<'a, T> Future for LocalFutureObj<'a, T> { |
| 76 | + type Output = T; |
| 77 | + |
| 78 | + #[inline] |
| 79 | + fn poll(self: PinMut<Self>, cx: &mut Context) -> Poll<T> { |
| 80 | + unsafe { |
| 81 | + (self.poll_fn)(self.ptr, cx) |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl<'a, T> Drop for LocalFutureObj<'a, T> { |
| 87 | + fn drop(&mut self) { |
| 88 | + unsafe { |
| 89 | + (self.drop_fn)(self.ptr) |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/// A custom trait object for polling futures, roughly akin to |
| 95 | +/// `Box<dyn Future<Output = T> + Send + 'a>`. |
| 96 | +/// |
| 97 | +/// This custom trait object was introduced for two reasons: |
| 98 | +/// - Currently it is not possible to take `dyn Trait` by value and |
| 99 | +/// `Box<dyn Trait>` is not available in no_std contexts. |
| 100 | +/// - The `Future` trait is currently not object safe: The `Future::poll` |
| 101 | +/// method makes uses the arbitrary self types feature and traits in which |
| 102 | +/// this feature is used are currently not object safe due to current compiler |
| 103 | +/// limitations. (See tracking issue for arbitray self types for more |
| 104 | +/// information #44874) |
| 105 | +pub struct FutureObj<'a, T>(LocalFutureObj<'a, T>); |
| 106 | + |
| 107 | +unsafe impl<'a, T> Send for FutureObj<'a, T> {} |
| 108 | + |
| 109 | +impl<'a, T> FutureObj<'a, T> { |
| 110 | + /// Create a `FutureObj` from a custom trait object representation. |
| 111 | + #[inline] |
| 112 | + pub fn new<F: UnsafeFutureObj<'a, T> + Send>(f: F) -> FutureObj<'a, T> { |
| 113 | + FutureObj(LocalFutureObj::new(f)) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl<'a, T> fmt::Debug for FutureObj<'a, T> { |
| 118 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 119 | + f.debug_struct("FutureObj") |
| 120 | + .finish() |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +impl<'a, T> Future for FutureObj<'a, T> { |
| 125 | + type Output = T; |
| 126 | + |
| 127 | + #[inline] |
| 128 | + fn poll(self: PinMut<Self>, cx: &mut Context) -> Poll<T> { |
| 129 | + let pinned_field = unsafe { PinMut::map_unchecked(self, |x| &mut x.0) }; |
| 130 | + pinned_field.poll(cx) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +/// A custom implementation of a future trait object for `FutureObj`, providing |
| 135 | +/// a hand-rolled vtable. |
| 136 | +/// |
| 137 | +/// This custom representation is typically used only in `no_std` contexts, |
| 138 | +/// where the default `Box`-based implementation is not available. |
| 139 | +/// |
| 140 | +/// The implementor must guarantee that it is safe to call `poll` repeatedly (in |
| 141 | +/// a non-concurrent fashion) with the result of `into_raw` until `drop` is |
| 142 | +/// called. |
| 143 | +pub unsafe trait UnsafeFutureObj<'a, T>: 'a { |
| 144 | + /// Convert an owned instance into a (conceptually owned) void pointer. |
| 145 | + fn into_raw(self) -> *mut (); |
| 146 | + |
| 147 | + /// Poll the future represented by the given void pointer. |
| 148 | + /// |
| 149 | + /// # Safety |
| 150 | + /// |
| 151 | + /// The trait implementor must guarantee that it is safe to repeatedly call |
| 152 | + /// `poll` with the result of `into_raw` until `drop` is called; such calls |
| 153 | + /// are not, however, allowed to race with each other or with calls to |
| 154 | + /// `drop`. |
| 155 | + unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll<T>; |
| 156 | + |
| 157 | + /// Drops the future represented by the given void pointer. |
| 158 | + /// |
| 159 | + /// # Safety |
| 160 | + /// |
| 161 | + /// The trait implementor must guarantee that it is safe to call this |
| 162 | + /// function once per `into_raw` invocation; that call cannot race with |
| 163 | + /// other calls to `drop` or `poll`. |
| 164 | + unsafe fn drop(ptr: *mut ()); |
| 165 | +} |
| 166 | + |
| 167 | +unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for &'a mut F |
| 168 | + where F: Future<Output = T> + Unpin + 'a |
| 169 | +{ |
| 170 | + fn into_raw(self) -> *mut () { |
| 171 | + self as *mut F as *mut () |
| 172 | + } |
| 173 | + |
| 174 | + unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll<T> { |
| 175 | + PinMut::new_unchecked(&mut *(ptr as *mut F)).poll(cx) |
| 176 | + } |
| 177 | + |
| 178 | + unsafe fn drop(_ptr: *mut ()) {} |
| 179 | +} |
0 commit comments