Skip to content

Commit 98b46ba

Browse files
committed
Add an Into<NonNull<T>> impl for Box<T>
1 parent dd74e56 commit 98b46ba

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/liballoc/boxed.rs

+9
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,15 @@ impl From<Box<str>> for Box<[u8]> {
479479
}
480480
}
481481

482+
#[allow(incoherent_fundamental_impls)]
483+
#[unstable(feature = "box_into_raw_non_null", issue = "47336")]
484+
impl<T: ?Sized> Into<NonNull<T>> for Box<T> {
485+
#[inline]
486+
fn into(self) -> NonNull<T> {
487+
Box::into_unique(self).into()
488+
}
489+
}
490+
482491
impl Box<dyn Any> {
483492
#[inline]
484493
#[stable(feature = "rust1", since = "1.0.0")]

src/liballoc/boxed_test.rs

+8
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,11 @@ fn boxed_slice_from_iter() {
148148
assert_eq!(boxed.len(), 100);
149149
assert_eq!(boxed[7], 7);
150150
}
151+
152+
#[test]
153+
fn to_nonnull() {
154+
let boxed: Box<i32> = Box::from(0);
155+
let ptr: std::ptr::NonNull<i32> = boxed.into();
156+
let deref = unsafe { *ptr.as_ref() };
157+
assert_eq!(deref, 0);
158+
}

0 commit comments

Comments
 (0)