Skip to content

Commit 8c2b371

Browse files
committed
Auto merge of #53227 - nivkner:pin_move, r=RalfJung
move the Pin API into its own module for centralized documentation This implements the change proposed by @withoutboats in #49150, as suggested by @RalfJung in the review of #53104, along with the documentation that was originally in it, that was deemed more appropriate in module-level documentation. r? @RalfJung
2 parents f7202e4 + 83ca347 commit 8c2b371

File tree

18 files changed

+496
-365
lines changed

18 files changed

+496
-365
lines changed

src/liballoc/boxed.rs

+9-202
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ use core::future::{Future, FutureObj, LocalFutureObj, UnsafeFutureObj};
6464
use core::hash::{Hash, Hasher};
6565
use core::iter::FusedIterator;
6666
use core::marker::{Unpin, Unsize};
67-
use core::mem::{self, PinMut};
67+
use core::mem;
68+
use core::pin::PinMut;
6869
use core::ops::{CoerceUnsized, Deref, DerefMut, Generator, GeneratorState};
6970
use core::ptr::{self, NonNull, Unique};
7071
use core::task::{Context, Poll, Spawn, SpawnErrorKind, SpawnObjError};
7172

7273
use raw_vec::RawVec;
74+
use pin::PinBox;
7375
use str::from_boxed_utf8_unchecked;
7476

7577
/// A pointer type for heap allocation.
@@ -758,166 +760,6 @@ impl<T> Generator for Box<T>
758760
}
759761
}
760762

