@@ -140,12 +140,40 @@ pub mod invariant {
140
140
type Aliasing : Aliasing ;
141
141
type Alignment : Alignment ;
142
142
type Validity : Validity ;
143
+
144
+ /// Invariants identical to `Self` except with a different aliasing
145
+ /// invariant.
146
+ type WithAliasing < A : Aliasing > : Invariants <
147
+ Aliasing = A ,
148
+ Alignment = Self :: Alignment ,
149
+ Validity = Self :: Validity ,
150
+ > ;
151
+
152
+ /// Invariants identical to `Self` except with a different alignment
153
+ /// invariant.
154
+ type WithAlignment < A : Alignment > : Invariants <
155
+ Aliasing = Self :: Aliasing ,
156
+ Alignment = A ,
157
+ Validity = Self :: Validity ,
158
+ > ;
159
+
160
+ /// Invariants identical to `Self` except with a different validity
161
+ /// invariant.
162
+ type WithValidity < V : Validity > : Invariants <
163
+ Aliasing = Self :: Aliasing ,
164
+ Alignment = Self :: Alignment ,
165
+ Validity = V ,
166
+ > ;
143
167
}
144
168
145
169
impl < A : Aliasing , AA : Alignment , V : Validity > Invariants for ( A , AA , V ) {
146
170
type Aliasing = A ;
147
171
type Alignment = AA ;
148
172
type Validity = V ;
173
+
174
+ type WithAliasing < AB : Aliasing > = ( AB , AA , V ) ;
175
+ type WithAlignment < AB : Alignment > = ( A , AB , V ) ;
176
+ type WithValidity < VB : Validity > = ( A , AA , VB ) ;
149
177
}
150
178
151
179
/// The aliasing invariant of a [`Ptr`][super::Ptr].
@@ -576,9 +604,7 @@ mod _conversions {
576
604
{
577
605
/// Converts a `Ptr` an unaligned `T` into a `Ptr` to an aligned
578
606
/// `Unalign<T>`.
579
- pub ( crate ) fn into_unalign (
580
- self ,
581
- ) -> Ptr < ' a , crate :: Unalign < T > , ( I :: Aliasing , Aligned , I :: Validity ) > {
607
+ pub ( crate ) fn into_unalign ( self ) -> Ptr < ' a , crate :: Unalign < T > , I :: WithAlignment < Aligned > > {
582
608
// SAFETY:
583
609
// - This cast preserves provenance.
584
610
// - This cast preserves address. `Unalign<T>` promises to have the
@@ -596,7 +622,7 @@ mod _conversions {
596
622
// SAFETY: `Unalign<T>` promises to have alignment 1, and so it is
597
623
// trivially aligned.
598
624
let ptr = unsafe { ptr. assume_alignment :: < Aligned > ( ) } ;
599
- ptr
625
+ ptr. unify_invariants ( )
600
626
}
601
627
}
602
628
}
@@ -621,7 +647,7 @@ mod _transitions {
621
647
#[ inline]
622
648
pub ( crate ) fn into_exclusive_or_post_monomorphization_error (
623
649
self ,
624
- ) -> Ptr < ' a , T , ( Exclusive , I :: Alignment , I :: Validity ) > {
650
+ ) -> Ptr < ' a , T , I :: WithAliasing < Exclusive > > {
625
651
// NOTE(https://github.com/rust-lang/rust/issues/131625): We do this
626
652
// rather than just having `Aliasing::IS_EXCLUSIVE` have the panic
627
653
// behavior because doing it that way causes rustdoc to fail while
@@ -681,7 +707,7 @@ mod _transitions {
681
707
#[ inline]
682
708
pub ( crate ) const unsafe fn assume_aliasing < A : Aliasing > (
683
709
self ,
684
- ) -> Ptr < ' a , T , ( A , I :: Alignment , I :: Validity ) > {
710
+ ) -> Ptr < ' a , T , I :: WithAliasing < A > > {
685
711
// SAFETY: The caller promises that `self` satisfies the aliasing
686
712
// requirements of `A`.
687
713
unsafe { self . assume_invariants ( ) }
@@ -698,7 +724,7 @@ mod _transitions {
698
724
#[ inline]
699
725
pub ( crate ) const unsafe fn assume_exclusive (
700
726
self ,
701
- ) -> Ptr < ' a , T , ( Exclusive , I :: Alignment , I :: Validity ) > {
727
+ ) -> Ptr < ' a , T , I :: WithAliasing < Exclusive > > {
702
728
// SAFETY: The caller promises that `self` satisfies the aliasing
703
729
// requirements of `Exclusive`.
704
730
unsafe { self . assume_aliasing :: < Exclusive > ( ) }
@@ -714,7 +740,7 @@ mod _transitions {
714
740
#[ inline]
715
741
pub ( crate ) const unsafe fn assume_alignment < A : Alignment > (
716
742
self ,
717
- ) -> Ptr < ' a , T , ( I :: Aliasing , A , I :: Validity ) > {
743
+ ) -> Ptr < ' a , T , I :: WithAlignment < A > > {
718
744
// SAFETY: The caller promises that `self`'s referent is
719
745
// well-aligned for `T` if required by `A` .
720
746
unsafe { self . assume_invariants ( ) }
@@ -724,7 +750,7 @@ mod _transitions {
724
750
/// on success.
725
751
pub ( crate ) fn bikeshed_try_into_aligned (
726
752
self ,
727
- ) -> Result < Ptr < ' a , T , ( I :: Aliasing , Aligned , I :: Validity ) > , AlignmentError < Self , T > >
753
+ ) -> Result < Ptr < ' a , T , I :: WithAlignment < Aligned > > , AlignmentError < Self , T > >
728
754
where
729
755
T : Sized ,
730
756
{
@@ -742,9 +768,7 @@ mod _transitions {
742
768
#[ inline]
743
769
// TODO(#859): Reconsider the name of this method before making it
744
770
// public.
745
- pub ( crate ) const fn bikeshed_recall_aligned (
746
- self ,
747
- ) -> Ptr < ' a , T , ( I :: Aliasing , Aligned , I :: Validity ) >
771
+ pub ( crate ) const fn bikeshed_recall_aligned ( self ) -> Ptr < ' a , T , I :: WithAlignment < Aligned > >
748
772
where
749
773
T : crate :: Unaligned ,
750
774
{
@@ -763,9 +787,7 @@ mod _transitions {
763
787
#[ doc( hidden) ]
764
788
#[ must_use]
765
789
#[ inline]
766
- pub const unsafe fn assume_validity < V : Validity > (
767
- self ,
768
- ) -> Ptr < ' a , T , ( I :: Aliasing , I :: Alignment , V ) > {
790
+ pub const unsafe fn assume_validity < V : Validity > ( self ) -> Ptr < ' a , T , I :: WithValidity < V > > {
769
791
// SAFETY: The caller promises that `self`'s referent conforms to
770
792
// the validity requirement of `V`.
771
793
unsafe { self . assume_invariants ( ) }
@@ -780,9 +802,7 @@ mod _transitions {
780
802
#[ doc( hidden) ]
781
803
#[ must_use]
782
804
#[ inline]
783
- pub const unsafe fn assume_initialized (
784
- self ,
785
- ) -> Ptr < ' a , T , ( I :: Aliasing , I :: Alignment , Initialized ) > {
805
+ pub const unsafe fn assume_initialized ( self ) -> Ptr < ' a , T , I :: WithValidity < Initialized > > {
786
806
// SAFETY: The caller has promised to uphold the safety
787
807
// preconditions.
788
808
unsafe { self . assume_validity :: < Initialized > ( ) }
@@ -797,7 +817,7 @@ mod _transitions {
797
817
#[ doc( hidden) ]
798
818
#[ must_use]
799
819
#[ inline]
800
- pub const unsafe fn assume_valid ( self ) -> Ptr < ' a , T , ( I :: Aliasing , I :: Alignment , Valid ) > {
820
+ pub const unsafe fn assume_valid ( self ) -> Ptr < ' a , T , I :: WithValidity < Valid > > {
801
821
// SAFETY: The caller has promised to uphold the safety
802
822
// preconditions.
803
823
unsafe { self . assume_validity :: < Valid > ( ) }
@@ -809,7 +829,7 @@ mod _transitions {
809
829
#[ inline]
810
830
// TODO(#859): Reconsider the name of this method before making it
811
831
// public.
812
- pub const fn bikeshed_recall_valid ( self ) -> Ptr < ' a , T , ( I :: Aliasing , I :: Alignment , Valid ) >
832
+ pub const fn bikeshed_recall_valid ( self ) -> Ptr < ' a , T , I :: WithValidity < Valid > >
813
833
where
814
834
T : crate :: FromBytes ,
815
835
I : Invariants < Validity = Initialized > ,
@@ -836,7 +856,7 @@ mod _transitions {
836
856
#[ inline]
837
857
pub ( crate ) fn try_into_valid (
838
858
mut self ,
839
- ) -> Result < Ptr < ' a , T , ( I :: Aliasing , I :: Alignment , Valid ) > , ValidityError < Self , T > >
859
+ ) -> Result < Ptr < ' a , T , I :: WithValidity < Valid > > , ValidityError < Self , T > >
840
860
where
841
861
T : TryFromBytes ,
842
862
I :: Aliasing : Reference ,
@@ -845,7 +865,7 @@ mod _transitions {
845
865
// This call may panic. If that happens, it doesn't cause any soundness
846
866
// issues, as we have not generated any invalid state which we need to
847
867
// fix before returning.
848
- if T :: is_bit_valid ( self . reborrow ( ) . forget_aligned ( ) ) {
868
+ if T :: is_bit_valid ( self . reborrow ( ) . forget_aligned ( ) . unify_invariants ( ) ) {
849
869
// SAFETY: If `T::is_bit_valid`, code may assume that `self`
850
870
// contains a bit-valid instance of `Self`.
851
871
Ok ( unsafe { self . assume_valid ( ) } )
@@ -858,7 +878,7 @@ mod _transitions {
858
878
#[ doc( hidden) ]
859
879
#[ must_use]
860
880
#[ inline]
861
- pub const fn forget_aligned ( self ) -> Ptr < ' a , T , ( I :: Aliasing , Any , I :: Validity ) > {
881
+ pub const fn forget_aligned ( self ) -> Ptr < ' a , T , I :: WithAlignment < Any > > {
862
882
// SAFETY: `Any` is less restrictive than `Aligned`.
863
883
unsafe { self . assume_invariants ( ) }
864
884
}
0 commit comments