I want the async block in the following code to implement Send
(Playground):
use std::collections::BTreeSet;
use std::future::ready;
pub fn test<T: Sync>(set: &BTreeSet<T>) -> impl Send + '_ {
async move {
for _ in set {
ready(()).await;
}
}
}
But it gives the following error:
Compiling playground v0.0.1 (/playground)
error[E0311]: the parameter type `T` may not live long enough
--> src/lib.rs:4:44
|
4 | pub fn test<T: Sync>(set: &BTreeSet<T>) -> impl Send + '_ {
| -- ^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
| |
| help: consider adding an explicit lifetime bound...: `T: 'a +`
error: aborting due to previous error
error: could not compile `playground`
To learn more, run the command again with --verbose.
I don't understand the error at all. Adding a lifetime bound does not solve the problem (Playground), unless the added lifetime bound is 'static
(Playground).
I tried replacing BTreeSet
with Vec
, VecDeque
, LinkedList
, HashSet
, BinaryHeap
. All compiled without error. What is so special about BTreeSet
?
Update: This seems to be fixed in Rust 1.66.0. The code now compiles without error.
I still don't know the reason of this error and how it was fixed.