]> git.lizzy.rs Git - rust.git/blob - src/liballoc/tests/rc.rs
Rollup merge of #52769 - sinkuu:stray_test, r=alexcrichton
[rust.git] / src / liballoc / tests / rc.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::any::Any;
12 use std::rc::{Rc, Weak};
13
14 #[test]
15 fn uninhabited() {
16     enum Void {}
17     let mut a = Weak::<Void>::new();
18     a = a.clone();
19     assert!(a.upgrade().is_none());
20
21     let mut a: Weak<dyn Any> = a;  // Unsizing
22     a = a.clone();
23     assert!(a.upgrade().is_none());
24 }
25
26 #[test]
27 fn slice() {
28     let a: Rc<[u32; 3]> = Rc::new([3, 2, 1]);
29     let a: Rc<[u32]> = a;  // Unsizing
30     let b: Rc<[u32]> = Rc::from(&[3, 2, 1][..]);  // Conversion
31     assert_eq!(a, b);
32
33     // Exercise is_dangling() with a DST
34     let mut a = Rc::downgrade(&a);
35     a = a.clone();
36     assert!(a.upgrade().is_some());
37 }
38
39 #[test]
40 fn trait_object() {
41     let a: Rc<u32> = Rc::new(4);
42     let a: Rc<dyn Any> = a;  // Unsizing
43
44     // Exercise is_dangling() with a DST
45     let mut a = Rc::downgrade(&a);
46     a = a.clone();
47     assert!(a.upgrade().is_some());
48
49     let mut b = Weak::<u32>::new();
50     b = b.clone();
51     assert!(b.upgrade().is_none());
52     let mut b: Weak<dyn Any> = b;  // Unsizing
53     b = b.clone();
54     assert!(b.upgrade().is_none());
55 }