]> git.proxmox.com Git - rustc.git/blame - 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
CommitLineData
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
9extern crate go_trait;
10
11use go_trait::{Go, GoMut, GoOnce, go, go_mut, go_once};
12
13use std::rc::Rc;
14use std::cell::Cell;
15
1a4d82fc 16struct SomeGoableThing {
c34b1796 17 counter: Rc<Cell<isize>>
1a4d82fc
JJ
18}
19
20impl Go for SomeGoableThing {
c34b1796 21 fn go(&self, arg: isize) {
1a4d82fc
JJ
22 self.counter.set(self.counter.get() + arg);
23 }
24}
25
1a4d82fc 26struct SomeGoOnceableThing {
c34b1796 27 counter: Rc<Cell<isize>>
1a4d82fc
JJ
28}
29
30impl 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
36fn 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}