Skip to content

Commit 13da951

Browse files
committed
move PinMut into pin module and export through std
1 parent f1b506a commit 13da951

File tree

14 files changed

+191
-156
lines changed

14 files changed

+191
-156
lines changed

src/liballoc/boxed.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ 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};

src/libcore/future/future.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
reason = "futures in libcore are unstable",
1313
issue = "50547")]
1414

15-
use mem::PinMut;
15+
use pin::PinMut;
1616
use marker::Unpin;
1717
use task::{self, Poll};
1818

src/libcore/future/future_obj.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use fmt;
1616
use future::Future;
1717
use marker::{PhantomData, Unpin};
18-
use mem::PinMut;
18+
use pin::PinMut;
1919
use task::{Context, Poll};
2020

2121
/// A custom trait object for polling futures, roughly akin to

src/libcore/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ pub mod cell;
191191
pub mod char;
192192
pub mod panic;
193193
pub mod panicking;
194+
pub mod pin;
194195
pub mod iter;
195196
pub mod option;
196197
pub mod raw;

src/libcore/mem.rs

Lines changed: 2 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
use clone;
1919
use cmp;
2020
use fmt;
21-
use future::{Future, UnsafeFutureObj};
2221
use hash;
2322
use intrinsics;
24-
use marker::{Copy, PhantomData, Sized, Unpin, Unsize};
23+
use marker::{Copy, PhantomData, Sized};
2524
use ptr;
26-
use task::{Context, Poll};
27-
use ops::{Deref, DerefMut, CoerceUnsized};
25+
use ops::{Deref, DerefMut};
2826

