Skip to content

Commit 04c1295

Browse files
committed
centralize and expand dyn-trait (method receiver) tests
1 parent c77b282 commit 04c1295

File tree

3 files changed

+140
-59
lines changed

3 files changed

+140
-59
lines changed

tests/run-pass/box_box_trait.rs

Lines changed: 0 additions & 29 deletions
This file was deleted.

tests/run-pass/dyn-traits.rs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#![feature(unsized_locals)]
2+
3+
fn ref_dyn() {
4+
struct Struct(i32);
5+
6+
trait Trait {
7+
fn method(&self);
8+
}
9+
10+
impl Trait for Struct {
11+
fn method(&self) {
12+
assert_eq!(self.0, 42);
13+
}
14+
}
15+
16+
struct Foo<T: ?Sized>(T);
17+
18+
let y: &dyn Trait = &Struct(42);
19+
y.method();
20+
let x: Foo<Struct> = Foo(Struct(42));
21+
let y: &Foo<dyn Trait> = &x;
22+
y.0.method();
23+
}
24+
25+
fn box_dyn() {
26+
let x: Box<dyn Fn(i32) -> i32> = Box::new(|x| x * 2);
27+
assert_eq!(x(21), 42);
28+
let mut i = 5;
29+
{
30+
let mut x: Box<dyn FnMut()> = Box::new(|| i *= 2);
31+
x(); x();
32+
}
33+
assert_eq!(i, 20);
34+
}
35+
36+
fn box_box_trait() {
37+
struct DroppableStruct;
38+
39+
static mut DROPPED: bool = false;
40+
41+
impl Drop for DroppableStruct {
42+
fn drop(&mut self) {
43+
unsafe { DROPPED = true; }
44+
}
45+
}
46+
47+
trait MyTrait { fn dummy(&self) { } }
48+
impl MyTrait for Box<DroppableStruct> {}
49+
50+
struct Whatever { w: Box<dyn MyTrait+'static> }
51+
52+
impl Whatever {
53+
fn new(w: Box<dyn MyTrait+'static>) -> Whatever {
54+
Whatever { w: w }
55+
}
56+
}
57+
58+
{
59+
let f = Box::new(DroppableStruct);
60+
let a = Whatever::new(Box::new(f) as Box<dyn MyTrait>);
61+
a.w.dummy();
62+
}
63+
assert!(unsafe { DROPPED });
64+
}
65+
66+
fn unsized_dyn() {
67+
pub trait Foo {
68+
fn foo(self) -> String;
69+
}
70+
71+
struct A;
72+
73+
impl Foo for A {
74+
fn foo(self) -> String {
75+
format!("hello")
76+
}
77+
}
78+
79+
let x = *(Box::new(A) as Box<dyn Foo>);
80+
assert_eq!(x.foo(), format!("hello"));
81+
82+
// I'm not sure whether we want this to work
83+
let x = Box::new(A) as Box<dyn Foo>;
84+
assert_eq!(x.foo(), format!("hello"));
85+
}
86+
87+
fn unsized_dyn_autoderef() {
88+
pub trait Foo {
89+
fn foo(self) -> String;
90+
}
91+
92+
impl Foo for [char] {
93+
fn foo(self) -> String {
94+
self.iter().collect()
95+
}
96+
}
97+
98+
impl Foo for str {
99+
fn foo(self) -> String {
100+
self.to_owned()
101+
}
102+
}
103+
104+
impl Foo for dyn FnMut() -> String {
105+
fn foo(mut self) -> String {
106+
self()
107+
}
108+
}
109+
110+
let x = *(Box::new(['h', 'e', 'l', 'l', 'o']) as Box<[char]>);
111+
assert_eq!(&x.foo() as &str, "hello");
112+
113+
let x = Box::new(['h', 'e', 'l', 'l', 'o']) as Box<[char]>;
114+
assert_eq!(&x.foo() as &str, "hello");
115+
116+
let x = "hello".to_owned().into_boxed_str();
117+
assert_eq!(&x.foo() as &str, "hello");
118+
119+
let x = *("hello".to_owned().into_boxed_str());
120+
assert_eq!(&x.foo() as &str, "hello");
121+
122+
let x = "hello".to_owned().into_boxed_str();
123+
assert_eq!(&x.foo() as &str, "hello");
124+
125+
let x = *(Box::new(|| "hello".to_owned()) as Box<dyn FnMut() -> String>);
126+
assert_eq!(&x.foo() as &str, "hello");
127+
128+
let x = Box::new(|| "hello".to_owned()) as Box<dyn FnMut() -> String>;
129+
assert_eq!(&x.foo() as &str, "hello");
130+
}
131+
132+
fn main() {
133+
ref_dyn();
134+
box_dyn();
135+
box_box_trait();
136+
137+
// "exotic" receivers
138+
unsized_dyn();
139+
unsized_dyn_autoderef();
140+
}

tests/run-pass/traits.rs

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)