]>
Commit | Line | Data |
---|---|---|
b7449926 | 1 | // run-pass |
0bf4aa26 | 2 | #![allow(unused_imports)] |
1a4d82fc JJ |
3 | // A model for how the `Fn` traits could work. You can implement at |
4 | // most one of `Go`, `GoMut`, or `GoOnce`, and then the others follow | |
5 | // automatically. | |
6 | ||
7 | // aux-build:go_trait.rs | |
8 | ||
9 | extern crate go_trait; | |
10 | ||
11 | use go_trait::{Go, GoMut, GoOnce, go, go_mut, go_once}; | |
12 | ||
13 | use std::rc::Rc; | |
14 | use std::cell::Cell; | |
15 | ||
1a4d82fc | 16 | struct SomeGoableThing { |
c34b1796 | 17 | counter: Rc<Cell<isize>> |
1a4d82fc JJ |
18 | } |
19 | ||
20 | impl Go for SomeGoableThing { | |
c34b1796 | 21 | fn go(&self, arg: isize) { |
1a4d82fc JJ |
22 | self.counter.set(self.counter.get() + arg); |
23 | } | |
24 | } | |
25 | ||
1a4d82fc | 26 | struct SomeGoOnceableThing { |
c34b1796 | 27 | counter: Rc<Cell<isize>> |
1a4d82fc JJ |
28 | } |
29 | ||
30 | impl GoOnce for SomeGoOnceableThing { | |
c34b1796 | 31 | fn go_once(self, arg: isize) { |
1a4d82fc JJ |
32 | self.counter.set(self.counter.get() + arg); |
33 | } | |
34 | } | |
35 | ||
1a4d82fc JJ |
36 | fn main() { |
37 | let counter = Rc::new(Cell::new(0)); | |
38 | let mut x = SomeGoableThing { counter: counter.clone() }; | |
39 | ||
40 | go(&x, 10); | |
41 | assert_eq!(counter.get(), 10); | |
42 | ||
43 | go_mut(&mut x, 100); | |
44 | assert_eq!(counter.get(), 110); | |
45 | ||
46 | go_once(x, 1_000); | |
47 | assert_eq!(counter.get(), 1_110); | |
48 | ||
49 | let x = SomeGoOnceableThing { counter: counter.clone() }; | |
50 | ||
51 | go_once(x, 10_000); | |
52 | assert_eq!(counter.get(), 11_110); | |
53 | } |