@@ -474,16 +474,17 @@ impl<T: ?Sized> *mut T {
474
474
/// This is purely a convenience for casting to a `u8` pointer and
475
475
/// using [offset][pointer::offset] on it. See that method for documentation
476
476
/// and safety requirements.
477
+ ///
478
+ /// For non-`Sized` pointees this operation changes only the data pointer,
479
+ /// leaving the metadata untouched.
477
480
#[ must_use]
478
481
#[ inline( always) ]
479
482
#[ unstable( feature = "pointer_byte_offsets" , issue = "none" ) ]
480
483
#[ 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 {
485
485
// 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 ) )
487
488
}
488
489
489
490
/// Calculates the offset from a pointer using wrapping arithmetic.
@@ -554,15 +555,18 @@ impl<T: ?Sized> *mut T {
554
555
/// This is purely a convenience for casting to a `u8` pointer and
555
556
/// using [wrapping_offset][pointer::wrapping_offset] on it. See that method
556
557
/// for documentation.
558
+ ///
559
+ /// For non-`Sized` pointees this operation changes only the data pointer,
560
+ /// leaving the metadata untouched.
557
561
#[ must_use]
558
562
#[ inline( always) ]
559
563
#[ unstable( feature = "pointer_byte_offsets" , issue = "none" ) ]
560
564
#[ 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
+ )
566
570
}
567
571
568
572
/// Returns `None` if the pointer is null, or else returns a unique reference to
@@ -830,13 +834,13 @@ impl<T: ?Sized> *mut T {
830
834
/// This is purely a convenience for casting to a `u8` pointer and
831
835
/// using [offset_from][pointer::offset_from] on it. See that method for
832
836
/// documentation and safety requirements.
837
+ ///
838
+ /// For non-`Sized` pointees this operation considers only the data pointers,
839
+ /// ignoring the metadata.
833
840
#[ inline( always) ]
834
841
#[ unstable( feature = "pointer_byte_offsets" , issue = "none" ) ]
835
842
#[ 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 {
840
844
// SAFETY: the caller must uphold the safety contract for `offset_from`.
841
845
unsafe { self . cast :: < u8 > ( ) . offset_from ( origin. cast :: < u8 > ( ) ) }
842
846
}
@@ -983,16 +987,17 @@ impl<T: ?Sized> *mut T {
983
987
/// This is purely a convenience for casting to a `u8` pointer and
984
988
/// using [add][pointer::add] on it. See that method for documentation
985
989
/// and safety requirements.
990
+ ///
991
+ /// For non-`Sized` pointees this operation changes only the data pointer,
992
+ /// leaving the metadata untouched.
986
993
#[ must_use]
987
994
#[ inline( always) ]
988
995
#[ unstable( feature = "pointer_byte_offsets" , issue = "none" ) ]
989
996
#[ 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 {
994
998
// 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 ) )
996
1001
}
997
1002
998
1003
/// Calculates the offset from a pointer (convenience for
@@ -1067,16 +1072,17 @@ impl<T: ?Sized> *mut T {
1067
1072
/// This is purely a convenience for casting to a `u8` pointer and
1068
1073
/// using [sub][pointer::sub] on it. See that method for documentation
1069
1074
/// and safety requirements.
1075
+ ///
1076
+ /// For non-`Sized` pointees this operation changes only the data pointer,
1077
+ /// leaving the metadata untouched.
1070
1078
#[ must_use]
1071
1079
#[ inline( always) ]
1072
1080
#[ unstable( feature = "pointer_byte_offsets" , issue = "none" ) ]
1073
1081
#[ 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 {
1078
1083
// 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 ) )
1080
1086
}
1081
1087
1082
1088
/// Calculates the offset from a pointer using wrapping arithmetic.
@@ -1148,15 +1154,15 @@ impl<T: ?Sized> *mut T {
1148
1154
///
1149
1155
/// This is purely a convenience for casting to a `u8` pointer and
1150
1156
/// 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.
1151
1160
#[ must_use]
1152
1161
#[ inline( always) ]
1153
1162
#[ unstable( feature = "pointer_byte_offsets" , issue = "none" ) ]
1154
1163
#[ 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 ) )
1160
1166
}
1161
1167
1162
1168
/// Calculates the offset from a pointer using wrapping arithmetic.
@@ -1228,15 +1234,15 @@ impl<T: ?Sized> *mut T {
1228
1234
///
1229
1235
/// This is purely a convenience for casting to a `u8` pointer and
1230
1236
/// 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.
1231
1240
#[ must_use]
1232
1241
#[ inline( always) ]
1233
1242
#[ unstable( feature = "pointer_byte_offsets" , issue = "none" ) ]
1234
1243
#[ 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 ) )
1240
1246
}
1241
1247
1242
1248
/// Reads the value from `self` without moving it. This leaves the
@@ -1569,21 +1575,22 @@ impl<T: ?Sized> *mut T {
1569
1575
1570
1576
/// Returns whether the pointer is aligned to `align`.
1571
1577
///
1578
+ /// For non-`Sized` pointees this operation considers only the data pointer,
1579
+ /// ignoring the metadata.
1580
+ ///
1572
1581
/// # Panics
1573
1582
///
1574
1583
/// The function panics if `align` is not a power-of-two (this includes 0).
1575
1584
#[ must_use]
1576
1585
#[ inline]
1577
1586
#[ 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 {
1582
1588
if !align. is_power_of_two ( ) {
1583
1589
panic ! ( "is_aligned_to: align is not a power-of-two" ) ;
1584
1590
}
1585
1591
1586
- self . addr ( ) % align == 0
1592
+ // Cast is needed for `T: !Sized`
1593
+ self . cast :: < u8 > ( ) . addr ( ) % align == 0
1587
1594
}
1588
1595
}
1589
1596
0 commit comments