@@ -474,16 +474,17 @@ impl<T: ?Sized> *mut T {
474474 /// This is purely a convenience for casting to a `u8` pointer and
475475 /// using [offset][pointer::offset] on it. See that method for documentation
476476 /// and safety requirements.
477+ ///
478+ /// For non-`Sized` pointees this operation changes only the data pointer,
479+ /// leaving the metadata untouched.
477480 #[ must_use]
478481 #[ inline( always) ]
479482 #[ unstable( feature = "pointer_byte_offsets" , issue = "none" ) ]
480483 #[ rustc_const_unstable( feature = "const_pointer_byte_offsets" , issue = "none" ) ]
481- pub const unsafe fn byte_offset ( self , count : isize ) -> Self
482- where
483- T : Sized ,
484- {
484+ pub const unsafe fn byte_offset ( self , count : isize ) -> Self {
485485 // SAFETY: the caller must uphold the safety contract for `offset`.
486- unsafe { self . cast :: < u8 > ( ) . offset ( count) . cast :: < T > ( ) }
486+ let this = unsafe { self . cast :: < u8 > ( ) . offset ( count) . cast :: < ( ) > ( ) } ;
487+ from_raw_parts_mut :: < T > ( this, metadata ( self ) )
487488 }
488489
489490 /// Calculates the offset from a pointer using wrapping arithmetic.
@@ -554,15 +555,18 @@ impl<T: ?Sized> *mut T {
554555 /// This is purely a convenience for casting to a `u8` pointer and
555556 /// using [wrapping_offset][pointer::wrapping_offset] on it. See that method
556557 /// for documentation.
558+ ///
559+ /// For non-`Sized` pointees this operation changes only the data pointer,
560+ /// leaving the metadata untouched.
557561 #[ must_use]
558562 #[ inline( always) ]
559563 #[ unstable( feature = "pointer_byte_offsets" , issue = "none" ) ]
560564 #[ rustc_const_unstable( feature = "const_pointer_byte_offsets" , issue = "none" ) ]
561- pub const fn wrapping_byte_offset ( self , count : isize ) -> Self
562- where
563- T : Sized ,
564- {
565- self . cast :: < u8 > ( ) . wrapping_offset ( count ) . cast :: < T > ( )
565+ pub const fn wrapping_byte_offset ( self , count : isize ) -> Self {
566+ from_raw_parts_mut :: < T > (
567+ self . cast :: < u8 > ( ) . wrapping_offset ( count ) . cast :: < ( ) > ( ) ,
568+ metadata ( self ) ,
569+ )
566570 }
567571
568572 /// Returns `None` if the pointer is null, or else returns a unique reference to
@@ -830,13 +834,13 @@ impl<T: ?Sized> *mut T {
830834 /// This is purely a convenience for casting to a `u8` pointer and
831835 /// using [offset_from][pointer::offset_from] on it. See that method for
832836 /// documentation and safety requirements.
837+ ///
838+ /// For non-`Sized` pointees this operation considers only the data pointers,
839+ /// ignoring the metadata.
833840 #[ inline( always) ]
834841 #[ unstable( feature = "pointer_byte_offsets" , issue = "none" ) ]
835842 #[ rustc_const_unstable( feature = "const_pointer_byte_offsets" , issue = "none" ) ]
836- pub const unsafe fn byte_offset_from ( self , origin : * const T ) -> isize
837- where
838- T : Sized ,
839- {
843+ pub const unsafe fn byte_offset_from ( self , origin : * const T ) -> isize {
840844 // SAFETY: the caller must uphold the safety contract for `offset_from`.
841845 unsafe { self . cast :: < u8 > ( ) . offset_from ( origin. cast :: < u8 > ( ) ) }
842846 }
@@ -983,16 +987,17 @@ impl<T: ?Sized> *mut T {
983987 /// This is purely a convenience for casting to a `u8` pointer and
984988 /// using [add][pointer::add] on it. See that method for documentation
985989 /// and safety requirements.
990+ ///
991+ /// For non-`Sized` pointees this operation changes only the data pointer,
992+ /// leaving the metadata untouched.
986993 #[ must_use]
987994 #[ inline( always) ]
988995 #[ unstable( feature = "pointer_byte_offsets" , issue = "none" ) ]
989996 #[ rustc_const_unstable( feature = "const_pointer_byte_offsets" , issue = "none" ) ]
990- pub const unsafe fn byte_add ( self , count : usize ) -> Self
991- where
992- T : Sized ,
993- {
997+ pub const unsafe fn byte_add ( self , count : usize ) -> Self {
994998 // SAFETY: the caller must uphold the safety contract for `add`.
995- unsafe { self . cast :: < u8 > ( ) . add ( count) . cast :: < T > ( ) }
999+ let this = unsafe { self . cast :: < u8 > ( ) . add ( count) . cast :: < ( ) > ( ) } ;
1000+ from_raw_parts_mut :: < T > ( this, metadata ( self ) )
9961001 }
9971002
9981003 /// Calculates the offset from a pointer (convenience for
@@ -1067,16 +1072,17 @@ impl<T: ?Sized> *mut T {
10671072 /// This is purely a convenience for casting to a `u8` pointer and
10681073 /// using [sub][pointer::sub] on it. See that method for documentation
10691074 /// and safety requirements.
1075+ ///
1076+ /// For non-`Sized` pointees this operation changes only the data pointer,
1077+ /// leaving the metadata untouched.
10701078 #[ must_use]
10711079 #[ inline( always) ]
10721080 #[ unstable( feature = "pointer_byte_offsets" , issue = "none" ) ]
10731081 #[ rustc_const_unstable( feature = "const_pointer_byte_offsets" , issue = "none" ) ]
1074- pub const unsafe fn byte_sub ( self , count : usize ) -> Self
1075- where
1076- T : Sized ,
1077- {
1082+ pub const unsafe fn byte_sub ( self , count : usize ) -> Self {
10781083 // SAFETY: the caller must uphold the safety contract for `sub`.
1079- unsafe { self . cast :: < u8 > ( ) . sub ( count) . cast :: < T > ( ) }
1084+ let this = unsafe { self . cast :: < u8 > ( ) . sub ( count) . cast :: < ( ) > ( ) } ;
1085+ from_raw_parts_mut :: < T > ( this, metadata ( self ) )
10801086 }
10811087
10821088 /// Calculates the offset from a pointer using wrapping arithmetic.
@@ -1148,15 +1154,15 @@ impl<T: ?Sized> *mut T {
11481154 ///
11491155 /// This is purely a convenience for casting to a `u8` pointer and
11501156 /// using [wrapping_add][pointer::wrapping_add] on it. See that method for documentation.
1157+ ///
1158+ /// For non-`Sized` pointees this operation changes only the data pointer,
1159+ /// leaving the metadata untouched.
11511160 #[ must_use]
11521161 #[ inline( always) ]
11531162 #[ unstable( feature = "pointer_byte_offsets" , issue = "none" ) ]
11541163 #[ rustc_const_unstable( feature = "const_pointer_byte_offsets" , issue = "none" ) ]
1155- pub const fn wrapping_byte_add ( self , count : usize ) -> Self
1156- where
1157- T : Sized ,
1158- {
1159- self . cast :: < u8 > ( ) . wrapping_add ( count) . cast :: < T > ( )
1164+ pub const fn wrapping_byte_add ( self , count : usize ) -> Self {
1165+ from_raw_parts_mut :: < T > ( self . cast :: < u8 > ( ) . wrapping_add ( count) . cast :: < ( ) > ( ) , metadata ( self ) )
11601166 }
11611167
11621168 /// Calculates the offset from a pointer using wrapping arithmetic.
@@ -1228,15 +1234,15 @@ impl<T: ?Sized> *mut T {
12281234 ///
12291235 /// This is purely a convenience for casting to a `u8` pointer and
12301236 /// using [wrapping_sub][pointer::wrapping_sub] on it. See that method for documentation.
1237+ ///
1238+ /// For non-`Sized` pointees this operation changes only the data pointer,
1239+ /// leaving the metadata untouched.
12311240 #[ must_use]
12321241 #[ inline( always) ]
12331242 #[ unstable( feature = "pointer_byte_offsets" , issue = "none" ) ]
12341243 #[ rustc_const_unstable( feature = "const_pointer_byte_offsets" , issue = "none" ) ]
1235- pub const fn wrapping_byte_sub ( self , count : usize ) -> Self
1236- where
1237- T : Sized ,
1238- {
1239- self . cast :: < u8 > ( ) . wrapping_sub ( count) . cast :: < T > ( )
1244+ pub const fn wrapping_byte_sub ( self , count : usize ) -> Self {
1245+ from_raw_parts_mut :: < T > ( self . cast :: < u8 > ( ) . wrapping_sub ( count) . cast :: < ( ) > ( ) , metadata ( self ) )
12401246 }
12411247
12421248 /// Reads the value from `self` without moving it. This leaves the
@@ -1569,21 +1575,22 @@ impl<T: ?Sized> *mut T {
15691575
15701576 /// Returns whether the pointer is aligned to `align`.
15711577 ///
1578+ /// For non-`Sized` pointees this operation considers only the data pointer,
1579+ /// ignoring the metadata.
1580+ ///
15721581 /// # Panics
15731582 ///
15741583 /// The function panics if `align` is not a power-of-two (this includes 0).
15751584 #[ must_use]
15761585 #[ inline]
15771586 #[ unstable( feature = "pointer_is_aligned" , issue = "none" ) ]
1578- pub fn is_aligned_to ( self , align : usize ) -> bool
1579- where
1580- T : Sized ,
1581- {
1587+ pub fn is_aligned_to ( self , align : usize ) -> bool {
15821588 if !align. is_power_of_two ( ) {
15831589 panic ! ( "is_aligned_to: align is not a power-of-two" ) ;
15841590 }
15851591
1586- self . addr ( ) % align == 0
1592+ // Cast is needed for `T: !Sized`
1593+ self . cast :: < u8 > ( ) . addr ( ) % align == 0
15871594 }
15881595}
15891596
0 commit comments