Skip to content

Commit 876236c

Browse files
committed
Replace use of transmute with pointer casts in Arc/Rc::as_weak().
per #100472 (comment)
1 parent b369af8 commit 876236c

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

library/alloc/src/rc.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -952,8 +952,9 @@ impl<T: ?Sized> Rc<T> {
952952
#[inline]
953953
#[unstable(feature = "rc_as_weak", issue = "100472")]
954954
#[must_use]
955-
pub const fn as_weak<'a>(this: &'a Self) -> &'a Weak<T> {
956-
unsafe { mem::transmute::<&'a Rc<T>, &'a Weak<T>>(this) }
955+
pub const fn as_weak(this: &Self) -> &Weak<T> {
956+
let weak = this as *const Self as *const Weak<T>;
957+
unsafe { &*weak }
957958
}
958959

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

library/alloc/src/sync.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -979,8 +979,9 @@ impl<T: ?Sized> Arc<T> {
979979
#[inline]
980980
#[unstable(feature = "rc_as_weak", issue = "100472")]
981981
#[must_use]
982-
pub const fn as_weak<'a>(this: &'a Self) -> &'a Weak<T> {
983-
unsafe { mem::transmute::<&'a Arc<T>, &'a Weak<T>>(this) }
982+
pub const fn as_weak(this: &Self) -> &Weak<T> {
983+
let weak = this as *const Self as *const Weak<T>;
984+
unsafe { &*weak }
984985
}
985986

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

0 commit comments

Comments
 (0)