Skip to content

Commit c166b03

Browse files
committed
Auto merge of #50497 - RalfJung:pinmut, r=withoutboats
Rename Pin to PinMut, and some more breaking changes As discussed at [1] §3 and [2] and [3], a formal look at pinning requires considering a distinguished "shared pinned" mode/typestate. Given that, it seems desirable to at least eventually actually expose that typestate as a reference type. This renames Pin to PinMut, freeing the name Pin in case we want to use it for a shared pinned reference later on. [1] https://www.ralfj.de/blog/2018/04/10/safe-intrusive-collections-with-pinning.html [2] rust-lang/rfcs#2349 (comment) [3] #49150 (comment) Cc @withoutboats
2 parents b183bd0 + 939c25a commit c166b03

File tree

3 files changed

+34
-31
lines changed

3 files changed

+34
-31
lines changed

src/liballoc/boxed.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use core::fmt;
6262
use core::hash::{Hash, Hasher};
6363
use core::iter::FusedIterator;
6464
use core::marker::{Unpin, Unsize};
65-
use core::mem::{self, Pin};
65+
use core::mem::{self, PinMut};
6666
use core::ops::{CoerceUnsized, Deref, DerefMut, Generator, GeneratorState};
6767
use core::ptr::{self, NonNull, Unique};
6868
use core::convert::From;
@@ -771,8 +771,8 @@ impl<T> PinBox<T> {
771771
#[unstable(feature = "pin", issue = "49150")]
772772
impl<T: ?Sized> PinBox<T> {
773773
/// Get a pinned reference to the data in this PinBox.
774-
pub fn as_pin<'a>(&'a mut self) -> Pin<'a, T> {
775-
unsafe { Pin::new_unchecked(&mut *self.inner) }
774+
pub fn as_pin_mut<'a>(&'a mut self) -> PinMut<'a, T> {
775+
unsafe { PinMut::new_unchecked(&mut *self.inner) }
776776
}
777777

778778
/// Get a mutable reference to the data inside this PinBox.

src/libcore/marker.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -595,15 +595,15 @@ unsafe impl<T: ?Sized> Freeze for *mut T {}
595595
unsafe impl<'a, T: ?Sized> Freeze for &'a T {}
596596
unsafe impl<'a, T: ?Sized> Freeze for &'a mut T {}
597597

598-
/// Types which can be moved out of a `Pin`.
598+
/// Types which can be moved out of a `PinMut`.
599599
///
600-
/// The `Unpin` trait is used to control the behavior of the [`Pin`] type. If a
600+
/// The `Unpin` trait is used to control the behavior of the [`PinMut`] type. If a
601601
/// type implements `Unpin`, it is safe to move a value of that type out of the
602-
/// `Pin` pointer.
602+
/// `PinMut` pointer.
603603
///
604604
/// This trait is automatically implemented for almost every type.
605605
///
606-
/// [`Pin`]: ../mem/struct.Pin.html
606+
/// [`PinMut`]: ../mem/struct.PinMut.html
607607
#[unstable(feature = "pin", issue = "49150")]
608608
pub unsafe auto trait Unpin {}
609609

src/libcore/mem.rs

