1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
fn ref_box_dyn() {
struct Struct(i32);
trait Trait {
fn method(&self);
fn box_method(self: Box<Self>);
}
impl Trait for Struct {
fn method(&self) {
assert_eq!(self.0, 42);
}
fn box_method(self: Box<Self>) {
assert_eq!(self.0, 7);
}
}
struct Foo<T: ?Sized>(T);
let y: &dyn Trait = &Struct(42);
y.method();
let x: Foo<Struct> = Foo(Struct(42));
let y: &Foo<dyn Trait> = &x;
y.0.method();
let y: Box<dyn Trait> = Box::new(Struct(42));
y.method();
let y = &y;
y.method();
let y: Box<dyn Trait> = Box::new(Struct(7));
y.box_method();
}
fn box_box_trait() {
struct DroppableStruct;
static mut DROPPED: bool = false;
impl Drop for DroppableStruct {
fn drop(&mut self) {
unsafe {
DROPPED = true;
}
}
}
trait MyTrait {
fn dummy(&self) {}
}
impl MyTrait for Box<DroppableStruct> {}
struct Whatever {
w: Box<dyn MyTrait + 'static>,
}
impl Whatever {
fn new(w: Box<dyn MyTrait + 'static>) -> Whatever {
Whatever { w: w }
}
}
{
let f = Box::new(DroppableStruct);
let a = Whatever::new(Box::new(f) as Box<dyn MyTrait>);
a.w.dummy();
}
assert!(unsafe { DROPPED });
}
// Disabled for now: unsized locals are not supported,
// their current MIR encoding is just not great.
/*
fn unsized_dyn() {
pub trait Foo {
fn foo(self) -> String;
}
struct A;
impl Foo for A {
fn foo(self) -> String {
format!("hello")
}
}
let x = *(Box::new(A) as Box<dyn Foo>);
assert_eq!(x.foo(), format!("hello"));
// I'm not sure whether we want this to work
let x = Box::new(A) as Box<dyn Foo>;
assert_eq!(x.foo(), format!("hello"));
}
fn unsized_dyn_autoderef() {
pub trait Foo {
fn foo(self) -> String;
}
impl Foo for [char] {
fn foo(self) -> String {
self.iter().collect()
}
}
impl Foo for str {
fn foo(self) -> String {
self.to_owned()
}
}
impl Foo for dyn FnMut() -> String {
fn foo(mut self) -> String {
self()
}
}
let x = *(Box::new(['h', 'e', 'l', 'l', 'o']) as Box<[char]>);
assert_eq!(&x.foo() as &str, "hello");
let x = Box::new(['h', 'e', 'l', 'l', 'o']) as Box<[char]>;
assert_eq!(&x.foo() as &str, "hello");
let x = "hello".to_owned().into_boxed_str();
assert_eq!(&x.foo() as &str, "hello");
let x = *("hello".to_owned().into_boxed_str());
assert_eq!(&x.foo() as &str, "hello");
let x = "hello".to_owned().into_boxed_str();
assert_eq!(&x.foo() as &str, "hello");
let x = *(Box::new(|| "hello".to_owned()) as Box<dyn FnMut() -> String>);
assert_eq!(&x.foo() as &str, "hello");
let x = Box::new(|| "hello".to_owned()) as Box<dyn FnMut() -> String>;
assert_eq!(&x.foo() as &str, "hello");
}
*/
fn vtable_ptr_eq() {
use std::{fmt, ptr};
// We don't always get the same vtable when casting this to a wide pointer.
let x = &2;
let x_wide = x as &dyn fmt::Display;
assert!((0..256).any(|_| !ptr::eq(x as &dyn fmt::Display, x_wide)));
}
fn main() {
ref_box_dyn();
box_box_trait();
vtable_ptr_eq();
}
|