]>
Commit | Line | Data |
---|---|---|
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 | ||
476ff2be | 11 | #![feature(fn_traits, unboxed_closures)] |
1a4d82fc JJ |
12 | |
13 | use std::ops::{Fn, FnMut, FnOnce}; | |
14 | ||
15 | struct SFn { | |
16 | x: isize, | |
17 | y: isize, | |
18 | } | |
19 | ||
85aaf69f | 20 | impl Fn<(isize,)> for SFn { |
1a4d82fc JJ |
21 | extern "rust-call" fn call(&self, (z,): (isize,)) -> isize { |
22 | self.x * self.y * z | |
23 | } | |
24 | } | |
25 | ||
c34b1796 AL |
26 | impl FnMut<(isize,)> for SFn { |
27 | extern "rust-call" fn call_mut(&mut self, args: (isize,)) -> isize { self.call(args) } | |
28 | } | |
29 | ||
30 | impl FnOnce<(isize,)> for SFn { | |
31 | type Output = isize; | |
32 | extern "rust-call" fn call_once(self, args: (isize,)) -> isize { self.call(args) } | |
33 | } | |
34 | ||
1a4d82fc JJ |
35 | struct SFnMut { |
36 | x: isize, | |
37 | y: isize, | |
38 | } | |
39 | ||
85aaf69f | 40 | impl FnMut<(isize,)> for SFnMut { |
1a4d82fc JJ |
41 | extern "rust-call" fn call_mut(&mut self, (z,): (isize,)) -> isize { |
42 | self.x * self.y * z | |
43 | } | |
44 | } | |
45 | ||
c34b1796 AL |
46 | impl FnOnce<(isize,)> for SFnMut { |
47 | type Output = isize; | |
48 | extern "rust-call" fn call_once(mut self, args: (isize,)) -> isize { self.call_mut(args) } | |
49 | } | |
50 | ||
1a4d82fc JJ |
51 | struct SFnOnce { |
52 | x: String, | |
53 | } | |
54 | ||
85aaf69f SL |
55 | impl FnOnce<(String,)> for SFnOnce { |
56 | type Output = usize; | |
57 | ||
1a4d82fc JJ |
58 | extern "rust-call" fn call_once(self, (z,): (String,)) -> usize { |
59 | self.x.len() + z.len() | |
60 | } | |
61 | } | |
62 | ||
63 | fn f() { | |
64 | let mut s = SFn { | |
65 | x: 1, | |
66 | y: 2, | |
67 | }; | |
68 | let sp = &mut s; | |
69 | s(3); //~ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable | |
1a4d82fc JJ |
70 | } |
71 | ||
72 | fn g() { | |
73 | let s = SFnMut { | |
74 | x: 1, | |
75 | y: 2, | |
76 | }; | |
77 | s(3); //~ ERROR cannot borrow immutable local variable `s` as mutable | |
78 | } | |
79 | ||
80 | fn h() { | |
81 | let s = SFnOnce { | |
82 | x: "hello".to_string(), | |
83 | }; | |
84 | s(" world".to_string()); | |
85 | s(" world".to_string()); //~ ERROR use of moved value: `s` | |
86 | } | |
87 | ||
88 | fn main() {} |