Skip to content

Commit 1a913a6

Browse files
committed
Add a test case for async dyn* traits
1 parent 7fe6f36 commit 1a913a6

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// check-pass
2+
// edition: 2021
3+
4+
// This test case is meant to demonstrate how close we can get to async
5+
// functions in dyn traits with the current level of dyn* support.
6+
7+
#![feature(dyn_star)]
8+
#![allow(incomplete_features)]
9+
10+
use std::future::Future;
11+
12+
trait DynAsyncCounter {
13+
fn increment<'a>(&'a mut self) -> dyn* Future<Output = usize> + 'a;
14+
}
15+
16+
struct MyCounter {
17+
count: usize,
18+
}
19+
20+
impl DynAsyncCounter for MyCounter {
21+
fn increment<'a>(&'a mut self) -> dyn* Future<Output = usize> + 'a {
22+
Box::pin(async {
23+
self.count += 1;
24+
self.count
25+
}) as dyn* Future<Output = _> // FIXME(dyn-star): coercion doesn't work here yet
26+
}
27+
}
28+
29+
async fn do_counter(counter: &mut dyn DynAsyncCounter) -> usize {
30+
counter.increment().await
31+
}
32+
33+
fn main() {
34+
let mut counter = MyCounter { count: 0 };
35+
let _ = do_counter(&mut counter);
36+
}

0 commit comments

Comments
 (0)