@@ -1092,24 +1092,15 @@ pub use self::unsafe_pinned::UnsafePinned;
1092
1092
#[ rustc_pub_transparent]
1093
1093
#[ derive( Copy , Clone ) ]
1094
1094
pub struct Pin < Ptr > {
1095
- // FIXME(#93176): this field is made `#[unstable] #[doc(hidden)] pub` to:
1096
- // - deter downstream users from accessing it (which would be unsound!),
1097
- // - let the `pin!` macro access it (such a macro requires using struct
1098
- // literal syntax in order to benefit from lifetime extension).
1099
- //
1100
- // However, if the `Deref` impl exposes a field with the same name as this
1101
- // field, then the two will collide, resulting in a confusing error when the
1102
- // user attempts to access the field through a `Pin<Ptr>`. Therefore, the
1103
- // name `__pointer` is designed to be unlikely to collide with any other
1104
- // field. Long-term, macro hygiene is expected to offer a more robust
1105
- // alternative, alongside `unsafe` fields.
1106
- #[ unstable( feature = "unsafe_pin_internals" , issue = "none" ) ]
1107
- #[ doc( hidden) ]
1108
- pub __pointer : Ptr ,
1095
+ /// Only public for bootstrap.
1096
+ #[ cfg( bootstrap) ]
1097
+ pub pointer : Ptr ,
1098
+ #[ cfg( not( bootstrap) ) ]
1099
+ pointer : Ptr ,
1109
1100
}
1110
1101
1111
1102
// The following implementations aren't derived in order to avoid soundness
1112
- // issues. `&self.__pointer ` should not be accessible to untrusted trait
1103
+ // issues. `&self.pointer ` should not be accessible to untrusted trait
1113
1104
// implementations.
1114
1105
//
1115
1106
// See <https://internals.rust-lang.org/t/unsoundness-in-pin/11311/73> for more details.
@@ -1223,7 +1214,7 @@ impl<Ptr: Deref<Target: Unpin>> Pin<Ptr> {
1223
1214
#[ rustc_const_stable( feature = "const_pin" , since = "1.84.0" ) ]
1224
1215
#[ stable( feature = "pin_into_inner" , since = "1.39.0" ) ]
1225
1216
pub const fn into_inner ( pin : Pin < Ptr > ) -> Ptr {
1226
- pin. __pointer
1217
+ pin. pointer
1227
1218
}
1228
1219
}
1229
1220
@@ -1360,7 +1351,7 @@ impl<Ptr: Deref> Pin<Ptr> {
1360
1351
#[ rustc_const_stable( feature = "const_pin" , since = "1.84.0" ) ]
1361
1352
#[ stable( feature = "pin" , since = "1.33.0" ) ]
1362
1353
pub const unsafe fn new_unchecked ( pointer : Ptr ) -> Pin < Ptr > {
1363
- Pin { __pointer : pointer }
1354
+ Pin { pointer }
1364
1355
}
1365
1356
1366
1357
/// Gets a shared reference to the pinned value this [`Pin`] points to.
@@ -1374,7 +1365,7 @@ impl<Ptr: Deref> Pin<Ptr> {
1374
1365
#[ inline( always) ]
1375
1366
pub fn as_ref ( & self ) -> Pin < & Ptr :: Target > {
1376
1367
// SAFETY: see documentation on this function
1377
- unsafe { Pin :: new_unchecked ( & * self . __pointer ) }
1368
+ unsafe { Pin :: new_unchecked ( & * self . pointer ) }
1378
1369
}
1379
1370
}
1380
1371
@@ -1418,7 +1409,7 @@ impl<Ptr: DerefMut> Pin<Ptr> {
1418
1409
#[ inline( always) ]
1419
1410
pub fn as_mut ( & mut self ) -> Pin < & mut Ptr :: Target > {
1420
1411
// SAFETY: see documentation on this function
1421
- unsafe { Pin :: new_unchecked ( & mut * self . __pointer ) }
1412
+ unsafe { Pin :: new_unchecked ( & mut * self . pointer ) }
1422
1413
}
1423
1414
1424
1415
/// Gets `Pin<&mut T>` to the underlying pinned value from this nested `Pin`-pointer.
@@ -1485,7 +1476,7 @@ impl<Ptr: DerefMut> Pin<Ptr> {
1485
1476
where
1486
1477
Ptr :: Target : Sized ,
1487
1478
{
1488
- * ( self . __pointer ) = value;
1479
+ * ( self . pointer ) = value;
1489
1480
}
1490
1481
}
1491
1482
@@ -1513,7 +1504,7 @@ impl<Ptr: Deref> Pin<Ptr> {
1513
1504
#[ rustc_const_stable( feature = "const_pin" , since = "1.84.0" ) ]
1514
1505
#[ stable( feature = "pin_into_inner" , since = "1.39.0" ) ]
1515
1506
pub const unsafe fn into_inner_unchecked ( pin : Pin < Ptr > ) -> Ptr {
1516
- pin. __pointer
1507
+ pin. pointer
1517
1508
}
1518
1509
}
1519
1510
@@ -1539,7 +1530,7 @@ impl<'a, T: ?Sized> Pin<&'a T> {
1539
1530
U : ?Sized ,
1540
1531
F : FnOnce ( & T ) -> & U ,
1541
1532
{
1542
- let pointer = & * self . __pointer ;
1533
+ let pointer = & * self . pointer ;
1543
1534
let new_pointer = func ( pointer) ;
1544
1535
1545
1536
// SAFETY: the safety contract for `new_unchecked` must be
@@ -1569,7 +1560,7 @@ impl<'a, T: ?Sized> Pin<&'a T> {
1569
1560
#[ rustc_const_stable( feature = "const_pin" , since = "1.84.0" ) ]
1570
1561
#[ stable( feature = "pin" , since = "1.33.0" ) ]
1571
1562
pub const fn get_ref ( self ) -> & ' a T {
1572
- self . __pointer
1563
+ self . pointer
1573
1564
}
1574
1565
}
1575
1566
@@ -1580,7 +1571,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
1580
1571
#[ rustc_const_stable( feature = "const_pin" , since = "1.84.0" ) ]
1581
1572
#[ stable( feature = "pin" , since = "1.33.0" ) ]
1582
1573
pub const fn into_ref ( self ) -> Pin < & ' a T > {
1583
- Pin { __pointer : self . __pointer }
1574
+ Pin { pointer : self . pointer }
1584
1575
}
1585
1576
1586
1577
/// Gets a mutable reference to the data inside of this `Pin`.
@@ -1600,7 +1591,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
1600
1591
where
1601
1592
T : Unpin ,
1602
1593
{
1603
- self . __pointer
1594
+ self . pointer
1604
1595
}
1605
1596
1606
1597
/// Gets a mutable reference to the data inside of this `Pin`.
@@ -1618,7 +1609,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
1618
1609
#[ stable( feature = "pin" , since = "1.33.0" ) ]
1619
1610
#[ rustc_const_stable( feature = "const_pin" , since = "1.84.0" ) ]
1620
1611
pub const unsafe fn get_unchecked_mut ( self ) -> & ' a mut T {
1621
- self . __pointer
1612
+ self . pointer
1622
1613
}
1623
1614
1624
1615
/// Constructs a new pin by mapping the interior value.
@@ -1705,21 +1696,21 @@ impl<Ptr: LegacyReceiver> LegacyReceiver for Pin<Ptr> {}
1705
1696
#[ stable( feature = "pin" , since = "1.33.0" ) ]
1706
1697
impl < Ptr : fmt:: Debug > fmt:: Debug for Pin < Ptr > {
1707
1698
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1708
- fmt:: Debug :: fmt ( & self . __pointer , f)
1699
+ fmt:: Debug :: fmt ( & self . pointer , f)
1709
1700
}
1710
1701
}
1711
1702
1712
1703
#[ stable( feature = "pin" , since = "1.33.0" ) ]
1713
1704
impl < Ptr : fmt:: Display > fmt:: Display for Pin < Ptr > {
1714
1705
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1715
- fmt:: Display :: fmt ( & self . __pointer , f)
1706
+ fmt:: Display :: fmt ( & self . pointer , f)
1716
1707
}
1717
1708
}
1718
1709
1719
1710
#[ stable( feature = "pin" , since = "1.33.0" ) ]
1720
1711
impl < Ptr : fmt:: Pointer > fmt:: Pointer for Pin < Ptr > {
1721
1712
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1722
- fmt:: Pointer :: fmt ( & self . __pointer , f)
1713
+ fmt:: Pointer :: fmt ( & self . pointer , f)
1723
1714
}
1724
1715
}
1725
1716
@@ -1945,80 +1936,22 @@ unsafe impl<T: ?Sized> PinCoerceUnsized for *mut T {}
1945
1936
/// constructor.
1946
1937
///
1947
1938
/// [`Box::pin`]: ../../std/boxed/struct.Box.html#method.pin
1939
+ #[ cfg( not( bootstrap) ) ]
1948
1940
#[ stable( feature = "pin_macro" , since = "1.68.0" ) ]
1949
1941
#[ rustc_macro_transparency = "semitransparent" ]
1950
- #[ allow_internal_unstable( unsafe_pin_internals) ]
1951
- #[ rustc_macro_edition_2021]
1942
+ #[ allow_internal_unstable( super_let) ]
1952
1943
pub macro pin ( $value: expr $( , ) ?) {
1953
- // This is `Pin::new_unchecked(&mut { $value })`, so, for starters, let's
1954
- // review such a hypothetical macro (that any user-code could define):
1955
- //
1956
- // ```rust
1957
- // macro_rules! pin {( $value:expr ) => (
1958
- // match &mut { $value } { at_value => unsafe { // Do not wrap `$value` in an `unsafe` block.
1959
- // $crate::pin::Pin::<&mut _>::new_unchecked(at_value)
1960
- // }}
1961
- // )}
1962
- // ```
1963
- //
1964
- // Safety:
1965
- // - `type P = &mut _`. There are thus no pathological `Deref{,Mut}` impls
1966
- // that would break `Pin`'s invariants.
1967
- // - `{ $value }` is braced, making it a _block expression_, thus **moving**
1968
- // the given `$value`, and making it _become an **anonymous** temporary_.
1969
- // By virtue of being anonymous, it can no longer be accessed, thus
1970
- // preventing any attempts to `mem::replace` it or `mem::forget` it, _etc._
1971
- //
1972
- // This gives us a `pin!` definition that is sound, and which works, but only
1973
- // in certain scenarios:
1974
- // - If the `pin!(value)` expression is _directly_ fed to a function call:
1975
- // `let poll = pin!(fut).poll(cx);`
1976
- // - If the `pin!(value)` expression is part of a scrutinee:
1977
- // ```rust
1978
- // match pin!(fut) { pinned_fut => {
1979
- // pinned_fut.as_mut().poll(...);
1980
- // pinned_fut.as_mut().poll(...);
1981
- // }} // <- `fut` is dropped here.
1982
- // ```
1983
- // Alas, it doesn't work for the more straight-forward use-case: `let` bindings.
1984
- // ```rust
1985
- // let pinned_fut = pin!(fut); // <- temporary value is freed at the end of this statement
1986
- // pinned_fut.poll(...) // error[E0716]: temporary value dropped while borrowed
1987
- // // note: consider using a `let` binding to create a longer lived value
1988
- // ```
1989
- // - Issues such as this one are the ones motivating https://github.com/rust-lang/rfcs/pull/66
1990
- //
1991
- // This makes such a macro incredibly unergonomic in practice, and the reason most macros
1992
- // out there had to take the path of being a statement/binding macro (_e.g._, `pin!(future);`)
1993
- // instead of featuring the more intuitive ergonomics of an expression macro.
1994
- //
1995
- // Luckily, there is a way to avoid the problem. Indeed, the problem stems from the fact that a
1996
- // temporary is dropped at the end of its enclosing statement when it is part of the parameters
1997
- // given to function call, which has precisely been the case with our `Pin::new_unchecked()`!
1998
- // For instance,
1999
- // ```rust
2000
- // let p = Pin::new_unchecked(&mut <temporary>);
2001
- // ```
2002
- // becomes:
2003
- // ```rust
2004
- // let p = { let mut anon = <temporary>; &mut anon };
2005
- // ```
2006
- //
2007
- // However, when using a literal braced struct to construct the value, references to temporaries
2008
- // can then be taken. This makes Rust change the lifespan of such temporaries so that they are,
2009
- // instead, dropped _at the end of the enscoping block_.
2010
- // For instance,
2011
- // ```rust
2012
- // let p = Pin { __pointer: &mut <temporary> };
2013
- // ```
2014
- // becomes:
2015
- // ```rust
2016
- // let mut anon = <temporary>;
2017
- // let p = Pin { __pointer: &mut anon };
2018
- // ```
2019
- // which is *exactly* what we want.
2020
- //
2021
- // See https://doc.rust-lang.org/1.58.1/reference/destructors.html#temporary-lifetime-extension
2022
- // for more info.
2023
- $crate:: pin:: Pin :: < & mut _ > { __pointer : & mut { $value } }
1944
+ {
1945
+ super let mut pinned = $value;
1946
+ // SAFETY: The value is pinned: it is the local above which cannot be named outside this macro.
1947
+ unsafe { $crate:: pin:: Pin :: new_unchecked ( & mut pinned) }
1948
+ }
1949
+ }
1950
+
1951
+ /// Only for bootstrap.
1952
+ #[ cfg( bootstrap) ]
1953
+ #[ stable( feature = "pin_macro" , since = "1.68.0" ) ]
1954
+ #[ rustc_macro_transparency = "semitransparent" ]
1955
+ pub macro pin ( $value: expr $( , ) ?) {
1956
+ $crate:: pin:: Pin :: < & mut _ > { pointer : & mut { $value } }
2024
1957
}
0 commit comments