]> git.lizzy.rs Git - rust.git/blob - tests/ui/self/object-safety-sized-self-return-Self.rs
Rollup merge of #107091 - clubby789:infer-ftl-missing-dollar, r=compiler-errors
[rust.git] / tests / ui / self / object-safety-sized-self-return-Self.rs
1 // run-pass
2 // Check that a trait is still object-safe (and usable) if it has
3 // methods that return `Self` so long as they require `Self : Sized`.
4
5
6 trait Counter {
7     fn new() -> Self where Self : Sized;
8     fn tick(&mut self) -> u32;
9 }
10
11 struct CCounter {
12     c: u32
13 }
14
15 impl Counter for CCounter {
16     fn new() -> CCounter { CCounter { c: 0 } }
17     fn tick(&mut self) -> u32 { self.c += 1; self.c }
18 }
19
20 fn preticked<C:Counter>() -> C {
21     let mut c: C = Counter::new();
22     tick(&mut c);
23     c
24 }
25
26 fn tick(c: &mut dyn Counter) {
27     tick_generic(c);
28 }
29
30 fn tick_generic<C:?Sized+Counter>(c: &mut C) {
31     c.tick();
32     c.tick();
33 }
34
35 fn main() {
36     let mut c = preticked::<CCounter>();
37     tick(&mut c);
38     assert_eq!(c.tick(), 5);
39 }