I thought I could give each of them a lifetime and give the return type the lifetime 'a + 'b
but the compiler seems to disagree, insisting that they have different lifetimes...
use std::collections::HashMap;
use std::sync::Arc;
use url::Url;
trait A: Send + Sync {
fn yay(&self, url: &Url) -> Option<usize>;
}
struct B {
hash: HashMap<&'static str, Arc<dyn A>>,
arr: Vec<Arc<dyn A>>,
}
impl B {
fn test<'a, 'b>(&'a self, url: &'b Url) -> impl Iterator<Item=(&'a Arc<dyn A>, usize)> + 'a + 'b {
let domain = url.domain().map(|d| d.to_ascii_lowercase());
let checker = |item: &'a Arc<dyn A>| {
let yay = item.yay(url);
yay.map(|i| (item, i))
};
let a = domain.and_then(|d| self.hash.get(d.as_str()).and_then(checker));
let b = self.arr.iter().flat_map(checker);
a.into_iter().chain(b)
}
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0623]: lifetime mismatch
--> src/lib.rs:15:48
|
15 | fn test<'a, 'b>(&'a self, url: &'b Url) -> impl Iterator<Item=(&'a Arc<dyn A>, usize)> + 'a + 'b {
| ------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| | |
| | ...but data from `self` is returned here
| this parameter and the return type are declared with different lifetimes...
error[E0623]: lifetime mismatch
--> src/lib.rs:15:48
|
15 | fn test<'a, 'b>(&'a self, url: &'b Url) -> impl Iterator<Item=(&'a Arc<dyn A>, usize)> + 'a + 'b {
| -------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| | |
| | ...but data from `url` is returned here
| this parameter and the return type are declared with different lifetimes...
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0623`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.