]> git.lizzy.rs Git - rust.git/blob - tests/rustdoc/async-fn.rs
rustdoc: use a newline instead of `<br>` to format code headers
[rust.git] / tests / rustdoc / async-fn.rs
1 // edition:2018
2 // @has async_fn/fn.foo.html '//pre[@class="rust item-decl"]' 'pub async fn foo() -> Option<Foo>'
3 pub async fn foo() -> Option<Foo> {
4     None
5 }
6
7 // @has async_fn/fn.bar.html '//pre[@class="rust item-decl"]' 'pub async fn bar(a: i32, b: i32) -> i32'
8 pub async fn bar(a: i32, b: i32) -> i32 {
9     0
10 }
11
12 // @has async_fn/fn.baz.html '//pre[@class="rust item-decl"]' 'pub async fn baz<T>(a: T) -> T'
13 pub async fn baz<T>(a: T) -> T {
14     a
15 }
16
17 // @has async_fn/fn.qux.html '//pre[@class="rust item-decl"]' 'pub async unsafe fn qux() -> char'
18 pub async unsafe fn qux() -> char {
19     '⚠'
20 }
21
22 // @has async_fn/fn.mut_args.html '//pre[@class="rust item-decl"]' 'pub async fn mut_args(a: usize)'
23 pub async fn mut_args(mut a: usize) {}
24
25 // @has async_fn/fn.mut_ref.html '//pre[@class="rust item-decl"]' 'pub async fn mut_ref(x: i32)'
26 pub async fn mut_ref(ref mut x: i32) {}
27
28 trait Bar {}
29
30 impl Bar for () {}
31
32 // @has async_fn/fn.quux.html '//pre[@class="rust item-decl"]' 'pub async fn quux() -> impl Bar'
33 pub async fn quux() -> impl Bar {
34     ()
35 }
36
37 // @has async_fn/struct.Foo.html
38 // @matches - '//h4[@class="code-header"]' 'pub async fn f\(\)$'
39 // @matches - '//h4[@class="code-header"]' 'pub async unsafe fn g\(\)$'
40 // @matches - '//h4[@class="code-header"]' 'pub async fn mut_self\(self, first: usize\)$'
41 pub struct Foo;
42
43 impl Foo {
44     pub async fn f() {}
45     pub async unsafe fn g() {}
46     pub async fn mut_self(mut self, mut first: usize) {}
47 }
48
49 pub trait Pattern<'a> {}
50
51 pub trait Trait<const N: usize> {}
52 // @has async_fn/fn.const_generics.html
53 // @has - '//pre[@class="rust item-decl"]' 'pub async fn const_generics<const N: usize>(_: impl Trait<N>)'
54 pub async fn const_generics<const N: usize>(_: impl Trait<N>) {}
55
56 // test that elided lifetimes are properly elided and not displayed as `'_`
57 // regression test for #63037
58 // @has async_fn/fn.elided.html
59 // @has - '//pre[@class="rust item-decl"]' 'pub async fn elided(foo: &str) -> &str'
60 pub async fn elided(foo: &str) -> &str {}
61 // This should really be shown as written, but for implementation reasons it's difficult.
62 // See `impl Clean for TyKind::Ref`.
63 // @has async_fn/fn.user_elided.html
64 // @has - '//pre[@class="rust item-decl"]' 'pub async fn user_elided(foo: &str) -> &str'
65 pub async fn user_elided(foo: &'_ str) -> &str {}
66 // @has async_fn/fn.static_trait.html
67 // @has - '//pre[@class="rust item-decl"]' 'pub async fn static_trait(foo: &str) -> Box<dyn Bar>'
68 pub async fn static_trait(foo: &str) -> Box<dyn Bar> {}
69 // @has async_fn/fn.lifetime_for_trait.html
70 // @has - '//pre[@class="rust item-decl"]' "pub async fn lifetime_for_trait(foo: &str) -> Box<dyn Bar + '_>"
71 pub async fn lifetime_for_trait(foo: &str) -> Box<dyn Bar + '_> {}
72 // @has async_fn/fn.elided_in_input_trait.html
73 // @has - '//pre[@class="rust item-decl"]' "pub async fn elided_in_input_trait(t: impl Pattern<'_>)"
74 pub async fn elided_in_input_trait(t: impl Pattern<'_>) {}
75
76 struct AsyncFdReadyGuard<'a, T> { x: &'a T }
77
78 impl Foo {
79     // @has async_fn/struct.Foo.html
80     // @has - '//*[@class="method"]' 'pub async fn complicated_lifetimes( &self, context: &impl Bar ) -> impl Iterator<Item = &usize>'
81     pub async fn complicated_lifetimes(&self, context: &impl Bar) -> impl Iterator<Item = &usize> {}
82     // taken from `tokio` as an example of a method that was particularly bad before
83     // @has - '//*[@class="method"]' "pub async fn readable<T>(&self) -> Result<AsyncFdReadyGuard<'_, T>, ()>"
84     pub async fn readable<T>(&self) -> Result<AsyncFdReadyGuard<'_, T>, ()> {}
85     // @has - '//*[@class="method"]' "pub async fn mut_self(&mut self)"
86     pub async fn mut_self(&mut self) {}
87 }
88
89 // test named lifetimes, just in case
90 // @has async_fn/fn.named.html
91 // @has - '//pre[@class="rust item-decl"]' "pub async fn named<'a, 'b>(foo: &'a str) -> &'b str"
92 pub async fn named<'a, 'b>(foo: &'a str) -> &'b str {}
93 // @has async_fn/fn.named_trait.html
94 // @has - '//pre[@class="rust item-decl"]' "pub async fn named_trait<'a, 'b>(foo: impl Pattern<'a>) -> impl Pattern<'b>"
95 pub async fn named_trait<'a, 'b>(foo: impl Pattern<'a>) -> impl Pattern<'b> {}