Skip to content

Commit

Permalink
Replace use of transmute with pointer casts in Arc/Rc::as_weak().
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyBanks committed Aug 15, 2022
1 parent b369af8 commit 876236c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,8 +952,9 @@ impl<T: ?Sized> Rc<T> {
#[inline]
#[unstable(feature = "rc_as_weak", issue = "100472")]
#[must_use]
pub const fn as_weak<'a>(this: &'a Self) -> &'a Weak<T> {
unsafe { mem::transmute::<&'a Rc<T>, &'a Weak<T>>(this) }
pub const fn as_weak(this: &Self) -> &Weak<T> {
let weak = this as *const Self as *const Weak<T>;
unsafe { &*weak }
}

/// Gets the number of [`Weak`] pointers to this allocation.
Expand Down
5 changes: 3 additions & 2 deletions library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -979,8 +979,9 @@ impl<T: ?Sized> Arc<T> {
#[inline]
#[unstable(feature = "rc_as_weak", issue = "100472")]
#[must_use]
pub const fn as_weak<'a>(this: &'a Self) -> &'a Weak<T> {
unsafe { mem::transmute::<&'a Arc<T>, &'a Weak<T>>(this) }
pub const fn as_weak(this: &Self) -> &Weak<T> {
let weak = this as *const Self as *const Weak<T>;
unsafe { &*weak }
}

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

0 comments on commit 876236c

Please sign in to comment.