@@ -1087,24 +1087,15 @@ use crate::{cmp, fmt};
1087
1087
#[ rustc_pub_transparent]
1088
1088
#[ derive( Copy , Clone ) ]
1089
1089
pub struct Pin < Ptr > {
1090
- // FIXME(#93176): this field is made `#[unstable] #[doc(hidden)] pub` to:
1091
- // - deter downstream users from accessing it (which would be unsound!),
1092
- // - let the `pin!` macro access it (such a macro requires using struct
1093
- // literal syntax in order to benefit from lifetime extension).
1094
- //
1095
- // However, if the `Deref` impl exposes a field with the same name as this
1096
- // field, then the two will collide, resulting in a confusing error when the
1097
- // user attempts to access the field through a `Pin<Ptr>`. Therefore, the
1098
- // name `__pointer` is designed to be unlikely to collide with any other
1099
- // field. Long-term, macro hygiene is expected to offer a more robust
1100
- // alternative, alongside `unsafe` fields.
1101
- #[ unstable( feature = "unsafe_pin_internals" , issue = "none" ) ]
1102
- #[ doc( hidden) ]
1103
- pub __pointer : Ptr ,
1090
+ /// Only public for bootstrap.
1091
+ #[ cfg( bootstrap) ]
1092
+ pub pointer : Ptr ,
1093
+ #[ cfg( not( bootstrap) ) ]
1094
+ pointer : Ptr ,
1104
1095
}
1105
1096
1106
1097
// The following implementations aren't derived in order to avoid soundness
1107
- // issues. `&self.__pointer ` should not be accessible to untrusted trait
1098
+ // issues. `&self.pointer ` should not be accessible to untrusted trait
1108
1099
// implementations.
1109
1100
//
1110
1101
// See <https://internals.rust-lang.org/t/unsoundness-in-pin/11311/73> for more details.
@@ -1218,7 +1209,7 @@ impl<Ptr: Deref<Target: Unpin>> Pin<Ptr> {
1218
1209
#[ rustc_const_stable( feature = "const_pin" , since = "1.84.0" ) ]
1219
1210
#[ stable( feature = "pin_into_inner" , since = "1.39.0" ) ]
1220
1211
pub const fn into_inner ( pin : Pin < Ptr > ) -> Ptr {
1221
- pin. __pointer
1212
+ pin. pointer
1222
1213
}
1223
1214
}
1224
1215
@@ -1355,7 +1346,7 @@ impl<Ptr: Deref> Pin<Ptr> {
1355
1346
#[ rustc_const_stable( feature = "const_pin" , since = "1.84.0" ) ]
1356
1347
#[ stable( feature = "pin" , since = "1.33.0" ) ]
1357
1348
pub const unsafe fn new_unchecked ( pointer : Ptr ) -> Pin < Ptr > {
1358
- Pin { __pointer : pointer }
1349
+ Pin { pointer }
1359
1350
}
1360
1351
1361
1352
/// Gets a shared reference to the pinned value this [`Pin`] points to.
@@ -1369,7 +1360,7 @@ impl<Ptr: Deref> Pin<Ptr> {
1369
1360
#[ inline( always) ]
1370
1361
pub fn as_ref ( & self ) -> Pin < & Ptr :: Target > {
1371
1362
// SAFETY: see documentation on this function
1372
- unsafe { Pin :: new_unchecked ( & * self . __pointer ) }
1363
+ unsafe { Pin :: new_unchecked ( & * self . pointer ) }
1373
1364
}
1374
1365
}
1375
1366
@@ -1413,7 +1404,7 @@ impl<Ptr: DerefMut> Pin<Ptr> {
1413
1404
#[ inline( always) ]
1414
1405
pub fn as_mut ( & mut self ) -> Pin < & mut Ptr :: Target > {
1415
1406
// SAFETY: see documentation on this function
1416
- unsafe { Pin :: new_unchecked ( & mut * self . __pointer ) }
1407
+ unsafe { Pin :: new_unchecked ( & mut * self . pointer ) }
1417
1408
}
1418
1409
1419
1410
/// Gets `Pin<&mut T>` to the underlying pinned value from this nested `Pin`-pointer.
@@ -1480,7 +1471,7 @@ impl<Ptr: DerefMut> Pin<Ptr> {
1480
1471
where
1481
1472
Ptr :: Target : Sized ,
1482
1473
{
1483
- * ( self . __pointer ) = value;
1474
+ * ( self . pointer ) = value;
1484
1475
}
1485
1476
}
1486
1477
@@ -1508,7 +1499,7 @@ impl<Ptr: Deref> Pin<Ptr> {
1508
1499
#[ rustc_const_stable( feature = "const_pin" , since = "1.84.0" ) ]
1509
1500
#[ stable( feature = "pin_into_inner" , since = "1.39.0" ) ]
1510
1501
pub const unsafe fn into_inner_unchecked ( pin : Pin < Ptr > ) -> Ptr {
1511
- pin. __pointer
1502
+ pin. pointer
1512
1503
}
1513
1504
}
1514
1505
@@ -1534,7 +1525,7 @@ impl<'a, T: ?Sized> Pin<&'a T> {
1534
1525
U : ?Sized ,
1535
1526
F : FnOnce ( & T ) -> & U ,
1536
1527
{
1537
- let pointer = & * self . __pointer ;
1528
+ let pointer = & * self . pointer ;
1538
1529
let new_pointer = func ( pointer) ;
1539
1530
1540
1531
// SAFETY: the safety contract for `new_unchecked` must be
@@ -1564,7 +1555,7 @@ impl<'a, T: ?Sized> Pin<&'a T> {
1564
1555
#[ rustc_const_stable( feature = "const_pin" , since = "1.84.0" ) ]
1565
1556
#[ stable( feature = "pin" , since = "1.33.0" ) ]
1566
1557
pub const fn get_ref ( self ) -> & ' a T {
1567
- self . __pointer
1558
+ self . pointer
1568
1559
}
1569
1560
}
1570
1561
@@ -1575,7 +1566,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
1575
1566
#[ rustc_const_stable( feature = "const_pin" , since = "1.84.0" ) ]
1576
1567
#[ stable( feature = "pin" , since = "1.33.0" ) ]
1577
1568
pub const fn into_ref ( self ) -> Pin < & ' a T > {
1578
- Pin { __pointer : self . __pointer }
1569
+ Pin { pointer : self . pointer }
1579
1570
}
1580
1571
1581
1572
/// Gets a mutable reference to the data inside of this `Pin`.
@@ -1595,7 +1586,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
1595
1586
where
1596
1587
T : Unpin ,
1597
1588
{
1598
- self . __pointer
1589
+ self . pointer
1599
1590
}
1600
1591
1601
1592
/// Gets a mutable reference to the data inside of this `Pin`.
@@ -1613,7 +1604,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
1613
1604
#[ stable( feature = "pin" , since = "1.33.0" ) ]
1614
1605
#[ rustc_const_stable( feature = "const_pin" , since = "1.84.0" ) ]
1615
1606
pub const unsafe fn get_unchecked_mut ( self ) -> & ' a mut T {
1616
- self . __pointer
1607
+ self . pointer
1617
1608
}
1618
1609
1619
1610
/// Constructs a new pin by mapping the interior value.
@@ -1700,21 +1691,21 @@ impl<Ptr: LegacyReceiver> LegacyReceiver for Pin<Ptr> {}
1700
1691
#[ stable( feature = "pin" , since = "1.33.0" ) ]
1701
1692
impl < Ptr : fmt:: Debug > fmt:: Debug for Pin < Ptr > {
1702
1693
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1703
- fmt:: Debug :: fmt ( & self . __pointer , f)
1694
+ fmt:: Debug :: fmt ( & self . pointer , f)
1704
1695
}
1705
1696
}
1706
1697
1707
1698
#[ stable( feature = "pin" , since = "1.33.0" ) ]
1708
1699
impl < Ptr : fmt:: Display > fmt:: Display for Pin < Ptr > {
1709
1700
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1710
- fmt:: Display :: fmt ( & self . __pointer , f)
1701
+ fmt:: Display :: fmt ( & self . pointer , f)
1711
1702
}
1712
1703
}
1713
1704
1714
1705
#[ stable( feature = "pin" , since = "1.33.0" ) ]
1715
1706
impl < Ptr : fmt:: Pointer > fmt:: Pointer for Pin < Ptr > {
1716
1707
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1717
- fmt:: Pointer :: fmt ( & self . __pointer , f)
1708
+ fmt:: Pointer :: fmt ( & self . pointer , f)
1718
1709
}
1719
1710
}
1720
1711
@@ -1942,78 +1933,20 @@ unsafe impl<T: ?Sized> PinCoerceUnsized for *mut T {}
1942
1933
/// [`Box::pin`]: ../../std/boxed/struct.Box.html#method.pin
1943
1934
#[ stable( feature = "pin_macro" , since = "1.68.0" ) ]
1944
1935
#[ rustc_macro_transparency = "semitransparent" ]
1945
- #[ allow_internal_unstable( unsafe_pin_internals ) ]
1946
- #[ cfg_attr ( not( bootstrap) , rustc_macro_edition_2021 ) ]
1936
+ #[ allow_internal_unstable( super_let ) ]
1937
+ #[ cfg ( not( bootstrap) ) ]
1947
1938
pub macro pin ( $value: expr $( , ) ?) {
1948
- // This is `Pin::new_unchecked(&mut { $value })`, so, for starters, let's
1949
- // review such a hypothetical macro (that any user-code could define):
1950
- //
1951
- // ```rust
1952
- // macro_rules! pin {( $value:expr ) => (
1953
- // match &mut { $value } { at_value => unsafe { // Do not wrap `$value` in an `unsafe` block.
1954
- // $crate::pin::Pin::<&mut _>::new_unchecked(at_value)
1955
- // }}
1956
- // )}
1957
- // ```
1958
- //
1959
- // Safety:
1960
- // - `type P = &mut _`. There are thus no pathological `Deref{,Mut}` impls
1961
- // that would break `Pin`'s invariants.
1962
- // - `{ $value }` is braced, making it a _block expression_, thus **moving**
1963
- // the given `$value`, and making it _become an **anonymous** temporary_.
1964
- // By virtue of being anonymous, it can no longer be accessed, thus
1965
- // preventing any attempts to `mem::replace` it or `mem::forget` it, _etc._
1966
- //
1967
- // This gives us a `pin!` definition that is sound, and which works, but only
1968
- // in certain scenarios:
1969
- // - If the `pin!(value)` expression is _directly_ fed to a function call:
1970
- // `let poll = pin!(fut).poll(cx);`
1971
- // - If the `pin!(value)` expression is part of a scrutinee:
1972
- // ```rust
1973
- // match pin!(fut) { pinned_fut => {
1974
- // pinned_fut.as_mut().poll(...);
1975
- // pinned_fut.as_mut().poll(...);
1976
- // }} // <- `fut` is dropped here.
1977
- // ```
1978
- // Alas, it doesn't work for the more straight-forward use-case: `let` bindings.
1979
- // ```rust
1980
- // let pinned_fut = pin!(fut); // <- temporary value is freed at the end of this statement
1981
- // pinned_fut.poll(...) // error[E0716]: temporary value dropped while borrowed
1982
- // // note: consider using a `let` binding to create a longer lived value
1983
- // ```
1984
- // - Issues such as this one are the ones motivating https://github.com/rust-lang/rfcs/pull/66
1985
- //
1986
- // This makes such a macro incredibly unergonomic in practice, and the reason most macros
1987
- // out there had to take the path of being a statement/binding macro (_e.g._, `pin!(future);`)
1988
- // instead of featuring the more intuitive ergonomics of an expression macro.
1989
- //
1990
- // Luckily, there is a way to avoid the problem. Indeed, the problem stems from the fact that a
1991
- // temporary is dropped at the end of its enclosing statement when it is part of the parameters
1992
- // given to function call, which has precisely been the case with our `Pin::new_unchecked()`!
1993
- // For instance,
1994
- // ```rust
1995
- // let p = Pin::new_unchecked(&mut <temporary>);
1996
- // ```
1997
- // becomes:
1998
- // ```rust
1999
- // let p = { let mut anon = <temporary>; &mut anon };
2000
- // ```
2001
- //
2002
- // However, when using a literal braced struct to construct the value, references to temporaries
2003
- // can then be taken. This makes Rust change the lifespan of such temporaries so that they are,
2004
- // instead, dropped _at the end of the enscoping block_.
2005
- // For instance,
2006
- // ```rust
2007
- // let p = Pin { __pointer: &mut <temporary> };
2008
- // ```
2009
- // becomes:
2010
- // ```rust
2011
- // let mut anon = <temporary>;
2012
- // let p = Pin { __pointer: &mut anon };
2013
- // ```
2014
- // which is *exactly* what we want.
2015
- //
2016
- // See https://doc.rust-lang.org/1.58.1/reference/destructors.html#temporary-lifetime-extension
2017
- // for more info.
2018
- $crate:: pin:: Pin :: < & mut _ > { __pointer : & mut { $value } }
1939
+ {
1940
+ super let mut pinned = $value;
1941
+ // SAFETY: The value is pinned: it is the local above which cannot be named outisde this macro.
1942
+ unsafe { $crate:: pin:: Pin :: new_unchecked ( & mut pinned) }
1943
+ }
1944
+ }
1945
+
1946
+ /// Only for bootstrap.
1947
+ #[ cfg( bootstrap) ]
1948
+ #[ stable( feature = "pin_macro" , since = "1.68.0" ) ]
1949
+ #[ rustc_macro_transparency = "semitransparent" ]
1950
+ pub macro pin ( $value: expr $( , ) ?) {
1951
+ $crate:: pin:: Pin :: < & mut _ > { pointer : & mut { $value } }
2019
1952
}
0 commit comments