3 trait Foo<const N: usize> {
4 fn myfun(&self) -> usize;
6 trait Bar<const N: usize> : Foo<N> {}
9 struct FooType<const N: usize>;
10 struct BarType<const N: usize>;
13 impl<const N: usize> Foo<N> for FooType<N> {
14 fn myfun(&self) -> usize { N }
16 impl<const N: usize> Foo<N> for BarType<N> {
17 fn myfun(&self) -> usize { N + 1 }
19 impl<const N: usize> Bar<N> for BarType<N> {}
20 impl Foo<3> for BazType {
21 fn myfun(&self) -> usize { 999 }
23 impl Baz for BazType {}
26 trait Boz: Foo<3> + Foz {}
27 trait Bok<const N: usize>: Foo<N> + Foz {}
31 struct BokType<const N: usize>;
33 impl Foz for FozType {}
35 impl Foz for BozType {}
36 impl Foo<3> for BozType {
37 fn myfun(&self) -> usize { 9999 }
39 impl Boz for BozType {}
41 impl<const N: usize> Foz for BokType<N> {}
42 impl<const N: usize> Foo<N> for BokType<N> {
43 fn myfun(&self) -> usize { N + 2 }
45 impl<const N: usize> Bok<N> for BokType<N> {}
47 fn a<const N: usize>(x: &dyn Foo<N>) -> usize { x.myfun() }
48 fn b(x: &dyn Foo<3>) -> usize { x.myfun() }
49 fn c<T: Bok<N>, const N: usize>(x: T) -> usize { a::<N>(&x) }
50 fn d<T: ?Sized + Foo<3>>(x: &T) -> usize { x.myfun() }
51 fn e(x: &dyn Bar<3>) -> usize { d(x) }
54 let foo = FooType::<3> {};
55 assert!(a(&foo) == 3);
56 assert!(b(&foo) == 3);
57 assert!(d(&foo) == 3);
59 let bar = BarType::<3> {};
60 assert!(a(&bar) == 4);
61 assert!(b(&bar) == 4);
62 assert!(d(&bar) == 4);
63 assert!(e(&bar) == 4);
66 assert!(a(&baz) == 999);
67 assert!(b(&baz) == 999);
68 assert!(d(&baz) == 999);
71 assert!(a(&boz) == 9999);
72 assert!(b(&boz) == 9999);
73 assert!(d(&boz) == 9999);
75 let bok = BokType::<3> {};
76 assert!(a(&bok) == 5);
77 assert!(b(&bok) == 5);
78 assert!(d(&bok) == 5);
79 assert!(c(BokType::<3> {}) == 5);