Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove T: Sized on pointer as_ref() and as_mut() #44932

Merged
merged 5 commits into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 12 additions & 6 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,10 @@ 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 {
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)
Expand Down Expand Up @@ -1150,8 +1152,10 @@ 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 {
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)
Expand Down Expand Up @@ -1274,8 +1278,10 @@ 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 {
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)
Expand Down
51 changes: 51 additions & 0 deletions src/libcore/tests/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<isize>();
assert!(nci.as_ref().is_none());

let nmi: *mut ToString = null_mut::<isize>();
assert!(nmi.as_ref().is_none());
}
}

Expand All @@ -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::<isize>();
assert!(nmi.as_mut().is_none());
}
}

Expand Down