@@ -20,9 +20,9 @@ use cmp;
20
20
use fmt;
21
21
use hash;
22
22
use intrinsics;
23
- use marker:: { Copy , PhantomData , Sized } ;
23
+ use marker:: { Copy , PhantomData , Sized , Unpin , Unsize } ;
24
24
use ptr;
25
- use ops:: { Deref , DerefMut } ;
25
+ use ops:: { Deref , DerefMut , CoerceUnsized } ;
26
26
27
27
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
28
28
pub use intrinsics:: transmute;
@@ -1105,3 +1105,111 @@ impl<T: ::hash::Hash> ::hash::Hash for ManuallyDrop<T> {
1105
1105
pub unsafe fn unreachable ( ) -> ! {
1106
1106
intrinsics:: unreachable ( )
1107
1107
}
1108
+
1109
+ /// A pinned reference.
1110
+ ///
1111
+ /// A pinned reference is a lot like a mutable reference, except that it is not
1112
+ /// safe to move a value out of a pinned reference unless the type of that
1113
+ /// value implements the `Unpin` trait.
1114
+ #[ unstable( feature = "pin" , issue = "49150" ) ]
1115
+ #[ fundamental]
1116
+ pub struct Pin < ' a , T : ?Sized + ' a > {
1117
+ inner : & ' a mut T ,
1118
+ }
1119
+
1120
+ #[ unstable( feature = "pin" , issue = "49150" ) ]
1121
+ impl < ' a , T : ?Sized + Unpin > Pin < ' a , T > {
1122
+ /// Construct a new `Pin` around a reference to some data of a type that
1123
+ /// implements `Unpin`.
1124
+ #[ unstable( feature = "pin" , issue = "49150" ) ]
1125
+ pub fn new ( reference : & ' a mut T ) -> Pin < ' a , T > {
1126
+ Pin { inner : reference }
1127
+ }
1128
+ }
1129
+
1130
+
1131
+ #[ unstable( feature = "pin" , issue = "49150" ) ]
1132
+ impl < ' a , T : ?Sized > Pin < ' a , T > {
1133
+ /// Construct a new `Pin` around a reference to some data of a type that
1134
+ /// may or may not implement `Unpin`.
1135
+ ///
1136
+ /// This constructor is unsafe because we do not know what will happen with
1137
+ /// that data after the reference ends. If you cannot guarantee that the
1138
+ /// data will never move again, calling this constructor is invalid.
1139
+ #[ unstable( feature = "pin" , issue = "49150" ) ]
1140
+ pub unsafe fn new_unchecked ( reference : & ' a mut T ) -> Pin < ' a , T > {
1141
+ Pin { inner : reference }
1142
+ }
1143
+
1144
+ /// Borrow a Pin for a shorter lifetime than it already has.
1145
+ #[ unstable( feature = "pin" , issue = "49150" ) ]
1146
+ pub fn borrow < ' b > ( this : & ' b mut Pin < ' a , T > ) -> Pin < ' b , T > {
1147
+ Pin { inner : this. inner }
1148
+ }
1149
+
1150
+ /// Get a mutable reference to the data inside of this `Pin`.
1151
+ ///
1152
+ /// This function is unsafe. You must guarantee that you will never move
1153
+ /// the data out of the mutable reference you receive when you call this
1154
+ /// function.
1155
+ #[ unstable( feature = "pin" , issue = "49150" ) ]
1156
+ pub unsafe fn get_mut < ' b > ( this : & ' b mut Pin < ' a , T > ) -> & ' b mut T {
1157
+ this. inner
1158
+ }
1159
+
1160
+ /// Construct a new pin by mapping the interior value.
1161
+ ///
1162
+ /// For example, if you wanted to get a `Pin` of a field of something, you
1163
+ /// could use this to get access to that field in one line of code.
1164
+ ///
1165
+ /// This function is unsafe. You must guarantee that the data you return
1166
+ /// will not move so long as the argument value does not move (for example,
1167
+ /// because it is one of the fields of that value), and also that you do
1168
+ /// not move out of the argument you receive to the interior function.
1169
+ #[ unstable( feature = "pin" , issue = "49150" ) ]
1170
+ pub unsafe fn map < ' b , U , F > ( this : & ' b mut Pin < ' a , T > , f : F ) -> Pin < ' b , U > where
1171
+ F : FnOnce ( & mut T ) -> & mut U
1172
+ {
1173
+ Pin { inner : f ( this. inner ) }
1174
+ }
1175
+ }
1176
+
1177
+ #[ unstable( feature = "pin" , issue = "49150" ) ]
1178
+ impl < ' a , T : ?Sized > Deref for Pin < ' a , T > {
1179
+ type Target = T ;
1180
+
1181
+ fn deref ( & self ) -> & T {
1182
+ & * self . inner
1183
+ }
1184
+ }
1185
+
1186
+ #[ unstable( feature = "pin" , issue = "49150" ) ]
1187
+ impl < ' a , T : ?Sized + Unpin > DerefMut for Pin < ' a , T > {
1188
+ fn deref_mut ( & mut self ) -> & mut T {
1189
+ self . inner
1190
+ }
1191
+ }
1192
+
1193
+ #[ unstable( feature = "pin" , issue = "49150" ) ]
1194
+ impl < ' a , T : fmt:: Debug + ?Sized > fmt:: Debug for Pin < ' a , T > {
1195
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
1196
+ fmt:: Debug :: fmt ( & * * self , f)
1197
+ }
1198
+ }
1199
+
1200
+ #[ unstable( feature = "pin" , issue = "49150" ) ]
1201
+ impl < ' a , T : fmt:: Display + ?Sized > fmt:: Display for Pin < ' a , T > {
1202
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
1203
+ fmt:: Display :: fmt ( & * * self , f)
1204
+ }
1205
+ }
1206
+
1207
+ #[ unstable( feature = "pin" , issue = "49150" ) ]
1208
+ impl < ' a , T : ?Sized > fmt:: Pointer for Pin < ' a , T > {
1209
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
1210
+ fmt:: Pointer :: fmt ( & ( & * self . inner as * const T ) , f)
1211
+ }
1212
+ }
1213
+
1214
+ #[ unstable( feature = "pin" , issue = "49150" ) ]
1215
+ impl < ' a , T : ?Sized + Unsize < U > , U : ?Sized > CoerceUnsized < Pin < ' a , U > > for Pin < ' a , T > { }
0 commit comments