]>
Commit | Line | Data |
---|---|---|
c34b1796 AL |
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 | ||
b7449926 | 11 | // run-pass |
c34b1796 AL |
12 | // Check that a trait is still object-safe (and usable) if it has |
13 | // methods with by-value self so long as they require `Self : Sized`. | |
14 | ||
c34b1796 AL |
15 | |
16 | trait Counter { | |
17 | fn tick(&mut self) -> u32; | |
18 | fn get(self) -> u32 where Self : Sized; | |
19 | } | |
20 | ||
21 | struct CCounter { | |
22 | c: u32 | |
23 | } | |
24 | ||
25 | impl Counter for CCounter { | |
26 | fn tick(&mut self) -> u32 { self.c += 1; self.c } | |
27 | fn get(self) -> u32 where Self : Sized { self.c } | |
28 | } | |
29 | ||
30 | fn tick1<C:Counter>(mut c: C) -> u32 { | |
31 | tick2(&mut c); | |
32 | c.get() | |
33 | } | |
34 | ||
35 | fn tick2(c: &mut Counter) { | |
36 | tick3(c); | |
37 | } | |
38 | ||
39 | fn tick3<C:?Sized+Counter>(c: &mut C) { | |
40 | c.tick(); | |
41 | c.tick(); | |
42 | } | |
43 | ||
44 | fn main() { | |
45 | let mut c = CCounter { c: 0 }; | |
46 | let value = tick1(c); | |
47 | assert_eq!(value, 2); | |
48 | } |