]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects-rust-call.rs
Rollup merge of #64188 - alexcrichton:wasi-cdylib, r=varkor
[rust.git] / src / test / run-pass-valgrind / unsized-locals / by-value-trait-objects-rust-call.rs
1 #![feature(unsized_locals)]
2 #![feature(unboxed_closures)]
3
4 pub trait FnOnce<Args> {
5     type Output;
6     extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
7 }
8
9 struct A;
10
11 impl FnOnce<()> for A {
12     type Output = String;
13     extern "rust-call" fn call_once(self, (): ()) -> Self::Output {
14         format!("hello")
15     }
16 }
17
18 struct B(i32);
19
20 impl FnOnce<()> for B {
21     type Output = String;
22     extern "rust-call" fn call_once(self, (): ()) -> Self::Output {
23         format!("{}", self.0)
24     }
25 }
26
27 struct C(String);
28
29 impl FnOnce<()> for C {
30     type Output = String;
31     extern "rust-call" fn call_once(self, (): ()) -> Self::Output {
32         self.0
33     }
34 }
35
36 struct D(Box<String>);
37
38 impl FnOnce<()> for D {
39     type Output = String;
40     extern "rust-call" fn call_once(self, (): ()) -> Self::Output {
41         *self.0
42     }
43 }
44
45
46 fn main() {
47     let x = *(Box::new(A) as Box<dyn FnOnce<(), Output = String>>);
48     assert_eq!(x.call_once(()), format!("hello"));
49     let x = *(Box::new(B(42)) as Box<dyn FnOnce<(), Output = String>>);
50     assert_eq!(x.call_once(()), format!("42"));
51     let x = *(Box::new(C(format!("jumping fox"))) as Box<dyn FnOnce<(), Output = String>>);
52     assert_eq!(x.call_once(()), format!("jumping fox"));
53     let x = *(Box::new(D(Box::new(format!("lazy dog")))) as Box<dyn FnOnce<(), Output = String>>);
54     assert_eq!(x.call_once(()), format!("lazy dog"));
55 }