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