Skip to content

Commit 31fa73a

Browse files
committed
---
yaml --- r: 137708 b: refs/heads/auto c: 6340c1a h: refs/heads/master v: v3
1 parent 8eecefc commit 31fa73a

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1313
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1414
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1515
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
16-
refs/heads/auto: 389ef6601d18112a7a449eac2e190e4eb061bdf8
16+
refs/heads/auto: 6340c1a3739de1519f7416ac52263b4234965739
1717
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1818
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1919
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)