Skip to content

Commit

Permalink
Add an Into<NonNull<T>> impl for Box<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Dec 25, 2018
1 parent dd74e56 commit 98b46ba
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,15 @@ impl From<Box<str>> for Box<[u8]> {
}
}

#[allow(incoherent_fundamental_impls)]
#[unstable(feature = "box_into_raw_non_null", issue = "47336")]
impl<T: ?Sized> Into<NonNull<T>> for Box<T> {
#[inline]
fn into(self) -> NonNull<T> {
Box::into_unique(self).into()
}
}

impl Box<dyn Any> {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
8 changes: 8 additions & 0 deletions src/liballoc/boxed_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,11 @@ fn boxed_slice_from_iter() {
assert_eq!(boxed.len(), 100);
assert_eq!(boxed[7], 7);
}

#[test]
fn to_nonnull() {
let boxed: Box<i32> = Box::from(0);
let ptr: std::ptr::NonNull<i32> = boxed.into();
let deref = unsafe { *ptr.as_ref() };
assert_eq!(deref, 0);
}

0 comments on commit 98b46ba

Please sign in to comment.