Skip to content

Commit 0453d5f

Browse files
Cleaned up alloc::sync::Weak Clone implementation
Since both return points (tail and early return) return the same expression and the only difference is whether inner is available, the code that does the atomic operations and checks on inner was moved into the if body and the only return is at the tail. Original comments preserved.
1 parent 3a539c0 commit 0453d5f

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

library/alloc/src/sync.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -2917,20 +2917,17 @@ impl<T: ?Sized, A: Allocator + Clone> Clone for Weak<T, A> {
29172917
/// ```
29182918
#[inline]
29192919
fn clone(&self) -> Weak<T, A> {
2920-
let inner = if let Some(inner) = self.inner() {
2921-
inner
2922-
} else {
2923-
return Weak { ptr: self.ptr, alloc: self.alloc.clone() };
2924-
};
2925-
// See comments in Arc::clone() for why this is relaxed. This can use a
2926-
// fetch_add (ignoring the lock) because the weak count is only locked
2927-
// where are *no other* weak pointers in existence. (So we can't be
2928-
// running this code in that case).
2929-
let old_size = inner.weak.fetch_add(1, Relaxed);
2930-
2931-
// See comments in Arc::clone() for why we do this (for mem::forget).
2932-
if old_size > MAX_REFCOUNT {
2933-
abort();
2920+
if let Some(inner) = self.inner() {
2921+
// See comments in Arc::clone() for why this is relaxed. This can use a
2922+
// fetch_add (ignoring the lock) because the weak count is only locked
2923+
// where are *no other* weak pointers in existence. (So we can't be
2924+
// running this code in that case).
2925+
let old_size = inner.weak.fetch_add(1, Relaxed);
2926+
2927+
// See comments in Arc::clone() for why we do this (for mem::forget).
2928+
if old_size > MAX_REFCOUNT {
2929+
abort();
2930+
}
29342931
}
29352932

29362933
Weak { ptr: self.ptr, alloc: self.alloc.clone() }

0 commit comments

Comments
 (0)