]> git.proxmox.com Git - rustc.git/blob - src/test/ui/traits/traits-conditional-model-fn.rs
New upstream version 1.51.0+dfsg1
[rustc.git] / src / test / ui / traits / traits-conditional-model-fn.rs
1 // run-pass
2 #![allow(unused_imports)]
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
16 struct SomeGoableThing {
17 counter: Rc<Cell<isize>>
18 }
19
20 impl Go for SomeGoableThing {
21 fn go(&self, arg: isize) {
22 self.counter.set(self.counter.get() + arg);
23 }
24 }
25
26 struct SomeGoOnceableThing {
27 counter: Rc<Cell<isize>>
28 }
29
30 impl GoOnce for SomeGoOnceableThing {
31 fn go_once(self, arg: isize) {
32 self.counter.set(self.counter.get() + arg);
33 }
34 }
35
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 }