@@ -476,11 +476,6 @@ pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
476
476
impl < T : ?Sized > * const T {
477
477
/// Returns `true` if the pointer is null.
478
478
///
479
- /// Note that unsized types have many possible null pointers, as only the
480
- /// raw data pointer is considered, not their length, vtable, etc.
481
- /// Therefore, two pointers that are null may still not compare equal to
482
- /// each other.
483
- ///
484
479
/// # Examples
485
480
///
486
481
/// Basic usage:
@@ -492,10 +487,8 @@ impl<T: ?Sized> *const T {
492
487
/// ```
493
488
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
494
489
#[ inline]
495
- pub fn is_null ( self ) -> bool {
496
- // Compare via a cast to a thin pointer, so fat pointers are only
497
- // considering their "data" part for null-ness.
498
- ( self as * const u8 ) == null ( )
490
+ pub fn is_null ( self ) -> bool where T : Sized {
491
+ self == null ( )
499
492
}
500
493
501
494
/// Returns `None` if the pointer is null, or else returns a reference to
@@ -527,7 +520,9 @@ impl<T: ?Sized> *const T {
527
520
#[ stable( feature = "ptr_as_ref" , since = "1.9.0" ) ]
528
521
#[ inline]
529
522
pub unsafe fn as_ref < ' a > ( self ) -> Option < & ' a T > {
530
- if self . is_null ( ) {
523
+ // Check for null via a cast to a thin pointer, so fat pointers are only
524
+ // considering their "data" part for null-ness.
525
+ if ( self as * const u8 ) . is_null ( ) {
531
526
None
532
527
} else {
533
528
Some ( & * self )
@@ -1114,11 +1109,6 @@ impl<T: ?Sized> *const T {
1114
1109
impl < T : ?Sized > * mut T {
1115
1110
/// Returns `true` if the pointer is null.
1116
1111
///
1117
- /// Note that unsized types have many possible null pointers, as only the
1118
- /// raw data pointer is considered, not their length, vtable, etc.
1119
- /// Therefore, two pointers that are null may still not compare equal to
1120
- /// each other.
1121
- ///
1122
1112
/// # Examples
1123
1113
///
1124
1114
/// Basic usage:
@@ -1130,10 +1120,8 @@ impl<T: ?Sized> *mut T {
1130
1120
/// ```
1131
1121
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1132
1122
#[ inline]
1133
- pub fn is_null ( self ) -> bool {
1134
- // Compare via a cast to a thin pointer, so fat pointers are only
1135
- // considering their "data" part for null-ness.
1136
- ( self as * mut u8 ) == null_mut ( )
1123
+ pub fn is_null ( self ) -> bool where T : Sized {
1124
+ self == null_mut ( )
1137
1125
}
1138
1126
1139
1127
/// Returns `None` if the pointer is null, or else returns a reference to
@@ -1165,7 +1153,9 @@ impl<T: ?Sized> *mut T {
1165
1153
#[ stable( feature = "ptr_as_ref" , since = "1.9.0" ) ]
1166
1154
#[ inline]
1167
1155
pub unsafe fn as_ref < ' a > ( self ) -> Option < & ' a T > {
1168
- if self . is_null ( ) {
1156
+ // Check for null via a cast to a thin pointer, so fat pointers are only
1157
+ // considering their "data" part for null-ness.
1158
+ if ( self as * const u8 ) . is_null ( ) {
1169
1159
None
1170
1160
} else {
1171
1161
Some ( & * self )
@@ -1289,7 +1279,9 @@ impl<T: ?Sized> *mut T {
1289
1279
#[ stable( feature = "ptr_as_ref" , since = "1.9.0" ) ]
1290
1280
#[ inline]
1291
1281
pub unsafe fn as_mut < ' a > ( self ) -> Option < & ' a mut T > {
1292
- if self . is_null ( ) {
1282
+ // Check for null via a cast to a thin pointer, so fat pointers are only
1283
+ // considering their "data" part for null-ness.
1284
+ if ( self as * mut u8 ) . is_null ( ) {
1293
1285
None
1294
1286
} else {
1295
1287
Some ( & mut * self )
0 commit comments