Skip to content

Commit 1e21c9a

Browse files
committed
Auto merge of #53877 - withoutboats:compositional-pin, r=aturon
Update to a new pinning API. ~~Blocked on #53843 because of method resolution problems with new pin type.~~ @r? @cramertj cc @RalfJung @pythonesque anyone interested in #49150
2 parents ff6422d + 574bca7 commit 1e21c9a

File tree

18 files changed

+371
-459
lines changed

18 files changed

+371
-459
lines changed

src/liballoc/boxed.rs

+32-10
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,12 @@ use core::hash::{Hash, Hasher};
6565
use core::iter::FusedIterator;
6666
use core::marker::{Unpin, Unsize};
6767
use core::mem;
68-
use core::pin::PinMut;
68+
use core::pin::Pin;
6969
use core::ops::{CoerceUnsized, Deref, DerefMut, Generator, GeneratorState};
7070
use core::ptr::{self, NonNull, Unique};
7171
use core::task::{Context, Poll, Spawn, SpawnErrorKind, SpawnObjError};
7272

7373
use raw_vec::RawVec;
74-
use pin::PinBox;
7574
use str::from_boxed_utf8_unchecked;
7675

7776
/// A pointer type for heap allocation.
@@ -97,6 +96,12 @@ impl<T> Box<T> {
9796
pub fn new(x: T) -> Box<T> {
9897
box x
9998
}
99+
100+
#[unstable(feature = "pin", issue = "49150")]
101+
#[inline(always)]
102+
pub fn pinned(x: T) -> Pin<Box<T>> {
103+
(box x).into()
104+
}
100105
}
101106

102107
impl<T: ?Sized> Box<T> {
@@ -427,6 +432,16 @@ impl<T> From<T> for Box<T> {
427432
}
428433
}
429434

435+
#[unstable(feature = "pin", issue = "49150")]
436+
impl<T> From<Box<T>> for Pin<Box<T>> {
437+
fn from(boxed: Box<T>) -> Self {
438+
// It's not possible to move or replace the insides of a `Pin<Box<T>>`
439+
// when `T: !Unpin`, so it's safe to pin it directly without any
440+
// additional requirements.
441+
unsafe { Pin::new_unchecked(boxed) }
442+
}
443+
}
444+
430445
#[stable(feature = "box_from_slice", since = "1.17.0")]
431446
impl<'a, T: Copy> From<&'a [T]> for Box<[T]> {
432447
fn from(slice: &'a [T]) -> Box<[T]> {
@@ -789,8 +804,8 @@ impl<T> Generator for Box<T>
789804
impl<F: ?Sized + Future + Unpin> Future for Box<F> {
790805
type Output = F::Output;
791806

792-
fn poll(mut self: PinMut<Self>, cx: &mut Context) -> Poll<Self::Output> {
793-
PinMut::new(&mut **self).poll(cx)
807+
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
808+
F::poll(Pin::new(&mut *self), cx)
794809
}
795810
}
796811

@@ -804,8 +819,8 @@ unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for Box<F>
804819

805820
unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll<T> {
806821
let ptr = ptr as *mut F;
807-
let pin: PinMut<F> = PinMut::new_unchecked(&mut *ptr);
808-
pin.poll(cx)
822+
let pin: Pin<&mut F> = Pin::new_unchecked(&mut *ptr);
823+
F::poll(pin, cx)
809824
}
810825

811826
unsafe fn drop(ptr: *mut ()) {
@@ -843,9 +858,16 @@ impl<'a, F: Future<Output = ()> + 'a> From<Box<F>> for LocalFutureObj<'a, ()> {
843858
}
844859
}
845860

846-
#[unstable(feature = "pin", issue = "49150")]
847-
impl<T: Unpin + ?Sized> From<PinBox<T>> for Box<T> {
848-
fn from(pinned: PinBox<T>) -> Box<T> {
849-
unsafe { PinBox::unpin(pinned) }
861+
#[unstable(feature = "futures_api", issue = "50547")]
862+
impl<'a, F: Future<Output = ()> + Send + 'a> From<Pin<Box<F>>> for FutureObj<'a, ()> {
863+
fn from(boxed: Pin<Box<F>>) -> Self {
864+
FutureObj::new(boxed)
865+
}
866+
}
867+
868+
#[unstable(feature = "futures_api", issue = "50547")]
869+
impl<'a, F: Future<Output = ()> + 'a> From<Pin<Box<F>>> for LocalFutureObj<'a, ()> {
870+
fn from(boxed: Pin<Box<F>>) -> Self {
871+
LocalFutureObj::new(boxed)
850872
}
851873
}

src/liballoc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ pub mod collections;
160160
pub mod sync;
161161
pub mod rc;
162162
pub mod raw_vec;
163-
pub mod pin;
164163
pub mod prelude;
165164
pub mod borrow;
166165
pub mod fmt;

src/liballoc/pin.rs

-302
This file was deleted.

0 commit comments

Comments
 (0)