]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/overloaded-calls-simple.rs
Imported Upstream version 1.0.0~beta.3
[rustc.git] / src / test / run-pass / overloaded-calls-simple.rs
CommitLineData
1a4d82fc
JJ
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
c34b1796
AL
11
12#![feature(lang_items, unboxed_closures, core)]
1a4d82fc
JJ
13
14use std::ops::{Fn, FnMut, FnOnce};
15
16struct S1 {
85aaf69f
SL
17 x: i32,
18 y: i32,
1a4d82fc
JJ
19}
20
85aaf69f 21impl FnMut<(i32,)> for S1 {
85aaf69f 22 extern "rust-call" fn call_mut(&mut self, (z,): (i32,)) -> i32 {
1a4d82fc
JJ
23 self.x * self.y * z
24 }
25}
26
c34b1796
AL
27impl 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
1a4d82fc 34struct S2 {
85aaf69f
SL
35 x: i32,
36 y: i32,
1a4d82fc
JJ
37}
38
85aaf69f 39impl Fn<(i32,)> for S2 {
85aaf69f 40 extern "rust-call" fn call(&self, (z,): (i32,)) -> i32 {
1a4d82fc
JJ
41 self.x * self.y * z
42 }
43}
44
c34b1796
AL
45impl FnMut<(i32,)> for S2 {
46 extern "rust-call" fn call_mut(&mut self, args: (i32,)) -> i32 { self.call(args) }
47}
48
49impl FnOnce<(i32,)> for S2 {
50 type Output = i32;
51 extern "rust-call" fn call_once(self, args: (i32,)) -> i32 { self.call(args) }
52}
53
1a4d82fc 54struct S3 {
85aaf69f
SL
55 x: i32,
56 y: i32,
1a4d82fc
JJ
57}
58
85aaf69f
SL
59impl FnOnce<(i32,i32)> for S3 {
60 type Output = i32;
61 extern "rust-call" fn call_once(self, (z,zz): (i32,i32)) -> i32 {
1a4d82fc
JJ
62 self.x * self.y * z * zz
63 }
64}
65
66fn 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}