Skip to content

Commit 6f19acc

Browse files
committed
Fix Arc:into_inner behavior
1 parent c3f8f63 commit 6f19acc

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

library/alloc/src/raw_rc.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,20 @@ impl<T, A> RawWeak<T, A> {
598598
{
599599
Self::new_zeroed_in::<STRONG_COUNT>(A::default())
600600
}
601+
602+
unsafe fn assume_init_into_inner<R>(mut self) -> T
603+
where
604+
A: Allocator,
605+
R: RcOps,
606+
{
607+
unsafe {
608+
let result = self.ptr.read();
609+
610+
self.drop_unchecked::<R>();
611+
612+
result
613+
}
614+
}
601615
}
602616

603617
impl<T, A> RawWeak<[T], A> {
@@ -1001,14 +1015,7 @@ impl<T, A> RawRc<T, A> {
10011015
R: RcOps,
10021016
{
10031017
unsafe {
1004-
match self.try_unwrap::<R>() {
1005-
Ok(value) => Some(value),
1006-
Err(mut rc) => {
1007-
rc.drop::<R>();
1008-
1009-
None
1010-
}
1011-
}
1018+
R::dec_strong(&self.strong_count()).then(|| self.weak.assume_init_into_inner::<R>())
10121019
}
10131020
}
10141021

@@ -1019,7 +1026,7 @@ impl<T, A> RawRc<T, A> {
10191026
{
10201027
unsafe {
10211028
if R::lock_strong_count(self.strong_count()) {
1022-
Ok(RawUniqueRc::from_weak(self.weak).into_inner::<R>())
1029+
Ok(self.weak.assume_init_into_inner::<R>())
10231030
} else {
10241031
Err(self)
10251032
}
@@ -1893,20 +1900,6 @@ impl<T, A> RawUniqueRc<T, A> {
18931900
{
18941901
unsafe { Self::from_weak_with_value(RawWeak::new_uninit::<0>(), value) }
18951902
}
1896-
1897-
pub unsafe fn into_inner<R>(mut self) -> T
1898-
where
1899-
A: Allocator,
1900-
R: RcOps,
1901-
{
1902-
unsafe {
1903-
let result = ptr::read(self.as_ref());
1904-
1905-
self.weak.drop_unchecked::<R>();
1906-
1907-
result
1908-
}
1909-
}
19101903
}
19111904

19121905
impl<T, A> AsRef<T> for RawUniqueRc<T, A>

0 commit comments

Comments
 (0)