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