Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/libcore/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ macro_rules! impl_zeroable_for_pointer_types {
unsafe impl<T: ?Sized> Zeroable for $Ptr {
#[inline]
fn is_zero(&self) -> bool {
// Cast because `is_null` is only available on thin pointers
(*self as *mut u8).is_null()
(*self).is_null()
}
}
)+
Expand Down
18 changes: 11 additions & 7 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,10 @@ impl<T: ?Sized> *const T {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn is_null(self) -> bool where T: Sized {
self == null()
pub fn is_null(self) -> bool {
// Compare via a cast to a thin pointer, so fat pointers are only
// considering their "data" part for null-ness.
(self as *const u8) == null()
}

/// Returns `None` if the pointer is null, or else returns a reference to
Expand Down Expand Up @@ -519,7 +521,7 @@ impl<T: ?Sized> *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 {
pub unsafe fn as_ref<'a>(self) -> Option<&'a T> {
if self.is_null() {
None
} else {
Expand Down Expand Up @@ -1118,8 +1120,10 @@ impl<T: ?Sized> *mut T {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn is_null(self) -> bool where T: Sized {
self == null_mut()
pub fn is_null(self) -> bool {
// Compare via a cast to a thin pointer, so fat pointers are only
// considering their "data" part for null-ness.
(self as *mut u8) == null_mut()
}

/// Returns `None` if the pointer is null, or else returns a reference to
Expand Down Expand Up @@ -1150,7 +1154,7 @@ impl<T: ?Sized> *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 {
pub unsafe fn as_ref<'a>(self) -> Option<&'a T> {
if self.is_null() {
None
} else {
Expand Down Expand Up @@ -1274,7 +1278,7 @@ impl<T: ?Sized> *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 {
pub unsafe fn as_mut<'a>(self) -> Option<&'a mut T> {
if self.is_null() {
None
} else {
Expand Down
54 changes: 54 additions & 0 deletions src/libcore/tests/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

use core::ptr::*;
use core::slice;
use core::cell::RefCell;

#[test]
Expand Down Expand Up @@ -62,6 +63,28 @@ fn test_is_null() {

let mq = unsafe { mp.offset(1) };
assert!(!mq.is_null());

// Pointers to unsized types
let s: &mut [u8] = &mut [1, 2, 3];
let cs: *const [u8] = s;
assert!(!cs.is_null());

let ms: *mut [u8] = s;
assert!(!ms.is_null());

let cz: *const [u8] = &[];
assert!(!cz.is_null());

let mz: *mut [u8] = &mut [];
assert!(!mz.is_null());

unsafe {
let ncs: *const [u8] = slice::from_raw_parts(null(), 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is UB (null in a &[u8]) so unfortunately can begin to fail when the compiler gets smarter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any better way to get a null unsized pointer? Or are they impossible to be null in practice? If the latter, we could just drop those parts of the tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose I could implement a local Repr hack for the tests, similar to slice::from_raw_parts but without ever calling it a &.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on how much you hate transmute, you could do let ncs: *const [u8] = unsafe { mem::transmute((0usize, 0usize)) };

Copy link
Member Author

@cuviper cuviper Sep 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went ahead and added a transmute with Repr, which I think is a little safer than a tuple because it can be #[repr(C)]. This is just for testing anyway, so whatever we do isn't going to affect anyone in the wild.

assert!(ncs.is_null());

let nms: *mut [u8] = slice::from_raw_parts_mut(null_mut(), 0);
assert!(nms.is_null());
}
}

#[test]
Expand All @@ -85,6 +108,26 @@ fn test_as_ref() {
let p = &u as *const isize;
assert_eq!(p.as_ref().unwrap(), &2);
}

// Pointers to unsized types
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] = slice::from_raw_parts(null(), 0);
assert_eq!(ncs.as_ref(), None);

let nms: *mut [u8] = slice::from_raw_parts_mut(null_mut(), 0);
assert_eq!(nms.as_ref(), None);
}
}

Expand All @@ -103,6 +146,17 @@ fn test_as_mut() {
let p = &mut u as *mut isize;
assert!(p.as_mut().unwrap() == &mut 2);
}

// Pointers to unsized types
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] = slice::from_raw_parts_mut(null_mut(), 0);
assert_eq!(nms.as_mut(), None);
}
}

Expand Down