diff options
| author | Eric Holk <ericholk@microsoft.com> | 2022-11-17 16:03:55 -0800 |
|---|---|---|
| committer | Eric Holk <ericholk@microsoft.com> | 2022-11-21 10:13:57 -0800 |
| commit | 1a913a634721d3a69f3b41a638d605812aca8d5a (patch) | |
| tree | 60f80d05d4692153b6060b2c9becd1f6d49219d7 | |
| parent | 7fe6f36224e92db6fbde952e0b7e50863161f6ee (diff) | |
| download | rust-1a913a634721d3a69f3b41a638d605812aca8d5a.tar.gz rust-1a913a634721d3a69f3b41a638d605812aca8d5a.zip | |
Add a test case for async dyn* traits
| -rw-r--r-- | src/test/ui/dyn-star/dyn-async-trait.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/test/ui/dyn-star/dyn-async-trait.rs b/src/test/ui/dyn-star/dyn-async-trait.rs new file mode 100644 index 00000000000..a8c5a1dbf66 --- /dev/null +++ b/src/test/ui/dyn-star/dyn-async-trait.rs @@ -0,0 +1,36 @@ +// check-pass +// edition: 2021 + +// This test case is meant to demonstrate how close we can get to async +// functions in dyn traits with the current level of dyn* support. + +#![feature(dyn_star)] +#![allow(incomplete_features)] + +use std::future::Future; + +trait DynAsyncCounter { + fn increment<'a>(&'a mut self) -> dyn* Future<Output = usize> + 'a; +} + +struct MyCounter { + count: usize, +} + +impl DynAsyncCounter for MyCounter { + fn increment<'a>(&'a mut self) -> dyn* Future<Output = usize> + 'a { + Box::pin(async { + self.count += 1; + self.count + }) as dyn* Future<Output = _> // FIXME(dyn-star): coercion doesn't work here yet + } +} + +async fn do_counter(counter: &mut dyn DynAsyncCounter) -> usize { + counter.increment().await +} + +fn main() { + let mut counter = MyCounter { count: 0 }; + let _ = do_counter(&mut counter); +} |
