rust/tests/ui/async-await/in-trait/async-example.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

33 lines
579 B
Rust
Raw Normal View History

//@ check-pass
//@ edition: 2021
trait MyTrait {
2023-09-26 15:20:25 -05:00
#[allow(async_fn_in_trait)]
async fn foo(&self) -> i32;
2023-09-26 15:20:25 -05:00
#[allow(async_fn_in_trait)]
2022-10-07 11:08:56 -05:00
async fn bar(&self) -> i32;
}
impl MyTrait for i32 {
async fn foo(&self) -> i32 {
*self
}
2022-10-07 11:08:56 -05:00
async fn bar(&self) -> i32 {
self.foo().await
}
}
2022-10-07 11:08:56 -05:00
fn main() {
let x = 5;
// Calling from non-async context
let _ = x.foo();
let _ = x.bar();
// Calling from async block in non-async context
async {
let _: i32 = x.foo().await;
let _: i32 = x.bar().await;
2022-10-07 11:08:56 -05:00
};
}