]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/pass/dyn-arbitrary-self.rs
Rollup merge of #103084 - inquisitivecrystal:control-flow, r=scottmcm
[rust.git] / src / tools / miri / tests / pass / dyn-arbitrary-self.rs
1 #![feature(arbitrary_self_types, unsize, coerce_unsized, dispatch_from_dyn)]
2 #![feature(rustc_attrs)]
3
4 fn pin_box_dyn() {
5     use std::pin::Pin;
6
7     trait Foo {
8         fn bar(self: Pin<&mut Self>) -> bool;
9     }
10
11     impl Foo for &'static str {
12         fn bar(self: Pin<&mut Self>) -> bool {
13             true
14         }
15     }
16
17     let mut test: Pin<Box<dyn Foo>> = Box::pin("foo");
18     test.as_mut().bar();
19 }
20
21 fn stdlib_pointers() {
22     use std::{pin::Pin, rc::Rc, sync::Arc};
23
24     trait Trait {
25         fn by_rc(self: Rc<Self>) -> i64;
26         fn by_arc(self: Arc<Self>) -> i64;
27         fn by_pin_mut(self: Pin<&mut Self>) -> i64;
28         fn by_pin_box(self: Pin<Box<Self>>) -> i64;
29     }
30
31     impl Trait for i64 {
32         fn by_rc(self: Rc<Self>) -> i64 {
33             *self
34         }
35         fn by_arc(self: Arc<Self>) -> i64 {
36             *self
37         }
38         fn by_pin_mut(self: Pin<&mut Self>) -> i64 {
39             *self
40         }
41         fn by_pin_box(self: Pin<Box<Self>>) -> i64 {
42             *self
43         }
44     }
45
46     let rc = Rc::new(1i64) as Rc<dyn Trait>;
47     assert_eq!(1, rc.by_rc());
48
49     let arc = Arc::new(2i64) as Arc<dyn Trait>;
50     assert_eq!(2, arc.by_arc());
51
52     let mut value = 3i64;
53     let pin_mut = Pin::new(&mut value) as Pin<&mut dyn Trait>;
54     assert_eq!(3, pin_mut.by_pin_mut());
55
56     let pin_box = Into::<Pin<Box<i64>>>::into(Box::new(4i64)) as Pin<Box<dyn Trait>>;
57     assert_eq!(4, pin_box.by_pin_box());
58 }
59
60 fn pointers_and_wrappers() {
61     use std::{
62         marker::Unsize,
63         ops::{CoerceUnsized, Deref, DispatchFromDyn},
64     };
65
66     struct Ptr<T: ?Sized>(Box<T>);
67
68     impl<T: ?Sized> Deref for Ptr<T> {
69         type Target = T;
70
71         fn deref(&self) -> &T {
72             &*self.0
73         }
74     }
75
76     impl<T: Unsize<U> + ?Sized, U: ?Sized> CoerceUnsized<Ptr<U>> for Ptr<T> {}
77     impl<T: Unsize<U> + ?Sized, U: ?Sized> DispatchFromDyn<Ptr<U>> for Ptr<T> {}
78
79     struct Wrapper<T: ?Sized>(T);
80
81     impl<T: ?Sized> Deref for Wrapper<T> {
82         type Target = T;
83
84         fn deref(&self) -> &T {
85             &self.0
86         }
87     }
88
89     impl<T: CoerceUnsized<U>, U> CoerceUnsized<Wrapper<U>> for Wrapper<T> {}
90     impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<Wrapper<U>> for Wrapper<T> {}
91
92     trait Trait {
93         // This method isn't object-safe yet. Unsized by-value `self` is object-safe (but not callable
94         // without unsized_locals), but wrappers arond `Self` currently are not.
95         // FIXME (mikeyhew) uncomment this when unsized rvalues object-safety is implemented
96         // fn wrapper(self: Wrapper<Self>) -> i32;
97         fn ptr_wrapper(self: Ptr<Wrapper<Self>>) -> i32;
98         fn wrapper_ptr(self: Wrapper<Ptr<Self>>) -> i32;
99         fn wrapper_ptr_wrapper(self: Wrapper<Ptr<Wrapper<Self>>>) -> i32;
100     }
101
102     impl Trait for i32 {
103         fn ptr_wrapper(self: Ptr<Wrapper<Self>>) -> i32 {
104             **self
105         }
106         fn wrapper_ptr(self: Wrapper<Ptr<Self>>) -> i32 {
107             **self
108         }
109         fn wrapper_ptr_wrapper(self: Wrapper<Ptr<Wrapper<Self>>>) -> i32 {
110             ***self
111         }
112     }
113
114     let pw = Ptr(Box::new(Wrapper(5))) as Ptr<Wrapper<dyn Trait>>;
115     assert_eq!(pw.ptr_wrapper(), 5);
116
117     let wp = Wrapper(Ptr(Box::new(6))) as Wrapper<Ptr<dyn Trait>>;
118     assert_eq!(wp.wrapper_ptr(), 6);
119
120     let wpw = Wrapper(Ptr(Box::new(Wrapper(7)))) as Wrapper<Ptr<Wrapper<dyn Trait>>>;
121     assert_eq!(wpw.wrapper_ptr_wrapper(), 7);
122 }
123
124 fn main() {
125     pin_box_dyn();
126     stdlib_pointers();
127     pointers_and_wrappers();
128 }