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