diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 5cd672b03ffc9..126558e3025d5 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -517,8 +517,10 @@ impl *const T { /// ``` #[stable(feature = "ptr_as_ref", since = "1.9.0")] #[inline] - pub unsafe fn as_ref<'a>(self) -> Option<&'a T> where T: Sized { - if self.is_null() { + pub unsafe fn as_ref<'a>(self) -> Option<&'a T> { + // Check for null via a cast to a thin pointer, so fat pointers are only + // considering their "data" part for null-ness. + if (self as *const u8).is_null() { None } else { Some(&*self) @@ -1148,8 +1150,10 @@ impl *mut T { /// ``` #[stable(feature = "ptr_as_ref", since = "1.9.0")] #[inline] - pub unsafe fn as_ref<'a>(self) -> Option<&'a T> where T: Sized { - if self.is_null() { + pub unsafe fn as_ref<'a>(self) -> Option<&'a T> { + // Check for null via a cast to a thin pointer, so fat pointers are only + // considering their "data" part for null-ness. + if (self as *const u8).is_null() { None } else { Some(&*self) @@ -1272,8 +1276,10 @@ impl *mut T { /// ``` #[stable(feature = "ptr_as_ref", since = "1.9.0")] #[inline] - pub unsafe fn as_mut<'a>(self) -> Option<&'a mut T> where T: Sized { - if self.is_null() { + pub unsafe fn as_mut<'a>(self) -> Option<&'a mut T> { + // Check for null via a cast to a thin pointer, so fat pointers are only + // considering their "data" part for null-ness. + if (self as *mut u8).is_null() { None } else { Some(&mut *self) diff --git a/src/libcore/tests/ptr.rs b/src/libcore/tests/ptr.rs index c2d53840f8f57..e93e9be0cd50b 100644 --- a/src/libcore/tests/ptr.rs +++ b/src/libcore/tests/ptr.rs @@ -85,6 +85,39 @@ fn test_as_ref() { let p = &u as *const isize; assert_eq!(p.as_ref().unwrap(), &2); } + + // Pointers to unsized types -- slices + let s: &mut [u8] = &mut [1, 2, 3]; + let cs: *const [u8] = s; + assert_eq!(cs.as_ref(), Some(&*s)); + + let ms: *mut [u8] = s; + assert_eq!(ms.as_ref(), Some(&*s)); + + let cz: *const [u8] = &[]; + assert_eq!(cz.as_ref(), Some(&[][..])); + + let mz: *mut [u8] = &mut []; + assert_eq!(mz.as_ref(), Some(&[][..])); + + let ncs: *const [u8] = null::<[u8; 3]>(); + assert_eq!(ncs.as_ref(), None); + + let nms: *mut [u8] = null_mut::<[u8; 3]>(); + assert_eq!(nms.as_ref(), None); + + // Pointers to unsized types -- trait objects + let ci: *const ToString = &3; + assert!(ci.as_ref().is_some()); + + let mi: *mut ToString = &mut 3; + assert!(mi.as_ref().is_some()); + + let nci: *const ToString = null::(); + assert!(nci.as_ref().is_none()); + + let nmi: *mut ToString = null_mut::(); + assert!(nmi.as_ref().is_none()); } } @@ -103,6 +136,24 @@ fn test_as_mut() { let p = &mut u as *mut isize; assert!(p.as_mut().unwrap() == &mut 2); } + + // Pointers to unsized types -- slices + let s: &mut [u8] = &mut [1, 2, 3]; + let ms: *mut [u8] = s; + assert_eq!(ms.as_mut(), Some(s)); + + let mz: *mut [u8] = &mut []; + assert_eq!(mz.as_mut(), Some(&mut [][..])); + + let nms: *mut [u8] = null_mut::<[u8; 3]>(); + assert_eq!(nms.as_mut(), None); + + // Pointers to unsized types -- trait objects + let mi: *mut ToString = &mut 3; + assert!(mi.as_mut().is_some()); + + let nmi: *mut ToString = null_mut::(); + assert!(nmi.as_mut().is_none()); } }