761-
/// A pinned, heap allocated reference.
762-
#[unstable(feature = "pin", issue = "49150")]
763-
#[fundamental]
764-
#[repr(transparent)]
765-
pub struct PinBox<T: ?Sized> {
766-
inner: Box<T>,
767-
}
768-
769-
#[unstable(feature = "pin", issue = "49150")]
770-
impl<T> PinBox<T> {
771-
/// Allocate memory on the heap, move the data into it and pin it.
772-
#[unstable(feature = "pin", issue = "49150")]
773-
pub fn new(data: T) -> PinBox<T> {
774-
PinBox { inner: Box::new(data) }
775-
}
776-
}
777-
778-
#[unstable(feature = "pin", issue = "49150")]
779-
impl<T: ?Sized> PinBox<T> {
780-
/// Get a pinned reference to the data in this PinBox.
781-
#[inline]
782-
pub fn as_pin_mut<'a>(&'a mut self) -> PinMut<'a, T> {
783-
unsafe { PinMut::new_unchecked(&mut *self.inner) }
784-
}
785-
786-
/// Constructs a `PinBox` from a raw pointer.
787-
///
788-
/// After calling this function, the raw pointer is owned by the
789-
/// resulting `PinBox`. Specifically, the `PinBox` destructor will call
790-
/// the destructor of `T` and free the allocated memory. Since the
791-
/// way `PinBox` allocates and releases memory is unspecified, the
792-
/// only valid pointer to pass to this function is the one taken
793-
/// from another `PinBox` via the [`PinBox::into_raw`] function.
794-
///
795-
/// This function is unsafe because improper use may lead to
796-
/// memory problems. For example, a double-free may occur if the
797-
/// function is called twice on the same raw pointer.
798-
///
799-
/// [`PinBox::into_raw`]: struct.PinBox.html#method.into_raw
800-
///
801-
/// # Examples
802-
///
803-
/// ```
804-
/// #![feature(pin)]
805-
/// use std::boxed::PinBox;
806-
/// let x = PinBox::new(5);
807-
/// let ptr = PinBox::into_raw(x);
808-
/// let x = unsafe { PinBox::from_raw(ptr) };
809-
/// ```
810-
#[inline]
811-
pub unsafe fn from_raw(raw: *mut T) -> Self {
812-
PinBox { inner: Box::from_raw(raw) }
813-
}
814-
815-
/// Consumes the `PinBox`, returning the wrapped raw pointer.
816-
///
817-
/// After calling this function, the caller is responsible for the
818-
/// memory previously managed by the `PinBox`. In particular, the
819-
/// caller should properly destroy `T` and release the memory. The
820-
/// proper way to do so is to convert the raw pointer back into a
821-
/// `PinBox` with the [`PinBox::from_raw`] function.
822-
///
823-
/// Note: this is an associated function, which means that you have
824-
/// to call it as `PinBox::into_raw(b)` instead of `b.into_raw()`. This
825-
/// is so that there is no conflict with a method on the inner type.
826-
///
827-
/// [`PinBox::from_raw`]: struct.PinBox.html#method.from_raw
828-
///
829-
/// # Examples
830-
///
831-
/// ```
832-
/// #![feature(pin)]
833-
/// use std::boxed::PinBox;
834-
/// let x = PinBox::new(5);
835-
/// let ptr = PinBox::into_raw(x);
836-
/// ```
837-
#[inline]
838-
pub fn into_raw(b: PinBox<T>) -> *mut T {
839-
Box::into_raw(b.inner)
840-
}
841-
842-
/// Get a mutable reference to the data inside this PinBox.
843-
///
844-
/// This function is unsafe. Users must guarantee that the data is never
845-
/// moved out of this reference.
846-
#[inline]
847-
pub unsafe fn get_mut<'a>(this: &'a mut PinBox<T>) -> &'a mut T {
848-
&mut *this.inner
849-
}
850-
851-
/// Convert this PinBox into an unpinned Box.
852-
///
853-
/// This function is unsafe. Users must guarantee that the data is never
854-
/// moved out of the box.
855-
#[inline]
856-
pub unsafe fn unpin(this: PinBox<T>) -> Box<T> {
857-
this.inner
858-
}
859-
}
860-
861-
#[unstable(feature = "pin", issue = "49150")]
862-
impl<T: ?Sized> From<Box<T>> for PinBox<T> {
863-
fn from(boxed: Box<T>) -> PinBox<T> {
864-
PinBox { inner: boxed }
865-
}
866-
}
867-
868-
#[unstable(feature = "pin", issue = "49150")]
869-
impl<T: Unpin + ?Sized> From<PinBox<T>> for Box<T> {
870-
fn from(pinned: PinBox<T>) -> Box<T> {
871-
pinned.inner
872-
}
873-
}
874-
875-
#[unstable(feature = "pin", issue = "49150")]
876-
impl<T: ?Sized> Deref for PinBox<T> {
877-
type Target = T;
878-
879-
fn deref(&self) -> &T {
880-
&*self.inner
881-
}
882-
}
883-
884-
#[unstable(feature = "pin", issue = "49150")]
885-
impl<T: Unpin + ?Sized> DerefMut for PinBox<T> {
886-
fn deref_mut(&mut self) -> &mut T {
887-
&mut *self.inner
888-
}
889-
}
890-
891-
#[unstable(feature = "pin", issue = "49150")]
892-
impl<T: fmt::Display + ?Sized> fmt::Display for PinBox<T> {
893-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
894-
fmt::Display::fmt(&*self.inner, f)
895-
}
896-
}
897-
898-
#[unstable(feature = "pin", issue = "49150")]
899-
impl<T: fmt::Debug + ?Sized> fmt::Debug for PinBox<T> {
900-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
901-
fmt::Debug::fmt(&*self.inner, f)
902-
}
903-
}
904-
905-
#[unstable(feature = "pin", issue = "49150")]
906-
impl<T: ?Sized> fmt::Pointer for PinBox<T> {
907-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
908-
// It's not possible to extract the inner Uniq directly from the Box,
909-
// instead we cast it to a *const which aliases the Unique
910-
let ptr: *const T = &*self.inner;
911-
fmt::Pointer::fmt(&ptr, f)
912-
}
913-
}
914-
915-
#[unstable(feature = "pin", issue = "49150")]
916-
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<PinBox<U>> for PinBox<T> {}
917-
918-
#[unstable(feature = "pin", issue = "49150")]
919-
impl<T: ?Sized> Unpin for PinBox<T> {}
920-
921763
#[unstable(feature = "futures_api", issue = "50547")]
922764
impl<F: ?Sized + Future + Unpin> Future for Box<F> {
923765
type Output = F::Output;
@@ -927,15 +769,6 @@ impl<F: ?Sized + Future + Unpin> Future for Box<F> {
927769
}
928770
}
929771

