]> git.lizzy.rs Git - rust.git/blob - tests/rustdoc/const-generics/const-generics-docs.rs
ade70bbe80d923a1b5f02467eecd13e6e4b5916e
[rust.git] / tests / rustdoc / const-generics / const-generics-docs.rs
1 // edition:2018
2 // aux-build: extern_crate.rs
3 #![crate_name = "foo"]
4
5 extern crate extern_crate;
6 // @has foo/fn.extern_fn.html '//pre[@class="rust item-decl"]' \
7 //      'pub fn extern_fn<const N: usize>() -> impl Iterator<Item = [u8; N]>'
8 pub use extern_crate::extern_fn;
9 // @has foo/struct.ExternTy.html '//pre[@class="rust item-decl"]' \
10 //      'pub struct ExternTy<const N: usize> {'
11 pub use extern_crate::ExternTy;
12 // @has foo/type.TyAlias.html '//pre[@class="rust item-decl"]' \
13 //      'type TyAlias<const N: usize> = ExternTy<N>;'
14 pub use extern_crate::TyAlias;
15 // @has foo/trait.WTrait.html '//pre[@class="rust item-decl"]' \
16 //      'pub trait WTrait<const N: usize, const M: usize>'
17 // @has - '//pre[@class="rust item-decl"]' 'fn hey<const P: usize>() -> usize'
18 pub use extern_crate::WTrait;
19
20 // @has foo/trait.Trait.html '//pre[@class="rust item-decl"]' \
21 //      'pub trait Trait<const N: usize>'
22 // @has - '//*[@id="impl-Trait%3C1%3E-for-u8"]//h3[@class="code-header"]' 'impl Trait<1> for u8'
23 // @has - '//*[@id="impl-Trait%3C2%3E-for-u8"]//h3[@class="code-header"]' 'impl Trait<2> for u8'
24 // @has - '//*[@id="impl-Trait%3C{1%20+%202}%3E-for-u8"]//h3[@class="code-header"]' 'impl Trait<{1 + 2}> for u8'
25 // @has - '//*[@id="impl-Trait%3CN%3E-for-%5Bu8%3B%20N%5D"]//h3[@class="code-header"]' \
26 //      'impl<const N: usize> Trait<N> for [u8; N]'
27 pub trait Trait<const N: usize> {}
28 impl Trait<1> for u8 {}
29 impl Trait<2> for u8 {}
30 impl Trait<{1 + 2}> for u8 {}
31 impl<const N: usize> Trait<N> for [u8; N] {}
32
33 // @has foo/struct.Foo.html '//pre[@class="rust item-decl"]' \
34 //      'pub struct Foo<const N: usize>where u8: Trait<N>'
35 pub struct Foo<const N: usize> where u8: Trait<N>;
36 // @has foo/struct.Bar.html '//pre[@class="rust item-decl"]' 'pub struct Bar<T, const N: usize>(_)'
37 pub struct Bar<T, const N: usize>([T; N]);
38
39 // @has foo/struct.Foo.html '//*[@id="impl-Foo%3CM%3E"]/h3[@class="code-header"]' 'impl<const M: usize> Foo<M>where u8: Trait<M>'
40 impl<const M: usize> Foo<M> where u8: Trait<M> {
41     // @has - '//*[@id="associatedconstant.FOO_ASSOC"]' 'pub const FOO_ASSOC: usize'
42     pub const FOO_ASSOC: usize = M + 13;
43
44     // @has - '//*[@id="method.hey"]' 'pub fn hey<const N: usize>(&self) -> Bar<u8, N>'
45     pub fn hey<const N: usize>(&self) -> Bar<u8, N> {
46         Bar([0; N])
47     }
48 }
49
50 // @has foo/struct.Bar.html '//*[@id="impl-Bar%3Cu8%2C%20M%3E"]/h3[@class="code-header"]' 'impl<const M: usize> Bar<u8, M>'
51 impl<const M: usize> Bar<u8, M> {
52     // @has - '//*[@id="method.hey"]' \
53     //      'pub fn hey<const N: usize>(&self) -> Foo<N>where u8: Trait<N>'
54     pub fn hey<const N: usize>(&self) -> Foo<N> where u8: Trait<N> {
55         Foo
56     }
57 }
58
59 // @has foo/fn.test.html '//pre[@class="rust item-decl"]' \
60 //      'pub fn test<const N: usize>() -> impl Trait<N>where u8: Trait<N>'
61 pub fn test<const N: usize>() -> impl Trait<N> where u8: Trait<N> {
62     2u8
63 }
64
65 // @has foo/fn.a_sink.html '//pre[@class="rust item-decl"]' \
66 //      'pub async fn a_sink<const N: usize>(v: [u8; N]) -> impl Trait<N>'
67 pub async fn a_sink<const N: usize>(v: [u8; N]) -> impl Trait<N> {
68     v
69 }
70
71 // @has foo/fn.b_sink.html '//pre[@class="rust item-decl"]' \
72 //      'pub async fn b_sink<const N: usize>(_: impl Trait<N>)'
73 pub async fn b_sink<const N: usize>(_: impl Trait<N>) {}
74
75 // @has foo/fn.concrete.html '//pre[@class="rust item-decl"]' \
76 //      'pub fn concrete() -> [u8; 22]'
77 pub fn concrete() -> [u8; 3 + std::mem::size_of::<u64>() << 1] {
78     Default::default()
79 }
80
81 // @has foo/type.Faz.html '//pre[@class="rust item-decl"]' \
82 //      'type Faz<const N: usize> = [u8; N];'
83 pub type Faz<const N: usize> = [u8; N];
84 // @has foo/type.Fiz.html '//pre[@class="rust item-decl"]' \
85 //      'type Fiz<const N: usize> = [[u8; N]; 48];'
86 pub type Fiz<const N: usize> = [[u8; N]; 3 << 4];
87
88 macro_rules! define_me {
89     ($t:tt<$q:tt>) => {
90         pub struct $t<const $q: usize>([u8; $q]);
91     }
92 }
93
94 // @has foo/struct.Foz.html '//pre[@class="rust item-decl"]' \
95 //      'pub struct Foz<const N: usize>(_);'
96 define_me!(Foz<N>);
97
98 trait Q {
99     const ASSOC: usize;
100 }
101
102 impl<const N: usize> Q for [u8; N] {
103     const ASSOC: usize = N;
104 }
105
106 // @has foo/fn.q_user.html '//pre[@class="rust item-decl"]' \
107 //      'pub fn q_user() -> [u8; 13]'
108 pub fn q_user() -> [u8; <[u8; 13] as Q>::ASSOC] {
109     [0; <[u8; 13] as Q>::ASSOC]
110 }
111
112 // @has foo/union.Union.html '//pre[@class="rust item-decl"]' \
113 //      'pub union Union<const N: usize>'
114 pub union Union<const N: usize> {
115     // @has - //pre "pub arr: [u8; N]"
116     pub arr: [u8; N],
117     // @has - //pre "pub another_arr: [(); N]"
118     pub another_arr: [(); N],
119 }
120
121 // @has foo/enum.Enum.html '//pre[@class="rust item-decl"]' \
122 //      'pub enum Enum<const N: usize>'
123 pub enum Enum<const N: usize> {
124     // @has - //pre "Variant([u8; N])"
125     Variant([u8; N]),
126     // @has - //pre "EmptyVariant"
127     EmptyVariant,
128 }