]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/object-safety-sized-self-return-Self.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / object-safety-sized-self-return-Self.rs
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 // Check that a trait is still object-safe (and usable) if it has
12 // methods that return `Self` so long as they require `Self : Sized`.
13
14
15 trait Counter {
16     fn new() -> Self where Self : Sized;
17     fn tick(&mut self) -> u32;
18 }
19
20 struct CCounter {
21     c: u32
22 }
23
24 impl Counter for CCounter {
25     fn new() -> CCounter { CCounter { c: 0 } }
26     fn tick(&mut self) -> u32 { self.c += 1; self.c }
27 }
28
29 fn preticked<C:Counter>() -> C {
30     let mut c: C = Counter::new();
31     tick(&mut c);
32     c
33 }
34
35 fn tick(c: &mut Counter) {
36     tick_generic(c);
37 }
38
39 fn tick_generic<C:?Sized+Counter>(c: &mut C) {
40     c.tick();
41     c.tick();
42 }
43
44 fn main() {
45     let mut c = preticked::<CCounter>();
46     tick(&mut c);
47     assert_eq!(c.tick(), 5);
48 }