4 use std
::cell
::RefCell
;
6 use std
::convert
::{From, TryInto}
;
8 use std
::option
::Option
::{self, None, Some}
;
9 use std
::result
::Result
::{Err, Ok}
;
13 let x
= Rc
::new(RefCell
::new(5));
16 assert_eq
!(*y
.borrow(), 20);
26 fn test_simple_clone() {
34 fn test_destructor() {
35 let x
: Rc
<Box
<_
>> = Rc
::new(box 5);
42 let y
= Rc
::downgrade(&x
);
43 assert
!(y
.upgrade().is_some());
49 let y
= Rc
::downgrade(&x
);
51 assert
!(y
.upgrade().is_none());
55 fn weak_self_cyclic() {
57 x
: RefCell
<Option
<Weak
<Cycle
>>>,
60 let a
= Rc
::new(Cycle { x: RefCell::new(None) }
);
61 let b
= Rc
::downgrade(&a
.clone());
62 *a
.x
.borrow_mut() = Some(b
);
64 // hopefully we don't double-free (or leak)...
70 assert
!(Rc
::is_unique(&x
));
72 assert
!(!Rc
::is_unique(&x
));
74 assert
!(Rc
::is_unique(&x
));
75 let w
= Rc
::downgrade(&x
);
76 assert
!(!Rc
::is_unique(&x
));
78 assert
!(Rc
::is_unique(&x
));
82 fn test_strong_count() {
84 assert
!(Rc
::strong_count(&a
) == 1);
85 let w
= Rc
::downgrade(&a
);
86 assert
!(Rc
::strong_count(&a
) == 1);
87 let b
= w
.upgrade().expect("upgrade of live rc failed");
88 assert
!(Rc
::strong_count(&b
) == 2);
89 assert
!(Rc
::strong_count(&a
) == 2);
92 assert
!(Rc
::strong_count(&b
) == 1);
94 assert
!(Rc
::strong_count(&b
) == 2);
95 assert
!(Rc
::strong_count(&c
) == 2);
99 fn test_weak_count() {
101 assert
!(Rc
::strong_count(&a
) == 1);
102 assert
!(Rc
::weak_count(&a
) == 0);
103 let w
= Rc
::downgrade(&a
);
104 assert
!(Rc
::strong_count(&a
) == 1);
105 assert
!(Rc
::weak_count(&a
) == 1);
107 assert
!(Rc
::strong_count(&a
) == 1);
108 assert
!(Rc
::weak_count(&a
) == 0);
110 assert
!(Rc
::strong_count(&a
) == 2);
111 assert
!(Rc
::weak_count(&a
) == 0);
117 assert_eq
!(Weak
::weak_count(&Weak
::<u64>::new()), 0);
118 assert_eq
!(Weak
::strong_count(&Weak
::<u64>::new()), 0);
121 let w
= Rc
::downgrade(&a
);
122 assert_eq
!(Weak
::strong_count(&w
), 1);
123 assert_eq
!(Weak
::weak_count(&w
), 1);
125 assert_eq
!(Weak
::strong_count(&w
), 1);
126 assert_eq
!(Weak
::weak_count(&w
), 2);
127 assert_eq
!(Weak
::strong_count(&w2
), 1);
128 assert_eq
!(Weak
::weak_count(&w2
), 2);
130 assert_eq
!(Weak
::strong_count(&w2
), 1);
131 assert_eq
!(Weak
::weak_count(&w2
), 1);
133 assert_eq
!(Weak
::strong_count(&w2
), 2);
134 assert_eq
!(Weak
::weak_count(&w2
), 1);
137 assert_eq
!(Weak
::strong_count(&w2
), 0);
138 assert_eq
!(Weak
::weak_count(&w2
), 0);
145 assert_eq
!(Rc
::try_unwrap(x
), Ok(3));
148 assert_eq
!(Rc
::try_unwrap(x
), Err(Rc
::new(4)));
150 let _w
= Rc
::downgrade(&x
);
151 assert_eq
!(Rc
::try_unwrap(x
), Ok(5));
156 let x
= Rc
::new(box "hello");
159 let x_ptr
= Rc
::into_raw(x
);
162 assert_eq
!(**x_ptr
, "hello");
164 let x
= Rc
::from_raw(x_ptr
);
165 assert_eq
!(**x
, "hello");
167 assert_eq
!(Rc
::try_unwrap(x
).map(|x
| *x
), Ok("hello"));
172 fn test_into_from_raw_unsized() {
173 use std
::fmt
::Display
;
174 use std
::string
::ToString
;
176 let rc
: Rc
<str> = Rc
::from("foo");
178 let ptr
= Rc
::into_raw(rc
.clone());
179 let rc2
= unsafe { Rc::from_raw(ptr) }
;
181 assert_eq
!(unsafe { &*ptr }
, "foo");
184 let rc
: Rc
<dyn Display
> = Rc
::new(123);
186 let ptr
= Rc
::into_raw(rc
.clone());
187 let rc2
= unsafe { Rc::from_raw(ptr) }
;
189 assert_eq
!(unsafe { &*ptr }
.to_string(), "123");
190 assert_eq
!(rc2
.to_string(), "123");
195 let mut x
= Rc
::new(3);
196 *Rc
::get_mut(&mut x
).unwrap() = 4;
199 assert
!(Rc
::get_mut(&mut x
).is_none());
201 assert
!(Rc
::get_mut(&mut x
).is_some());
202 let _w
= Rc
::downgrade(&x
);
203 assert
!(Rc
::get_mut(&mut x
).is_none());
207 fn test_cowrc_clone_make_unique() {
208 let mut cow0
= Rc
::new(75);
209 let mut cow1
= cow0
.clone();
210 let mut cow2
= cow1
.clone();
212 assert
!(75 == *Rc
::make_mut(&mut cow0
));
213 assert
!(75 == *Rc
::make_mut(&mut cow1
));
214 assert
!(75 == *Rc
::make_mut(&mut cow2
));
216 *Rc
::make_mut(&mut cow0
) += 1;
217 *Rc
::make_mut(&mut cow1
) += 2;
218 *Rc
::make_mut(&mut cow2
) += 3;
220 assert
!(76 == *cow0
);
221 assert
!(77 == *cow1
);
222 assert
!(78 == *cow2
);
224 // none should point to the same backing memory
225 assert
!(*cow0
!= *cow1
);
226 assert
!(*cow0
!= *cow2
);
227 assert
!(*cow1
!= *cow2
);
231 fn test_cowrc_clone_unique2() {
232 let mut cow0
= Rc
::new(75);
233 let cow1
= cow0
.clone();
234 let cow2
= cow1
.clone();
236 assert
!(75 == *cow0
);
237 assert
!(75 == *cow1
);
238 assert
!(75 == *cow2
);
240 *Rc
::make_mut(&mut cow0
) += 1;
242 assert
!(76 == *cow0
);
243 assert
!(75 == *cow1
);
244 assert
!(75 == *cow2
);
246 // cow1 and cow2 should share the same contents
247 // cow0 should have a unique reference
248 assert
!(*cow0
!= *cow1
);
249 assert
!(*cow0
!= *cow2
);
250 assert
!(*cow1
== *cow2
);
254 fn test_cowrc_clone_weak() {
255 let mut cow0
= Rc
::new(75);
256 let cow1_weak
= Rc
::downgrade(&cow0
);
258 assert
!(75 == *cow0
);
259 assert
!(75 == *cow1_weak
.upgrade().unwrap());
261 *Rc
::make_mut(&mut cow0
) += 1;
263 assert
!(76 == *cow0
);
264 assert
!(cow1_weak
.upgrade().is_none());
269 let foo
= Rc
::new(75);
270 assert_eq
!(format
!("{:?}", foo
), "75");
275 let foo
: Rc
<[i32]> = Rc
::new([1, 2, 3]);
276 assert_eq
!(foo
, foo
.clone());
280 fn test_from_owned() {
282 let foo_rc
= Rc
::from(foo
);
283 assert
!(123 == *foo_rc
);
288 let foo
: Weak
<usize> = Weak
::new();
289 assert
!(foo
.upgrade().is_none());
294 let five
= Rc
::new(5);
295 let same_five
= five
.clone();
296 let other_five
= Rc
::new(5);
298 assert
!(Rc
::ptr_eq(&five
, &same_five
));
299 assert
!(!Rc
::ptr_eq(&five
, &other_five
));
304 let r
: Rc
<str> = Rc
::from("foo");
306 assert_eq
!(&r
[..], "foo");
310 fn test_copy_from_slice() {
311 let s
: &[u32] = &[1, 2, 3];
312 let r
: Rc
<[u32]> = Rc
::from(s
);
314 assert_eq
!(&r
[..], [1, 2, 3]);
318 fn test_clone_from_slice() {
319 #[derive(Clone, Debug, Eq, PartialEq)]
322 let s
: &[X
] = &[X(1), X(2), X(3)];
323 let r
: Rc
<[X
]> = Rc
::from(s
);
325 assert_eq
!(&r
[..], s
);
330 fn test_clone_from_slice_panic() {
331 use std
::string
::{String, ToString}
;
333 struct Fail(u32, String
);
335 impl Clone
for Fail
{
336 fn clone(&self) -> Fail
{
340 Fail(self.0, self.1.clone())
345 &[Fail(0, "foo".to_string()), Fail(1, "bar".to_string()), Fail(2, "baz".to_string())];
347 // Should panic, but not cause memory corruption
348 let _r
: Rc
<[Fail
]> = Rc
::from(s
);
353 let b
: Box
<u32> = box 123;
354 let r
: Rc
<u32> = Rc
::from(b
);
360 fn test_from_box_str() {
361 use std
::string
::String
;
363 let s
= String
::from("foo").into_boxed_str();
364 let r
: Rc
<str> = Rc
::from(s
);
366 assert_eq
!(&r
[..], "foo");
370 fn test_from_box_slice() {
371 let s
= vec
![1, 2, 3].into_boxed_slice();
372 let r
: Rc
<[u32]> = Rc
::from(s
);
374 assert_eq
!(&r
[..], [1, 2, 3]);
378 fn test_from_box_trait() {
379 use std
::fmt
::Display
;
380 use std
::string
::ToString
;
382 let b
: Box
<dyn Display
> = box 123;
383 let r
: Rc
<dyn Display
> = Rc
::from(b
);
385 assert_eq
!(r
.to_string(), "123");
389 fn test_from_box_trait_zero_sized() {
392 let b
: Box
<dyn Debug
> = box ();
393 let r
: Rc
<dyn Debug
> = Rc
::from(b
);
395 assert_eq
!(format
!("{:?}", r
), "()");
400 let v
= vec
![1, 2, 3];
401 let r
: Rc
<[u32]> = Rc
::from(v
);
403 assert_eq
!(&r
[..], [1, 2, 3]);
410 let r1
: Rc
<dyn Any
> = Rc
::new(i32::MAX
);
411 let r2
: Rc
<dyn Any
> = Rc
::new("abc");
413 assert
!(r1
.clone().downcast
::<u32>().is_err());
415 let r1i32
= r1
.downcast
::<i32>();
416 assert
!(r1i32
.is_ok());
417 assert_eq
!(r1i32
.unwrap(), Rc
::new(i32::MAX
));
419 assert
!(r2
.clone().downcast
::<i32>().is_err());
421 let r2str
= r2
.downcast
::<&'
static str>();
422 assert
!(r2str
.is_ok());
423 assert_eq
!(r2str
.unwrap(), Rc
::new("abc"));
427 fn test_array_from_slice() {
428 let v
= vec
![1, 2, 3];
429 let r
: Rc
<[u32]> = Rc
::from(v
);
431 let a
: Result
<Rc
<[u32; 3]>, _
> = r
.clone().try_into();
434 let a
: Result
<Rc
<[u32; 2]>, _
> = r
.clone().try_into();