-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use std::time::Duration; | ||
use async_std::task; | ||
|
||
use async_trait::async_trait; | ||
use auto_impl::auto_impl; | ||
|
||
// Note the order of the attributes here: | ||
// `#[async_trait]` must appear first | ||
#[async_trait] | ||
#[auto_impl(&, Box, Arc)] | ||
trait Component { | ||
async fn run(&self); | ||
} | ||
|
||
struct WaitABit(Duration); | ||
|
||
#[async_trait] | ||
impl Component for WaitABit { | ||
async fn run(&self) { | ||
task::sleep(self.0).await; | ||
} | ||
} | ||
|
||
async fn run_async(a: impl Component) { | ||
a.run().await; | ||
} | ||
|
||
#[async_std::main] | ||
async fn main() { | ||
// We can treat our `Box<WaitABit>` as an `impl Component` directly | ||
run_async(Box::new(WaitABit(Duration::from_secs(1)))).await; | ||
} |