2927
#[stable(feature = "rust1", since = "1.0.0")]
3028
pub use intrinsics::transmute;
@@ -1024,146 +1022,3 @@ impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
10241022
&mut self.value
10251023
}
10261024
}
1027-
1028-
/// A pinned reference.
1029-
///
1030-
/// A pinned reference is a lot like a mutable reference, except that it is not
1031-
/// safe to move a value out of a pinned reference unless the type of that
1032-
/// value implements the `Unpin` trait.
1033-
#[unstable(feature = "pin", issue = "49150")]
1034-
#[fundamental]
1035-
pub struct PinMut<'a, T: ?Sized + 'a> {
1036-
inner: &'a mut T,
1037-
}
1038-
1039-
#[unstable(feature = "pin", issue = "49150")]
1040-
impl<'a, T: ?Sized + Unpin> PinMut<'a, T> {
1041-
/// Construct a new `PinMut` around a reference to some data of a type that
1042-
/// implements `Unpin`.
1043-
#[unstable(feature = "pin", issue = "49150")]
1044-
pub fn new(reference: &'a mut T) -> PinMut<'a, T> {
1045-
PinMut { inner: reference }
1046-
}
1047-
1048-
/// Get a mutable reference to the data inside of this `PinMut`.
1049-
#[unstable(feature = "pin", issue = "49150")]
1050-
pub fn get_mut(this: PinMut<'a, T>) -> &'a mut T {
1051-
this.inner
1052-
}
1053-
}
1054-
1055-
1056-
#[unstable(feature = "pin", issue = "49150")]
1057-
impl<'a, T: ?Sized> PinMut<'a, T> {
1058-
/// Construct a new `PinMut` around a reference to some data of a type that
1059-
/// may or may not implement `Unpin`.
1060-
///
1061-
/// This constructor is unsafe because we do not know what will happen with
1062-
/// that data after the reference ends. If you cannot guarantee that the
1063-
/// data will never move again, calling this constructor is invalid.
1064-
#[unstable(feature = "pin", issue = "49150")]
1065-
pub unsafe fn new_unchecked(reference: &'a mut T) -> PinMut<'a, T> {
1066-
PinMut { inner: reference }
1067-
}
1068-
1069-
/// Reborrow a `PinMut` for a shorter lifetime.
1070-
///
1071-
/// For example, `PinMut::get_mut(x.reborrow())` (unsafely) returns a
1072-
/// short-lived mutable reference reborrowing from `x`.
1073-
#[unstable(feature = "pin", issue = "49150")]
1074-
pub fn reborrow<'b>(&'b mut self) -> PinMut<'b, T> {
1075-
PinMut { inner: self.inner }
1076-
}
1077-
1078-
/// Get a mutable reference to the data inside of this `PinMut`.
1079-
///
1080-
/// This function is unsafe. You must guarantee that you will never move
1081-
/// the data out of the mutable reference you receive when you call this
1082-
/// function.
1083-
#[unstable(feature = "pin", issue = "49150")]
1084-
pub unsafe fn get_mut_unchecked(this: PinMut<'a, T>) -> &'a mut T {
1085-
this.inner
1086-
}
1087-
1088-
/// Construct a new pin by mapping the interior value.
1089-
///
1090-
/// For example, if you wanted to get a `PinMut` of a field of something,
1091-
/// you could use this to get access to that field in one line of code.
1092-
///
1093-
/// This function is unsafe. You must guarantee that the data you return
1094-
/// will not move so long as the argument value does not move (for example,
1095-
/// because it is one of the fields of that value), and also that you do
1096-
/// not move out of the argument you receive to the interior function.
1097-
#[unstable(feature = "pin", issue = "49150")]
1098-
pub unsafe fn map_unchecked<U, F>(this: PinMut<'a, T>, f: F) -> PinMut<'a, U> where
1099-
F: FnOnce(&mut T) -> &mut U
1100-
{
1101-
PinMut { inner: f(this.inner) }
1102-
}
1103-
1104-
/// Assign a new value to the memory behind the pinned reference.
1105-
#[unstable(feature = "pin", issue = "49150")]
1106-
pub fn set(this: PinMut<'a, T>, value: T)
1107-
where T: Sized,
1108-
{
1109-
*this.inner = value;
1110-
}
1111-
}
1112-
1113-
#[unstable(feature = "pin", issue = "49150")]
1114-
impl<'a, T: ?Sized> Deref for PinMut<'a, T> {
1115-
type Target = T;
1116-
1117-
fn deref(&self) -> &T {
1118-
&*self.inner
1119-
}
1120-
}
1121-
1122-
#[unstable(feature = "pin", issue = "49150")]
1123-
impl<'a, T: ?Sized + Unpin> DerefMut for PinMut<'a, T> {
1124-
fn deref_mut(&mut self) -> &mut T {
1125-
self.inner
1126-
}
1127-
}
1128-
1129-
#[unstable(feature = "pin", issue = "49150")]
1130-
impl<'a, T: fmt::Debug + ?Sized> fmt::Debug for PinMut<'a, T> {
1131-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1132-
fmt::Debug::fmt(&**self, f)
1133-
}
1134-
}
1135-
1136-
#[unstable(feature = "pin", issue = "49150")]
1137-
impl<'a, T: fmt::Display + ?Sized> fmt::Display for PinMut<'a, T> {
1138-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1139-
fmt::Display::fmt(&**self, f)
1140-
}
1141-
}
1142-
1143-
#[unstable(feature = "pin", issue = "49150")]
1144-
impl<'a, T: ?Sized> fmt::Pointer for PinMut<'a, T> {
1145-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1146-
fmt::Pointer::fmt(&(&*self.inner as *const T), f)
1147-
}
1148-
}
1149-
1150-
#[unstable(feature = "pin", issue = "49150")]
1151-
impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<PinMut<'a, U>> for PinMut<'a, T> {}
1152-
1153-
#[unstable(feature = "pin", issue = "49150")]
1154-
impl<'a, T: ?Sized> Unpin for PinMut<'a, T> {}
1155-
1156-
#[unstable(feature = "futures_api", issue = "50547")]
1157-
unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for PinMut<'a, F>
1158-
where F: Future<Output = T> + 'a
1159-
{
1160-
fn into_raw(self) -> *mut () {
1161-
unsafe { PinMut::get_mut_unchecked(self) as *mut F as *mut () }
1162-
}
1163-
1164-
unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll<T> {
1165-
PinMut::new_unchecked(&mut *(ptr as *mut F)).poll(cx)
1166-
}
1167-
1168-
unsafe fn drop(_ptr: *mut ()) {}
1169-
}

src/libcore/option.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147

148148
use iter::{FromIterator, FusedIterator, TrustedLen};
149149
use {hint, mem, ops::{self, Deref}};
150-
use mem::PinMut;
150+
use pin::PinMut;
151151

152152
// Note that this is not a lang item per se, but it has a hidden dependency on
153153
// `Iterator`, which is one. The compiler assumes that the `next` method of

src/libcore/pin.rs

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
// Copyright 2012-2014 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+
//! Types which pin data to its location in memory
12+
13+
#![unstable(feature = "pin", issue = "49150")]
14+
15+
use fmt;
16+
use future::{Future, UnsafeFutureObj};
17+
use marker::{Sized, Unpin, Unsize};
18+
use task::{Context, Poll};
19+
use ops::{Deref, DerefMut, CoerceUnsized};
20+
21+
/// A pinned reference.
22+
///
23+
/// A pinned reference is a lot like a mutable reference, except that it is not
24+
/// safe to move a value out of a pinned reference unless the type of that
25+
/// value implements the `Unpin` trait.
26+
#[unstable(feature = "pin", issue = "49150")]
27+
#[fundamental]
28+
pub struct PinMut<'a, T: ?Sized + 'a> {
29+
inner: &'a mut T,
30+
}
31+
32+
#[unstable(feature = "pin", issue = "49150")]
33+
impl<'a, T: ?Sized + Unpin> PinMut<'a, T> {
34+
/// Construct a new `PinMut` around a reference to some data of a type that
35+
/// implements `Unpin`.
36+
#[unstable(feature = "pin", issue = "49150")]
37+
pub fn new(reference: &'a mut T) -> PinMut<'a, T> {
38+
PinMut { inner: reference }
39+
}
40+
41+
/// Get a mutable reference to the data inside of this `PinMut`.
42+
#[unstable(feature = "pin", issue = "49150")]
43+
pub fn get_mut(this: PinMut<'a, T>) -> &'a mut T {
44+
this.inner
45+
}
46+
}
47+
48+
49+
#[unstable(feature = "pin", issue = "49150")]
50+
impl<'a, T: ?Sized> PinMut<'a, T> {
51+
/// Construct a new `PinMut` around a reference to some data of a type that
52+
/// may or may not implement `Unpin`.
53+
///
54+
/// This constructor is unsafe because we do not know what will happen with
55+
/// that data after the reference ends. If you cannot guarantee that the
56+
/// data will never move again, calling this constructor is invalid.
57+
#[unstable(feature = "pin", issue = "49150")]
58+
pub unsafe fn new_unchecked(reference: &'a mut T) -> PinMut<'a, T> {
59+
PinMut { inner: reference }
60+
}
61+
62+
/// Reborrow a `PinMut` for a shorter lifetime.
63+
///
64+
/// For example, `PinMut::get_mut(x.reborrow())` (unsafely) returns a
65+
/// short-lived mutable reference reborrowing from `x`.
66+
#[unstable(feature = "pin", issue = "49150")]
67+
pub fn reborrow<'b>(&'b mut self) -> PinMut<'b, T> {
68+
PinMut { inner: self.inner }
69+
}
70+
71+
/// Get a mutable reference to the data inside of this `PinMut`.
72+
///
73+
/// This function is unsafe. You must guarantee that you will never move
74+
/// the data out of the mutable reference you receive when you call this
75+
/// function.
76+
#[unstable(feature = "pin", issue = "49150")]
77+
pub unsafe fn get_mut_unchecked(this: PinMut<'a, T>) -> &'a mut T {
78+
this.inner
79+
}
80+
81+
/// Construct a new pin by mapping the interior value.
82+
///
83+
/// For example, if you wanted to get a `PinMut` of a field of something,
84+
/// you could use this to get access to that field in one line of code.
85+
///
86+
/// This function is unsafe. You must guarantee that the data you return
87+
/// will not move so long as the argument value does not move (for example,
88+
/// because it is one of the fields of that value), and also that you do
89+
/// not move out of the argument you receive to the interior function.
90+
#[unstable(feature = "pin", issue = "49150")]
91+
pub unsafe fn map_unchecked<U, F>(this: PinMut<'a, T>, f: F) -> PinMut<'a, U> where
92+
F: FnOnce(&mut T) -> &mut U
93+
{
94+
PinMut { inner: f(this.inner) }
95+
}
96+
97+
/// Assign a new value to the memory behind the pinned reference.
98+
#[unstable(feature = "pin", issue = "49150")]
99+
pub fn set(this: PinMut<'a, T>, value: T)
100+
where T: Sized,
101+
{
102+
*this.inner = value;
103+
}
104+
}
105+
106+
#[unstable(feature = "pin", issue = "49150")]
107+
impl<'a, T: ?Sized> Deref for PinMut<'a, T> {
108+
type Target = T;
109+
110+
fn deref(&self) -> &T {
111+
&*self.inner
112+
}
113+
}
114+
115+
#[unstable(feature = "pin", issue = "49150")]
116+
impl<'a, T: ?Sized + Unpin> DerefMut for PinMut<'a, T> {
117+
fn deref_mut(&mut self) -> &mut T {
118+
self.inner
119+
}
120+
}
121+
122+
#[unstable(feature = "pin", issue = "49150")]
123+
impl<'a, T: fmt::Debug + ?Sized> fmt::Debug for PinMut<'a, T> {
124+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125+
fmt::Debug::fmt(&**self, f)
126+
}
127+
}
128+
129+
#[unstable(feature = "pin", issue = "49150")]
130+
impl<'a, T: fmt::Display + ?Sized> fmt::Display for PinMut<'a, T> {
131+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
132+
fmt::Display::fmt(&**self, f)
133+
}
134+
}
135+
136+
#[unstable(feature = "pin", issue = "49150")]
137+
impl<'a, T: ?Sized> fmt::Pointer for PinMut<'a, T> {
138+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
139+
fmt::Pointer::fmt(&(&*self.inner as *const T), f)
140+
}
141+
}
142+
143+
#[unstable(feature = "pin", issue = "49150")]
144+
impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<PinMut<'a, U>> for PinMut<'a, T> {}
145+
146+
#[unstable(feature = "pin", issue = "49150")]
147+
impl<'a, T: ?Sized> Unpin for PinMut<'a, T> {}
148+
149+
#[unstable(feature = "futures_api", issue = "50547")]
150+
unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for PinMut<'a, F>
151+
where F: Future<Output = T> + 'a
152+
{
153+
fn into_raw(self) -> *mut () {
154+
unsafe { PinMut::get_mut_unchecked(self) as *mut F as *mut () }
155+
}
156+
157+
unsafe fn poll(ptr: *mut (), cx: &mut Context) -> Poll<T> {
158+
PinMut::new_unchecked(&mut *(ptr as *mut F)).poll(cx)
159+
}
160+
161+
unsafe fn drop(_ptr: *mut ()) {}
162+
}

src/libstd/future.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
use core::cell::Cell;
1414
use core::marker::Unpin;
15-
use core::mem::PinMut;
15+
use core::pin::PinMut;
1616
use core::option::Option;
1717
use core::ptr::NonNull;
1818
use core::task::{self, Poll};

src/libstd/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ pub mod num;
466466
pub mod os;
467467
pub mod panic;
468468
pub mod path;
469+
pub mod pin;
469470
pub mod process;
470471
pub mod sync;
471472
pub mod time;

src/libstd/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ macro_rules! await {
230230
loop {
231231
if let $crate::task::Poll::Ready(x) =
232232
$crate::future::poll_in_task_cx(unsafe {
233-
$crate::mem::PinMut::new_unchecked(&mut pinned)
233+
$crate::pin::PinMut::new_unchecked(&mut pinned)
234234
})
235235
{
236236
break x;

0 commit comments

Comments
 (0)