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