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