|
| 1 | +// Copyright 2014 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 | + |
| 11 | +// A model for how the `Fn` traits could work. You can implement at |
| 12 | +// most one of `Go`, `GoMut`, or `GoOnce`, and then the others follow |
| 13 | +// automatically. |
| 14 | + |
| 15 | +use std::rc::Rc; |
| 16 | +use std::cell::Cell; |
| 17 | + |
| 18 | +trait Go { |
| 19 | + fn go(&self, arg: int); |
| 20 | +} |
| 21 | + |
| 22 | +fn go<G:Go>(this: &G, arg: int) { |
| 23 | + this.go(arg) |
| 24 | +} |
| 25 | + |
| 26 | +trait GoMut { |
| 27 | + fn go_mut(&mut self, arg: int); |
| 28 | +} |
| 29 | + |
| 30 | +fn go_mut<G:GoMut>(this: &mut G, arg: int) { |
| 31 | + this.go_mut(arg) |
| 32 | +} |
| 33 | + |
| 34 | +trait GoOnce { |
| 35 | + fn go_once(self, arg: int); |
| 36 | +} |
| 37 | + |
| 38 | +fn go_once<G:GoOnce>(this: G, arg: int) { |
| 39 | + this.go_once(arg) |
| 40 | +} |
| 41 | + |
| 42 | +impl<G> GoMut for G |
| 43 | + where G : Go |
| 44 | +{ |
| 45 | + fn go_mut(&mut self, arg: int) { |
| 46 | + go(&*self, arg) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl<G> GoOnce for G |
| 51 | + where G : GoMut |
| 52 | +{ |
| 53 | + fn go_once(mut self, arg: int) { |
| 54 | + go_mut(&mut self, arg) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/////////////////////////////////////////////////////////////////////////// |
| 59 | + |
| 60 | +struct SomeGoableThing { |
| 61 | + counter: Rc<Cell<int>> |
| 62 | +} |
| 63 | + |
| 64 | +impl Go for SomeGoableThing { |
| 65 | + fn go(&self, arg: int) { |
| 66 | + self.counter.set(self.counter.get() + arg); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/////////////////////////////////////////////////////////////////////////// |
| 71 | + |
| 72 | +struct SomeGoOnceableThing { |
| 73 | + counter: Rc<Cell<int>> |
| 74 | +} |
| 75 | + |
| 76 | +impl GoOnce for SomeGoOnceableThing { |
| 77 | + fn go_once(self, arg: int) { |
| 78 | + self.counter.set(self.counter.get() + arg); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/////////////////////////////////////////////////////////////////////////// |
| 83 | + |
| 84 | +fn main() { |
| 85 | + let counter = Rc::new(Cell::new(0)); |
| 86 | + let mut x = SomeGoableThing { counter: counter.clone() }; |
| 87 | + |
| 88 | + go(&x, 10); |
| 89 | + assert_eq!(counter.get(), 10); |
| 90 | + |
| 91 | + go_mut(&mut x, 100); |
| 92 | + assert_eq!(counter.get(), 110); |
| 93 | + |
| 94 | + go_once(x, 1_000); |
| 95 | + assert_eq!(counter.get(), 1_110); |
| 96 | + |
| 97 | + let x = SomeGoOnceableThing { counter: counter.clone() }; |
| 98 | + |
| 99 | + go_once(x, 10_000); |
| 100 | + assert_eq!(counter.get(), 11_110); |
| 101 | +} |
0 commit comments