]> git.proxmox.com Git - rustc.git/blob - tests/ui/overloaded/overloaded-calls-simple.rs
New upstream version 1.76.0+dfsg1
[rustc.git] / tests / ui / overloaded / overloaded-calls-simple.rs
1 // run-pass
2
3 #![feature(lang_items, unboxed_closures, fn_traits)]
4
5 struct S1 {
6 x: i32,
7 y: i32,
8 }
9
10 impl FnMut<(i32,)> for S1 {
11 extern "rust-call" fn call_mut(&mut self, (z,): (i32,)) -> i32 {
12 self.x * self.y * z
13 }
14 }
15
16 impl FnOnce<(i32,)> for S1 {
17 type Output = i32;
18 extern "rust-call" fn call_once(mut self, args: (i32,)) -> i32 {
19 self.call_mut(args)
20 }
21 }
22
23 struct S2 {
24 x: i32,
25 y: i32,
26 }
27
28 impl Fn<(i32,)> for S2 {
29 extern "rust-call" fn call(&self, (z,): (i32,)) -> i32 {
30 self.x * self.y * z
31 }
32 }
33
34 impl FnMut<(i32,)> for S2 {
35 extern "rust-call" fn call_mut(&mut self, args: (i32,)) -> i32 { self.call(args) }
36 }
37
38 impl FnOnce<(i32,)> for S2 {
39 type Output = i32;
40 extern "rust-call" fn call_once(self, args: (i32,)) -> i32 { self.call(args) }
41 }
42
43 struct S3 {
44 x: i32,
45 y: i32,
46 }
47
48 impl FnOnce<(i32,i32)> for S3 {
49 type Output = i32;
50 extern "rust-call" fn call_once(self, (z,zz): (i32,i32)) -> i32 {
51 self.x * self.y * z * zz
52 }
53 }
54
55 fn main() {
56 let mut s = S1 {
57 x: 3,
58 y: 3,
59 };
60 let ans = s(3);
61
62 assert_eq!(ans, 27);
63 let s = S2 {
64 x: 3,
65 y: 3,
66 };
67 let ans = s.call((3,));
68 assert_eq!(ans, 27);
69
70 let s = S3 {
71 x: 3,
72 y: 3,
73 };
74 let ans = s(3, 1);
75 assert_eq!(ans, 27);
76 }