Skip to content
/ rust Public
forked from rust-lang/rust

Commit 5004e10

Browse files
committed
Add a test for Weak created from UniqueArc::downgrade
1 parent eb094ed commit 5004e10

File tree

1 file changed

+22
-1
lines changed
  • library/alloctests/tests

1 file changed

+22
-1
lines changed

library/alloctests/tests/arc.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::any::Any;
22
use std::cell::{Cell, RefCell};
33
use std::iter::TrustedLen;
4-
use std::sync::{Arc, Weak};
4+
use std::sync::{Arc, UniqueArc, Weak};
55

66
#[test]
77
fn uninhabited() {
@@ -263,6 +263,27 @@ fn make_mut_unsized() {
263263
assert_eq!(*other_data, [110, 20, 30]);
264264
}
265265

266+
#[test]
267+
fn test_unique_arc_weak() {
268+
let data = UniqueArc::new(32);
269+
270+
// Test that `Weak` downgraded from `UniqueArc` cannot be upgraded.
271+
let weak = UniqueArc::downgrade(&data);
272+
assert_eq!(weak.strong_count(), 0);
273+
assert_eq!(weak.weak_count(), 0);
274+
assert!(weak.upgrade().is_none());
275+
276+
// Test that `Weak` can now be upgraded after the `UniqueArc` being converted to `Arc`.
277+
let strong = UniqueArc::into_arc(data);
278+
assert_eq!(*strong, 32);
279+
assert_eq!(weak.strong_count(), 1);
280+
assert_eq!(weak.weak_count(), 1);
281+
let upgraded = weak.upgrade().unwrap();
282+
assert_eq!(*upgraded, 32);
283+
assert_eq!(weak.strong_count(), 2);
284+
assert_eq!(weak.weak_count(), 1);
285+
}
286+
266287
#[allow(unused)]
267288
mod pin_coerce_unsized {
268289
use alloc::sync::{Arc, UniqueArc};

0 commit comments

Comments
 (0)