Skip to content

Commit 7a786d4

Browse files
committed
Add Into<NonNull<T>> impls for Arc<T>/Rc<T>
1 parent 6f839fb commit 7a786d4

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

src/liballoc/rc.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,14 @@ impl<T> From<Vec<T>> for Rc<[T]> {
11791179
}
11801180
}
11811181

1182+
#[unstable(feature = "rc_into_nonnull", reason = "newly added", issue = "0")]
1183+
impl<T: ?Sized> Into<NonNull<T>> for Rc<T> {
1184+
#[inline]
1185+
fn into(self) -> NonNull<T> {
1186+
unsafe { NonNull::new_unchecked(Rc::into_raw(self) as *mut _) }
1187+
}
1188+
}
1189+
11821190
/// `Weak` is a version of [`Rc`] that holds a non-owning reference to the
11831191
/// managed value. The value is accessed by calling [`upgrade`] on the `Weak`
11841192
/// pointer, which returns an [`Option`]`<`[`Rc`]`<T>>`.

src/liballoc/sync.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,14 @@ impl<T> From<Vec<T>> for Arc<[T]> {
15741574
}
15751575
}
15761576

1577+
#[unstable(feature = "arc_into_nonnull", reason = "newly added", issue = "0")]
1578+
impl<T: ?Sized> Into<NonNull<T>> for Arc<T> {
1579+
#[inline]
1580+
fn into(self) -> NonNull<T> {
1581+
unsafe { NonNull::new_unchecked(Arc::into_raw(self) as *mut _) }
1582+
}
1583+
}
1584+
15771585
#[cfg(test)]
15781586
mod tests {
15791587
use std::boxed::Box;

src/liballoc/tests/arc.rs

+5
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,8 @@ fn eq() {
9595
assert!(!(x != x));
9696
assert_eq!(*x.0.borrow(), 0);
9797
}
98+
99+
#[test]
100+
fn to_nonnull() {
101+
let _: std::ptr::NonNull<i32> = Arc::new(0).into();
102+
}

src/liballoc/tests/rc.rs

+5
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,8 @@ fn eq() {
9595
assert!(!(x != x));
9696
assert_eq!(*x.0.borrow(), 0);
9797
}
98+
99+
#[test]
100+
fn to_nonnull() {
101+
let _: std::ptr::NonNull<i32> = Rc::new(0).into();
102+
}

0 commit comments

Comments
 (0)