@@ -630,6 +630,44 @@ impl<T: ?Sized> *const T {
630
630
Some ( diff / size as isize )
631
631
}
632
632
}
633
+
634
+ /// Computes the byte offset that needs to be applied in order to
635
+ /// make the pointer aligned to `align`.
636
+ /// If it is not possible to align the pointer, the implementation returns
637
+ /// `usize::max_value()`.
638
+ ///
639
+ /// There are no guarantees whatsover that offsetting the pointer will not
640
+ /// overflow or go beyond the allocation that the pointer points into.
641
+ /// It is up to the caller to ensure that the returned offset is correct
642
+ /// in all terms other than alignment.
643
+ ///
644
+ /// # Examples
645
+ ///
646
+ /// Accessing adjacent `u8` as `u16`
647
+ ///
648
+ /// ```
649
+ /// # #![feature(core_intrinsics)]
650
+ /// # fn foo(n: usize) {
651
+ /// # use std::mem::align_of;
652
+ /// # unsafe {
653
+ /// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
654
+ /// let ptr = &x[n] as *const u8;
655
+ /// let offset = ptr.align_offset(align_of::<u16>());
656
+ /// if offset < x.len() - n - 1 {
657
+ /// let u16_ptr = ptr.offset(offset as isize) as *const u16;
658
+ /// assert_ne!(*u16_ptr, 500);
659
+ /// } else {
660
+ /// // while the pointer can be aligned via `offset`, it would point
661
+ /// // outside the allocation
662
+ /// }
663
+ /// # } }
664
+ /// ```
665
+ #[ unstable( feature = "align_offset" , issue = "44488" ) ]
666
+ pub fn align_offset ( self , align : usize ) -> usize {
667
+ unsafe {
668
+ intrinsics:: align_offset ( self as * const _ , align)
669
+ }
670
+ }
633
671
}
634
672
635
673
#[ lang = "mut_ptr" ]
@@ -821,6 +859,44 @@ impl<T: ?Sized> *mut T {
821
859
Some ( diff / size as isize )
822
860
}
823
861
}
862
+
863
+ /// Computes the byte offset that needs to be applied in order to
864
+ /// make the pointer aligned to `align`.
865
+ /// If it is not possible to align the pointer, the implementation returns
866
+ /// `usize::max_value()`.
867
+ ///
868
+ /// There are no guarantees whatsover that offsetting the pointer will not
869
+ /// overflow or go beyond the allocation that the pointer points into.
870
+ /// It is up to the caller to ensure that the returned offset is correct
871
+ /// in all terms other than alignment.
872
+ ///
873
+ /// # Examples
874
+ ///
875
+ /// Accessing adjacent `u8` as `u16`
876
+ ///
877
+ /// ```
878
+ /// # #![feature(core_intrinsics)]
879
+ /// # fn foo(n: usize) {
880
+ /// # use std::mem::align_of;
881
+ /// # unsafe {
882
+ /// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
883
+ /// let ptr = &x[n] as *const u8;
884
+ /// let offset = ptr.align_offset(align_of::<u16>());
885
+ /// if offset < x.len() - n - 1 {
886
+ /// let u16_ptr = ptr.offset(offset as isize) as *const u16;
887
+ /// assert_ne!(*u16_ptr, 500);
888
+ /// } else {
889
+ /// // while the pointer can be aligned via `offset`, it would point
890
+ /// // outside the allocation
891
+ /// }
892
+ /// # } }
893
+ /// ```
894
+ #[ unstable( feature = "align_offset" , issue = "44488" ) ]
895
+ pub fn align_offset ( self , align : usize ) -> usize {
896
+ unsafe {
897
+ intrinsics:: align_offset ( self as * const _ , align)
898
+ }
899
+ }
824
900
}
825
901
826
902
// Equality for pointers
0 commit comments