4 #![cfg_attr(full, feature(const_generics))]
5 #![cfg_attr(full, allow(incomplete_features))]
7 trait Foo<const N: usize> {
8 fn myfun(&self) -> usize;
10 trait Bar<const N: usize> : Foo<N> {}
13 struct FooType<const N: usize>;
14 struct BarType<const N: usize>;
17 impl<const N: usize> Foo<N> for FooType<N> {
18 fn myfun(&self) -> usize { N }
20 impl<const N: usize> Foo<N> for BarType<N> {
21 fn myfun(&self) -> usize { N + 1 }
23 impl<const N: usize> Bar<N> for BarType<N> {}
24 impl Foo<3> for BazType {
25 fn myfun(&self) -> usize { 999 }
27 impl Baz for BazType {}
30 trait Boz: Foo<3> + Foz {}
31 trait Bok<const N: usize>: Foo<N> + Foz {}
35 struct BokType<const N: usize>;
37 impl Foz for FozType {}
39 impl Foz for BozType {}
40 impl Foo<3> for BozType {
41 fn myfun(&self) -> usize { 9999 }
43 impl Boz for BozType {}
45 impl<const N: usize> Foz for BokType<N> {}
46 impl<const N: usize> Foo<N> for BokType<N> {
47 fn myfun(&self) -> usize { N + 2 }
49 impl<const N: usize> Bok<N> for BokType<N> {}
51 fn a<const N: usize>(x: &dyn Foo<N>) -> usize { x.myfun() }
52 fn b(x: &dyn Foo<3>) -> usize { x.myfun() }
53 fn c<T: Bok<N>, const N: usize>(x: T) -> usize { a::<N>(&x) }
54 fn d<T: ?Sized + Foo<3>>(x: &T) -> usize { x.myfun() }
55 fn e(x: &dyn Bar<3>) -> usize { d(x) }
58 let foo = FooType::<3> {};
59 assert!(a(&foo) == 3);
60 assert!(b(&foo) == 3);
61 assert!(d(&foo) == 3);
63 let bar = BarType::<3> {};
64 assert!(a(&bar) == 4);
65 assert!(b(&bar) == 4);
66 assert!(d(&bar) == 4);
67 assert!(e(&bar) == 4);
70 assert!(a(&baz) == 999);
71 assert!(b(&baz) == 999);
72 assert!(d(&baz) == 999);
75 assert!(a(&boz) == 9999);
76 assert!(b(&boz) == 9999);
77 assert!(d(&boz) == 9999);
79 let bok = BokType::<3> {};
80 assert!(a(&bok) == 5);
81 assert!(b(&bok) == 5);
82 assert!(d(&bok) == 5);
83 assert!(c(BokType::<3> {}) == 5);