]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/overloaded-calls-simple.rs
Imported Upstream version 1.6.0+dfsg1
[rustc.git] / src / test / run-pass / overloaded-calls-simple.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11
12 #![feature(lang_items, unboxed_closures, fn_traits)]
13
14 use std::ops::{Fn, FnMut, FnOnce};
15
16 struct S1 {
17 x: i32,
18 y: i32,
19 }
20
21 impl FnMut<(i32,)> for S1 {
22 extern "rust-call" fn call_mut(&mut self, (z,): (i32,)) -> i32 {
23 self.x * self.y * z
24 }
25 }
26
27 impl FnOnce<(i32,)> for S1 {
28 type Output = i32;
29 extern "rust-call" fn call_once(mut self, args: (i32,)) -> i32 {
30 self.call_mut(args)
31 }
32 }
33
34 struct S2 {
35 x: i32,
36 y: i32,
37 }
38
39 impl Fn<(i32,)> for S2 {
40 extern "rust-call" fn call(&self, (z,): (i32,)) -> i32 {
41 self.x * self.y * z
42 }
43 }
44
45 impl FnMut<(i32,)> for S2 {
46 extern "rust-call" fn call_mut(&mut self, args: (i32,)) -> i32 { self.call(args) }
47 }
48
49 impl FnOnce<(i32,)> for S2 {
50 type Output = i32;
51 extern "rust-call" fn call_once(self, args: (i32,)) -> i32 { self.call(args) }
52 }
53
54 struct S3 {
55 x: i32,
56 y: i32,
57 }
58
59 impl FnOnce<(i32,i32)> for S3 {
60 type Output = i32;
61 extern "rust-call" fn call_once(self, (z,zz): (i32,i32)) -> i32 {
62 self.x * self.y * z * zz
63 }
64 }
65
66 fn main() {
67 let mut s = S1 {
68 x: 3,
69 y: 3,
70 };
71 let ans = s(3);
72
73 assert_eq!(ans, 27);
74 let s = S2 {
75 x: 3,
76 y: 3,
77 };
78 let ans = s.call((3,));
79 assert_eq!(ans, 27);
80
81 let s = S3 {
82 x: 3,
83 y: 3,
84 };
85 let ans = s(3, 1);
86 assert_eq!(ans, 27);
87 }