]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects-rust-call2.rs
Add 'src/tools/miri/' from commit '75dd959a3a40eb5b4574f8d2e23aa6efbeb33573'
[rust.git] / src / test / run-pass-valgrind / unsized-locals / by-value-trait-objects-rust-call2.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<(String, Box<str>)> for A {
12     type Output = String;
13     extern "rust-call" fn call_once(self, (s1, s2): (String, Box<str>)) -> Self::Output {
14         assert_eq!(&s1 as &str, "s1");
15         assert_eq!(&s2 as &str, "s2");
16         format!("hello")
17     }
18 }
19
20 struct B(i32);
21
22 impl FnOnce<(String, Box<str>)> for B {
23     type Output = String;
24     extern "rust-call" fn call_once(self, (s1, s2): (String, Box<str>)) -> Self::Output {
25         assert_eq!(&s1 as &str, "s1");
26         assert_eq!(&s2 as &str, "s2");
27         format!("{}", self.0)
28     }
29 }
30
31 struct C(String);
32
33 impl FnOnce<(String, Box<str>)> for C {
34     type Output = String;
35     extern "rust-call" fn call_once(self, (s1, s2): (String, Box<str>)) -> Self::Output {
36         assert_eq!(&s1 as &str, "s1");
37         assert_eq!(&s2 as &str, "s2");
38         self.0
39     }
40 }
41
42 struct D(Box<String>);
43
44 impl FnOnce<(String, Box<str>)> for D {
45     type Output = String;
46     extern "rust-call" fn call_once(self, (s1, s2): (String, Box<str>)) -> Self::Output {
47         assert_eq!(&s1 as &str, "s1");
48         assert_eq!(&s2 as &str, "s2");
49         *self.0
50     }
51 }
52
53
54 fn main() {
55     let (s1, s2) = (format!("s1"), format!("s2").into_boxed_str());
56     let x = *(Box::new(A) as Box<dyn FnOnce<(String, Box<str>), Output = String>>);
57     assert_eq!(x.call_once((s1, s2)), format!("hello"));
58     let (s1, s2) = (format!("s1"), format!("s2").into_boxed_str());
59     let x = *(Box::new(B(42)) as Box<dyn FnOnce<(String, Box<str>), Output = String>>);
60     assert_eq!(x.call_once((s1, s2)), format!("42"));
61     let (s1, s2) = (format!("s1"), format!("s2").into_boxed_str());
62     let x = *(Box::new(C(format!("jumping fox")))
63               as Box<dyn FnOnce<(String, Box<str>), Output = String>>);
64     assert_eq!(x.call_once((s1, s2)), format!("jumping fox"));
65     let (s1, s2) = (format!("s1"), format!("s2").into_boxed_str());
66     let x = *(Box::new(D(Box::new(format!("lazy dog"))))
67               as Box<dyn FnOnce<(String, Box<str>), Output = String>>);
68     assert_eq!(x.call_once((s1, s2)), format!("lazy dog"));
69 }