@@ -474,6 +474,11 @@ pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
474474impl < T : ?Sized > * const T {
475475 /// Returns `true` if the pointer is null.
476476 ///
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+ ///
477482 /// # Examples
478483 ///
479484 /// Basic usage:
@@ -485,8 +490,10 @@ impl<T: ?Sized> *const T {
485490 /// ```
486491 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
487492 #[ 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 ( )
490497 }
491498
492499 /// Returns `None` if the pointer is null, or else returns a reference to
@@ -518,9 +525,7 @@ impl<T: ?Sized> *const T {
518525 #[ stable( feature = "ptr_as_ref" , since = "1.9.0" ) ]
519526 #[ inline]
520527 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 ( ) {
524529 None
525530 } else {
526531 Some ( & * self )
@@ -1107,6 +1112,11 @@ impl<T: ?Sized> *const T {
11071112impl < T : ?Sized > * mut T {
11081113 /// Returns `true` if the pointer is null.
11091114 ///
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+ ///
11101120 /// # Examples
11111121 ///
11121122 /// Basic usage:
@@ -1118,8 +1128,10 @@ impl<T: ?Sized> *mut T {
11181128 /// ```
11191129 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
11201130 #[ 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 ( )
11231135 }
11241136
11251137 /// Returns `None` if the pointer is null, or else returns a reference to
@@ -1151,9 +1163,7 @@ impl<T: ?Sized> *mut T {
11511163 #[ stable( feature = "ptr_as_ref" , since = "1.9.0" ) ]
11521164 #[ inline]
11531165 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 ( ) {
11571167 None
11581168 } else {
11591169 Some ( & * self )
@@ -1277,9 +1287,7 @@ impl<T: ?Sized> *mut T {
12771287 #[ stable( feature = "ptr_as_ref" , since = "1.9.0" ) ]
12781288 #[ inline]
12791289 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 ( ) {
12831291 None
12841292 } else {
12851293 Some ( & mut * self )
0 commit comments