use core::cmp::Ordering;
use core::convert::From;
use core::fmt;
-use core::future::{Future, FutureObj, LocalFutureObj, UnsafeFutureObj};
+use core::future::Future;
use core::hash::{Hash, Hasher};
use core::iter::FusedIterator;
use core::marker::{Unpin, Unsize};
use core::mem;
-use core::pin::PinMut;
+use core::pin::Pin;
use core::ops::{CoerceUnsized, Deref, DerefMut, Generator, GeneratorState};
use core::ptr::{self, NonNull, Unique};
-use core::task::{Context, Poll, Spawn, SpawnErrorKind, SpawnObjError};
+use core::task::{LocalWaker, Poll};
use raw_vec::RawVec;
-use pin::PinBox;
use str::from_boxed_utf8_unchecked;
/// A pointer type for heap allocation.
pub fn new(x: T) -> Box<T> {
box x
}
+
+ #[unstable(feature = "pin", issue = "49150")]
+ #[inline(always)]
+ pub fn pinned(x: T) -> Pin<Box<T>> {
+ (box x).into()
+ }
}
impl<T: ?Sized> Box<T> {
}
}
+#[unstable(feature = "pin", issue = "49150")]
+impl<T> From<Box<T>> for Pin<Box<T>> {
+ fn from(boxed: Box<T>) -> Self {
+ // It's not possible to move or replace the insides of a `Pin<Box<T>>`
+ // when `T: !Unpin`, so it's safe to pin it directly without any
+ // additional requirements.
+ unsafe { Pin::new_unchecked(boxed) }
+ }
+}
+
#[stable(feature = "box_from_slice", since = "1.17.0")]
impl<'a, T: Copy> From<&'a [T]> for Box<[T]> {
fn from(slice: &'a [T]) -> Box<[T]> {
}
}
+/* Nota bene
+ *
+ * We could have chosen not to add this impl, and instead have written a
+ * function of Pin<Box<T>> to Pin<T>. Such a function would not be sound,
+ * because Box<T> implements Unpin even when T does not, as a result of
+ * this impl.
+ *
+ * We chose this API instead of the alternative for a few reasons:
+ * - Logically, it is helpful to understand pinning in regard to the
+ * memory region being pointed to. For this reason none of the
+ * standard library pointer types support projecting through a pin
+ * (Box<T> is the only pointer type in std for which this would be
+ * safe.)
+ * - It is in practice very useful to have Box<T> be unconditionally
+ * Unpin because of trait objects, for which the structural auto
+ * trait functionality does not apply (e.g. Box<dyn Foo> would
+ * otherwise not be Unpin).
+ *
+ * Another type with the same semantics as Box but only a conditional
+ * implementation of `Unpin` (where `T: Unpin`) would be valid/safe, and
+ * could have a method to project a Pin<T> from it.
+ */
+#[unstable(feature = "pin", issue = "49150")]
+impl<T: ?Sized> Unpin for Box<T> { }
+
#[unstable(feature = "generator_trait", issue = "43122")]
impl<T> Generator for Box<T>
where T: Generator + ?Sized
impl<F: ?Sized + Future + Unpin> Future for Box<F> {
type Output = F::Output;
- fn poll(mut self: PinMut<Self>, cx: &mut Context) -> Poll<Self::Output> {
- PinMut::new(&mut **self).poll(cx)
- }
-}
-
-#[unstable(feature = "futures_api", issue = "50547")]
-unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for Box<F>
- where F: Future<Output = T> + 'a
-{
- fn into_raw(self) -> *mut () {
- Box::into_raw(self) as *mut ()
- }
-
- unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll<T> {
- let ptr = ptr as *mut F;
- let pin: PinMut<F> = PinMut::new_unchecked(&mut *ptr);
- pin.poll(cx)
- }
-
- unsafe fn drop(ptr: *mut ()) {
- drop(Box::from_raw(ptr as *mut F))
- }
-}
-
-#[unstable(feature = "futures_api", issue = "50547")]
-impl<Sp> Spawn for Box<Sp>
- where Sp: Spawn + ?Sized
-{
- fn spawn_obj(
- &mut self,
- future: FutureObj<'static, ()>,
- ) -> Result<(), SpawnObjError> {
- (**self).spawn_obj(future)
- }
-
- fn status(&self) -> Result<(), SpawnErrorKind> {
- (**self).status()
- }
-}
-
-#[unstable(feature = "futures_api", issue = "50547")]
-impl<'a, F: Future<Output = ()> + Send + 'a> From<Box<F>> for FutureObj<'a, ()> {
- fn from(boxed: Box<F>) -> Self {
- FutureObj::new(boxed)
- }
-}
-
-#[unstable(feature = "futures_api", issue = "50547")]
-impl<'a, F: Future<Output = ()> + 'a> From<Box<F>> for LocalFutureObj<'a, ()> {
- fn from(boxed: Box<F>) -> Self {
- LocalFutureObj::new(boxed)
- }
-}
-
-#[unstable(feature = "pin", issue = "49150")]
-impl<T: Unpin + ?Sized> From<PinBox<T>> for Box<T> {
- fn from(pinned: PinBox<T>) -> Box<T> {
- unsafe { PinBox::unpin(pinned) }
+ fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<Self::Output> {
+ F::poll(Pin::new(&mut *self), lw)
}
}