/src/test/rustdoc/const-generics/const-generics-docs.rs

https://gitlab.com/rust-lang/rust · Rust · 128 lines · 59 code · 18 blank · 51 comment · 5 complexity · 27f8e68cc871f86f1e41d6efbdf7a043 MD5 · raw file

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