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

Add function Arc/Rc::as_weak(…) to convert &Arc/Rc<T> to &Weak<T> #1

Closed
wants to merge 2 commits into from
Closed
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
27 changes: 27 additions & 0 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ struct RcBox<T: ?Sized> {
#[cfg_attr(not(test), rustc_diagnostic_item = "Rc")]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_insignificant_dtor]
#[repr(transparent)]
pub struct Rc<T: ?Sized> {
ptr: NonNull<RcBox<T>>,
phantom: PhantomData<RcBox<T>>,
Expand Down Expand Up @@ -933,6 +934,31 @@ impl<T: ?Sized> Rc<T> {
Weak { ptr: this.ptr }
}

/// Convert a reference to an [`Rc`] into a reference to a [`Weak`] of the same type,
/// without modifying the inner reference counts.
///
/// # Examples
///
/// ```
/// #![feature(rc_as_weak)]
///
/// use std::rc::{Rc, Weak};
///
/// let five: &Rc<i32> = &Rc::new(5);
///
/// let weak_five: &Rc<i32> = Rc::as_weak(five);
///
/// ```
#[inline]
#[unstable(feature = "rc_as_weak", issue = "none")]
#[must_use]
pub fn as_weak<'a>(this: &'a Self) -> &'a Weak<T>
where
T: Sized,
{
unsafe { mem::transmute::<&'a Rc<T>, &'a Weak<T>>(this) }
}

/// Gets the number of [`Weak`] pointers to this allocation.
///
/// # Examples
Expand Down Expand Up @@ -2143,6 +2169,7 @@ impl<T, I: iter::TrustedLen<Item = T>> ToRcSlice<T> for I {
///
/// [`upgrade`]: Weak::upgrade
#[stable(feature = "rc_weak", since = "1.4.0")]
#[repr(transparent)]
pub struct Weak<T: ?Sized> {
// This is a `NonNull` to allow optimizing the size of this type in enums,
// but it is not necessarily a valid pointer.
Expand Down
27 changes: 27 additions & 0 deletions library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ macro_rules! acquire {
/// [rc_examples]: crate::rc#examples
#[cfg_attr(not(test), rustc_diagnostic_item = "Arc")]
#[stable(feature = "rust1", since = "1.0.0")]
#[repr(transparent)]
pub struct Arc<T: ?Sized> {
ptr: NonNull<ArcInner<T>>,
phantom: PhantomData<ArcInner<T>>,
Expand Down Expand Up @@ -282,6 +283,7 @@ impl<T: ?Sized> Arc<T> {
///
/// [`upgrade`]: Weak::upgrade
#[stable(feature = "arc_weak", since = "1.4.0")]
#[repr(transparent)]
pub struct Weak<T: ?Sized> {
// This is a `NonNull` to allow optimizing the size of this type in enums,
// but it is not necessarily a valid pointer.
Expand Down Expand Up @@ -959,6 +961,31 @@ impl<T: ?Sized> Arc<T> {
}
}

/// Convert a reference to an [`Arc`] into a reference to a [`Weak`] of the same type,
/// without modifying the inner reference counts.
///
/// # Examples
///
/// ```
/// #![feature(arc_as_weak)]
///
/// use std::sync::{Arc, Weak};
///
/// let five: &Arc<i32> = &Arc::new(5);
///
/// let weak_five: &Weak<i32> = Arc::as_weak(five);
///
/// ```
#[inline]
#[unstable(feature = "arc_as_weak", issue = "none")]
#[must_use]
pub fn as_weak<'a>(this: &'a Self) -> &'a Weak<T>
where
T: Sized,
{
unsafe { mem::transmute::<&'a Arc<T>, &'a Weak<T>>(this) }
}

/// Gets the number of [`Weak`] pointers to this allocation.
///
/// # Safety
Expand Down