@@ -613,12 +613,41 @@ mod _conversions {
613
613
c
614
614
}
615
615
}
616
+
617
+ impl < ' a , T , I > Ptr < ' a , T , I >
618
+ where
619
+ I : Invariants ,
620
+ {
621
+ /// Converts a `Ptr` to a `Unalign<T>` into an `Ptr` to an unaligned `T`.
622
+ pub ( crate ) fn into_unaligned (
623
+ self ,
624
+ ) -> Ptr < ' a , crate :: Unalign < T > , ( I :: Aliasing , Aligned , I :: Validity ) > {
625
+ // SAFETY: We define `Unalign<T>` to be a `#[repr(C, packed)]` type
626
+ // wrapping a single `T` field. Thus, `Unalign<T>` has the same size
627
+ // as `T` and contains `UnsafeCell`s at the same locations as `T`.
628
+ // The cast is implemented in the form `|p: *mut T| p as *mut U`,
629
+ // where `U` is `Unalign<T>`.
630
+ let ptr = unsafe {
631
+ #[ allow( clippy:: as_conversions) ]
632
+ self . cast_unsized ( |p : * mut T | p as * mut crate :: Unalign < T > )
633
+ } ;
634
+ // SAFETY: We define `Unalign<T>` to be a `#[repr(C, packed)]` type
635
+ // wrapping a single `T` field, thus `Unalign<T>` has exactly the
636
+ // same validity as `T`.
637
+ let ptr = unsafe { ptr. assume_validity :: < I :: Validity > ( ) } ;
638
+ // SAFETY: We define `Unalign<T>` to be a `#[repr(C, packed)]` type
639
+ // wrapping a single `T` field, thus `Unalign<T>` is always
640
+ // trivially aligned.
641
+ let ptr = unsafe { ptr. assume_alignment :: < Aligned > ( ) } ;
642
+ ptr
643
+ }
644
+ }
616
645
}
617
646
618
647
/// State transitions between invariants.
619
648
mod _transitions {
620
649
use super :: * ;
621
- use crate :: { TryFromBytes , ValidityError } ;
650
+ use crate :: { AlignmentError , TryFromBytes , ValidityError } ;
622
651
623
652
impl < ' a , T , I > Ptr < ' a , T , I >
624
653
where
@@ -745,16 +774,16 @@ mod _transitions {
745
774
/// on success.
746
775
pub ( crate ) fn bikeshed_try_into_aligned (
747
776
self ,
748
- ) -> Option < Ptr < ' a , T , ( I :: Aliasing , Aligned , I :: Validity ) > >
777
+ ) -> Result < Ptr < ' a , T , ( I :: Aliasing , Aligned , I :: Validity ) > , AlignmentError < Self , T > >
749
778
where
750
779
T : Sized ,
751
780
{
752
781
if !crate :: util:: aligned_to :: < _ , T > ( self . as_non_null ( ) ) {
753
- return None ;
782
+ return Err ( AlignmentError :: new ( self ) ) ;
754
783
}
755
784
756
785
// SAFETY: We just checked the alignment.
757
- Some ( unsafe { self . assume_alignment :: < Aligned > ( ) } )
786
+ Ok ( unsafe { self . assume_alignment :: < Aligned > ( ) } )
758
787
}
759
788
760
789
/// Recalls that `self`'s referent is validly-aligned for `T`.
0 commit comments