]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/dst-coerce-rc.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / dst-coerce-rc.rs
1 // Copyright 2015 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 // Test a very simple custom DST coercion.
12
13 #![feature(core, rc_weak)]
14
15 use std::cell::RefCell;
16 use std::rc::{Rc, Weak};
17
18 trait Baz {
19     fn get(&self) -> i32;
20 }
21
22 impl Baz for i32 {
23     fn get(&self) -> i32 {
24         *self
25     }
26 }
27
28 fn main() {
29     let a: Rc<[i32; 3]> = Rc::new([1, 2, 3]);
30     let b: Rc<[i32]> = a;
31     assert_eq!(b[0], 1);
32     assert_eq!(b[1], 2);
33     assert_eq!(b[2], 3);
34
35     let a: Rc<i32> = Rc::new(42);
36     let b: Rc<Baz> = a.clone();
37     assert_eq!(b.get(), 42);
38
39     let c: Weak<i32> = Rc::downgrade(&a);
40     let d: Weak<Baz> = c.clone();
41
42     let _c = b.clone();
43
44     let a: Rc<RefCell<i32>> = Rc::new(RefCell::new(42));
45     let b: Rc<RefCell<Baz>> = a.clone();
46     assert_eq!(b.borrow().get(), 42);
47     // FIXME
48     let c: Weak<RefCell<Baz>> = Rc::downgrade(&a) as Weak<_>;
49 }