@@ -411,14 +411,20 @@ pub struct Pin<P> {
411
411
// - deter downstream users from accessing it (which would be unsound!),
412
412
// - let the `pin!` macro access it (such a macro requires using struct
413
413
// literal syntax in order to benefit from lifetime extension).
414
- // Long-term, `unsafe` fields or macro hygiene are expected to offer more robust alternatives.
414
+ //
415
+ // However, if `P::Deref` exposes a field with the same name as this field,
416
+ // then the two will collide, resulting in a confusing error when the user
417
+ // attempts to access the field through a `Pin<P>`. Therefore, the name
418
+ // `__pointer` is designed to be unlikely to collide with any other
419
+ // field. Long-term, macro hygiene is expected to offer a more robust
420
+ // alternative, alongside `unsafe` fields.
415
421
#[ unstable( feature = "unsafe_pin_internals" , issue = "none" ) ]
416
422
#[ doc( hidden) ]
417
- pub pointer : P ,
423
+ pub __pointer : P ,
418
424
}
419
425
420
426
// The following implementations aren't derived in order to avoid soundness
421
- // issues. `&self.pointer ` should not be accessible to untrusted trait
427
+ // issues. `&self.__pointer ` should not be accessible to untrusted trait
422
428
// implementations.
423
429
//
424
430
// See <https://internals.rust-lang.org/t/unsoundness-in-pin/11311/73> for more details.
@@ -525,7 +531,7 @@ impl<P: Deref<Target: Unpin>> Pin<P> {
525
531
#[ rustc_const_unstable( feature = "const_pin" , issue = "76654" ) ]
526
532
#[ stable( feature = "pin_into_inner" , since = "1.39.0" ) ]
527
533
pub const fn into_inner ( pin : Pin < P > ) -> P {
528
- pin. pointer
534
+ pin. __pointer
529
535
}
530
536
}
531
537
@@ -654,7 +660,7 @@ impl<P: Deref> Pin<P> {
654
660
#[ rustc_const_unstable( feature = "const_pin" , issue = "76654" ) ]
655
661
#[ stable( feature = "pin" , since = "1.33.0" ) ]
656
662
pub const unsafe fn new_unchecked ( pointer : P ) -> Pin < P > {
657
- Pin { pointer }
663
+ Pin { __pointer : pointer }
658
664
}
659
665
660
666
/// Gets a pinned shared reference from this pinned pointer.
@@ -668,7 +674,7 @@ impl<P: Deref> Pin<P> {
668
674
#[ inline( always) ]
669
675
pub fn as_ref ( & self ) -> Pin < & P :: Target > {
670
676
// SAFETY: see documentation on this function
671
- unsafe { Pin :: new_unchecked ( & * self . pointer ) }
677
+ unsafe { Pin :: new_unchecked ( & * self . __pointer ) }
672
678
}
673
679
674
680
/// Unwraps this `Pin<P>` returning the underlying pointer.
@@ -688,7 +694,7 @@ impl<P: Deref> Pin<P> {
688
694
#[ rustc_const_unstable( feature = "const_pin" , issue = "76654" ) ]
689
695
#[ stable( feature = "pin_into_inner" , since = "1.39.0" ) ]
690
696
pub const unsafe fn into_inner_unchecked ( pin : Pin < P > ) -> P {
691
- pin. pointer
697
+ pin. __pointer
692
698
}
693
699
}
694
700
@@ -725,7 +731,7 @@ impl<P: DerefMut> Pin<P> {
725
731
#[ inline( always) ]
726
732
pub fn as_mut ( & mut self ) -> Pin < & mut P :: Target > {
727
733
// SAFETY: see documentation on this function
728
- unsafe { Pin :: new_unchecked ( & mut * self . pointer ) }
734
+ unsafe { Pin :: new_unchecked ( & mut * self . __pointer ) }
729
735
}
730
736
731
737
/// Assigns a new value to the memory behind the pinned reference.
@@ -750,7 +756,7 @@ impl<P: DerefMut> Pin<P> {
750
756
where
751
757
P :: Target : Sized ,
752
758
{
753
- * ( self . pointer ) = value;
759
+ * ( self . __pointer ) = value;
754
760
}
755
761
}
756
762
@@ -776,7 +782,7 @@ impl<'a, T: ?Sized> Pin<&'a T> {
776
782
U : ?Sized ,
777
783
F : FnOnce ( & T ) -> & U ,
778
784
{
779
- let pointer = & * self . pointer ;
785
+ let pointer = & * self . __pointer ;
780
786
let new_pointer = func ( pointer) ;
781
787
782
788
// SAFETY: the safety contract for `new_unchecked` must be
@@ -806,7 +812,7 @@ impl<'a, T: ?Sized> Pin<&'a T> {
806
812
#[ rustc_const_unstable( feature = "const_pin" , issue = "76654" ) ]
807
813
#[ stable( feature = "pin" , since = "1.33.0" ) ]
808
814
pub const fn get_ref ( self ) -> & ' a T {
809
- self . pointer
815
+ self . __pointer
810
816
}
811
817
}
812
818
@@ -817,7 +823,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
817
823
#[ rustc_const_unstable( feature = "const_pin" , issue = "76654" ) ]
818
824
#[ stable( feature = "pin" , since = "1.33.0" ) ]
819
825
pub const fn into_ref ( self ) -> Pin < & ' a T > {
820
- Pin { pointer : self . pointer }
826
+ Pin { __pointer : self . __pointer }
821
827
}
822
828
823
829
/// Gets a mutable reference to the data inside of this `Pin`.
@@ -837,7 +843,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
837
843
where
838
844
T : Unpin ,
839
845
{
840
- self . pointer
846
+ self . __pointer
841
847
}
842
848
843
849
/// Gets a mutable reference to the data inside of this `Pin`.
@@ -855,7 +861,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
855
861
#[ stable( feature = "pin" , since = "1.33.0" ) ]
856
862
#[ rustc_const_unstable( feature = "const_pin" , issue = "76654" ) ]
857
863
pub const unsafe fn get_unchecked_mut ( self ) -> & ' a mut T {
858
- self . pointer
864
+ self . __pointer
859
865
}
860
866
861
867
/// Construct a new pin by mapping the interior value.
@@ -978,21 +984,21 @@ impl<P: Receiver> Receiver for Pin<P> {}
978
984
#[ stable( feature = "pin" , since = "1.33.0" ) ]
979
985
impl < P : fmt:: Debug > fmt:: Debug for Pin < P > {
980
986
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
981
- fmt:: Debug :: fmt ( & self . pointer , f)
987
+ fmt:: Debug :: fmt ( & self . __pointer , f)
982
988
}
983
989
}
984
990
985
991
#[ stable( feature = "pin" , since = "1.33.0" ) ]
986
992
impl < P : fmt:: Display > fmt:: Display for Pin < P > {
987
993
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
988
- fmt:: Display :: fmt ( & self . pointer , f)
994
+ fmt:: Display :: fmt ( & self . __pointer , f)
989
995
}
990
996
}
991
997
992
998
#[ stable( feature = "pin" , since = "1.33.0" ) ]
993
999
impl < P : fmt:: Pointer > fmt:: Pointer for Pin < P > {
994
1000
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
995
- fmt:: Pointer :: fmt ( & self . pointer , f)
1001
+ fmt:: Pointer :: fmt ( & self . __pointer , f)
996
1002
}
997
1003
}
998
1004
@@ -1235,16 +1241,16 @@ pub macro pin($value:expr $(,)?) {
1235
1241
// instead, dropped _at the end of the enscoping block_.
1236
1242
// For instance,
1237
1243
// ```rust
1238
- // let p = Pin { pointer : &mut <temporary> };
1244
+ // let p = Pin { __pointer : &mut <temporary> };
1239
1245
// ```
1240
1246
// becomes:
1241
1247
// ```rust
1242
1248
// let mut anon = <temporary>;
1243
- // let p = Pin { pointer : &mut anon };
1249
+ // let p = Pin { __pointer : &mut anon };
1244
1250
// ```
1245
1251
// which is *exactly* what we want.
1246
1252
//
1247
1253
// See https://doc.rust-lang.org/1.58.1/reference/destructors.html#temporary-lifetime-extension
1248
1254
// for more info.
1249
- $crate:: pin:: Pin :: < & mut _ > { pointer : & mut { $value } }
1255
+ $crate:: pin:: Pin :: < & mut _ > { __pointer : & mut { $value } }
1250
1256
}
0 commit comments