Skip to content

Commit ba135ee

Browse files
authored
Rollup merge of #104557 - eholk:dyn-star-in-traits, r=compiler-errors
Add a test case for async dyn* traits This adds a test case that approximates async functions in dyn traits using `dyn*`. The purpose is to have an example of where we are with `dyn*` and the goal of using it for dyn traits. Issue #102425 r? `@compiler-errors`
2 parents 7a3eca6 + ff38c35 commit ba135ee

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+
})
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)