930-
#[unstable(feature = "futures_api", issue = "50547")]
931-
impl<F: ?Sized + Future> Future for PinBox<F> {
932-
type Output = F::Output;
933-
934-
fn poll(mut self: PinMut<Self>, cx: &mut Context) -> Poll<Self::Output> {
935-
self.as_pin_mut().poll(cx)
936-
}
937-
}
938-
939772
#[unstable(feature = "futures_api", issue = "50547")]
940773
unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for Box<F>
941774
where F: Future<Output = T> + 'a
@@ -955,25 +788,6 @@ unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for Box<F>
955788
}
956789
}
957790

958-
#[unstable(feature = "futures_api", issue = "50547")]
959-
unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for PinBox<F>
960-
where F: Future<Output = T> + 'a
961-
{
962-
fn into_raw(self) -> *mut () {
963-
PinBox::into_raw(self) as *mut ()
964-
}
965-
966-
unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll<T> {
967-
let ptr = ptr as *mut F;
968-
let pin: PinMut<F> = PinMut::new_unchecked(&mut *ptr);
969-
pin.poll(cx)
970-
}
971-
972-
unsafe fn drop(ptr: *mut ()) {
973-
drop(PinBox::from_raw(ptr as *mut F))
974-
}
975-
}
976-
977791
#[unstable(feature = "futures_api", issue = "50547")]
978792
impl<Sp> Spawn for Box<Sp>
979793
where Sp: Spawn + ?Sized
@@ -990,13 +804,6 @@ impl<Sp> Spawn for Box<Sp>
990804
}
991805
}
992806

993-
#[unstable(feature = "futures_api", issue = "50547")]
994-
impl<'a, F: Future<Output = ()> + Send + 'a> From<PinBox<F>> for FutureObj<'a, ()> {
995-
fn from(boxed: PinBox<F>) -> Self {
996-
FutureObj::new(boxed)
997-
}
998-
}
999-
1000807
#[unstable(feature = "futures_api", issue = "50547")]
1001808
impl<'a, F: Future<Output = ()> + Send + 'a> From<Box<F>> for FutureObj<'a, ()> {
1002809
fn from(boxed: Box<F>) -> Self {
@@ -1005,15 +812,15 @@ impl<'a, F: Future<Output = ()> + Send + 'a> From<Box<F>> for FutureObj<'a, ()>
1005812
}
1006813

1007814
#[unstable(feature = "futures_api", issue = "50547")]
1008-
impl<'a, F: Future<Output = ()> + 'a> From<PinBox<F>> for LocalFutureObj<'a, ()> {
1009-
fn from(boxed: PinBox<F>) -> Self {
815+
impl<'a, F: Future<Output = ()> + 'a> From<Box<F>> for LocalFutureObj<'a, ()> {
816+
fn from(boxed: Box<F>) -> Self {
1010817
LocalFutureObj::new(boxed)
1011818
}
1012819
}
1013820

1014-
#[unstable(feature = "futures_api", issue = "50547")]
1015-
impl<'a, F: Future<Output = ()> + 'a> From<Box<F>> for LocalFutureObj<'a, ()> {
1016-
fn from(boxed: Box<F>) -> Self {
1017-
LocalFutureObj::new(boxed)
821+
#[unstable(feature = "pin", issue = "49150")]
822+
impl<T: Unpin + ?Sized> From<PinBox<T>> for Box<T> {
823+
fn from(pinned: PinBox<T>) -> Box<T> {
824+
unsafe { PinBox::unpin(pinned) }
1018825
}
1019826
}

src/liballoc/lib.rs

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

0 commit comments

Comments
 (0)