+27-24
Original file line numberDiff line numberDiff line change
@@ -1101,69 +1101,72 @@ impl<T: ::hash::Hash> ::hash::Hash for ManuallyDrop<T> {
11011101
/// value implements the `Unpin` trait.
11021102
#[unstable(feature = "pin", issue = "49150")]
11031103
#[fundamental]
1104-
pub struct Pin<'a, T: ?Sized + 'a> {
1104+
pub struct PinMut<'a, T: ?Sized + 'a> {
11051105
inner: &'a mut T,
11061106
}
11071107

11081108
#[unstable(feature = "pin", issue = "49150")]
1109-
impl<'a, T: ?Sized + Unpin> Pin<'a, T> {
1110-
/// Construct a new `Pin` around a reference to some data of a type that
1109+
impl<'a, T: ?Sized + Unpin> PinMut<'a, T> {
1110+
/// Construct a new `PinMut` around a reference to some data of a type that
11111111
/// implements `Unpin`.
11121112
#[unstable(feature = "pin", issue = "49150")]
1113-
pub fn new(reference: &'a mut T) -> Pin<'a, T> {
1114-
Pin { inner: reference }
1113+
pub fn new(reference: &'a mut T) -> PinMut<'a, T> {
1114+
PinMut { inner: reference }
11151115
}
11161116
}
11171117

11181118

11191119
#[unstable(feature = "pin", issue = "49150")]
1120-
impl<'a, T: ?Sized> Pin<'a, T> {
1121-
/// Construct a new `Pin` around a reference to some data of a type that
1120+
impl<'a, T: ?Sized> PinMut<'a, T> {
1121+
/// Construct a new `PinMut` around a reference to some data of a type that
11221122
/// may or may not implement `Unpin`.
11231123
///
11241124
/// This constructor is unsafe because we do not know what will happen with
11251125
/// that data after the reference ends. If you cannot guarantee that the
11261126
/// data will never move again, calling this constructor is invalid.
11271127
#[unstable(feature = "pin", issue = "49150")]
1128-
pub unsafe fn new_unchecked(reference: &'a mut T) -> Pin<'a, T> {
1129-
Pin { inner: reference }
1128+
pub unsafe fn new_unchecked(reference: &'a mut T) -> PinMut<'a, T> {
1129+
PinMut { inner: reference }
11301130
}
11311131

1132-
/// Borrow a Pin for a shorter lifetime than it already has.
1132+
/// Reborrow a `PinMut` for a shorter lifetime.
1133+
///
1134+
/// For example, `PinMut::get_mut(x.reborrow())` (unsafely) returns a
1135+
/// short-lived mutable reference reborrowing from `x`.
11331136
#[unstable(feature = "pin", issue = "49150")]
1134-
pub fn borrow<'b>(this: &'b mut Pin<'a, T>) -> Pin<'b, T> {
1135-
Pin { inner: this.inner }
1137+
pub fn reborrow<'b>(&'b mut self) -> PinMut<'b, T> {
1138+
PinMut { inner: self.inner }
11361139
}
11371140

1138-
/// Get a mutable reference to the data inside of this `Pin`.
1141+
/// Get a mutable reference to the data inside of this `PinMut`.
11391142
///
11401143
/// This function is unsafe. You must guarantee that you will never move
11411144
/// the data out of the mutable reference you receive when you call this
11421145
/// function.
11431146
#[unstable(feature = "pin", issue = "49150")]
1144-
pub unsafe fn get_mut<'b>(this: &'b mut Pin<'a, T>) -> &'b mut T {
1147+
pub unsafe fn get_mut(this: PinMut<'a, T>) -> &'a mut T {
11451148
this.inner
11461149
}
11471150

11481151
/// Construct a new pin by mapping the interior value.
11491152
///
1150-
/// For example, if you wanted to get a `Pin` of a field of something, you
1153+
/// For example, if you wanted to get a `PinMut` of a field of something, you
11511154
/// could use this to get access to that field in one line of code.
11521155
///
11531156
/// This function is unsafe. You must guarantee that the data you return
11541157
/// will not move so long as the argument value does not move (for example,
11551158
/// because it is one of the fields of that value), and also that you do
11561159
/// not move out of the argument you receive to the interior function.
11571160
#[unstable(feature = "pin", issue = "49150")]
1158-
pub unsafe fn map<'b, U, F>(this: &'b mut Pin<'a, T>, f: F) -> Pin<'b, U> where
1161+
pub unsafe fn map<U, F>(this: PinMut<'a, T>, f: F) -> PinMut<'a, U> where
11591162
F: FnOnce(&mut T) -> &mut U
11601163
{
1161-
Pin { inner: f(this.inner) }
1164+
PinMut { inner: f(this.inner) }
11621165
}
11631166
}
11641167

11651168
#[unstable(feature = "pin", issue = "49150")]
1166-
impl<'a, T: ?Sized> Deref for Pin<'a, T> {
1169+
impl<'a, T: ?Sized> Deref for PinMut<'a, T> {
11671170
type Target = T;
11681171

11691172
fn deref(&self) -> &T {
@@ -1172,35 +1175,35 @@ impl<'a, T: ?Sized> Deref for Pin<'a, T> {
11721175
}
11731176

11741177
#[unstable(feature = "pin", issue = "49150")]
1175-
impl<'a, T: ?Sized + Unpin> DerefMut for Pin<'a, T> {
1178+
impl<'a, T: ?Sized + Unpin> DerefMut for PinMut<'a, T> {
11761179
fn deref_mut(&mut self) -> &mut T {
11771180
self.inner
11781181
}
11791182
}
11801183

11811184
#[unstable(feature = "pin", issue = "49150")]
1182-
impl<'a, T: fmt::Debug + ?Sized> fmt::Debug for Pin<'a, T> {
1185+
impl<'a, T: fmt::Debug + ?Sized> fmt::Debug for PinMut<'a, T> {
11831186
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
11841187
fmt::Debug::fmt(&**self, f)
11851188
}
11861189
}
11871190

11881191
#[unstable(feature = "pin", issue = "49150")]
1189-
impl<'a, T: fmt::Display + ?Sized> fmt::Display for Pin<'a, T> {
1192+
impl<'a, T: fmt::Display + ?Sized> fmt::Display for PinMut<'a, T> {
11901193
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
11911194
fmt::Display::fmt(&**self, f)
11921195
}
11931196
}
11941197

11951198
#[unstable(feature = "pin", issue = "49150")]
1196-
impl<'a, T: ?Sized> fmt::Pointer for Pin<'a, T> {
1199+
impl<'a, T: ?Sized> fmt::Pointer for PinMut<'a, T> {
11971200
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
11981201
fmt::Pointer::fmt(&(&*self.inner as *const T), f)
11991202
}
12001203
}
12011204

12021205
#[unstable(feature = "pin", issue = "49150")]
1203-
impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Pin<'a, U>> for Pin<'a, T> {}
1206+
impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<PinMut<'a, U>> for PinMut<'a, T> {}
12041207

12051208
#[unstable(feature = "pin", issue = "49150")]
1206-
unsafe impl<'a, T: ?Sized> Unpin for Pin<'a, T> {}
1209+
unsafe impl<'a, T: ?Sized> Unpin for PinMut<'a, T> {}

0 commit comments

Comments
 (0)