use std::collections::{HashMap, HashSet};
pub struct P<'a> {
x: &'a str
}
pub struct Foo<'a, T> {
callbacks: Vec<Box<'a + FnMut(&T)>>
}
impl<'a, T> Foo<'a, T>{
pub fn foo(&mut self, payload: T) {
}
}
pub struct Foo2<'a> {
callbacks: Vec<Box<'a + FnMut(&P)>>
}
impl<'a> Foo2<'a>{
pub fn foo(&mut self, payload: P) {
}
}
struct Bar<'a, 'b> {
x: Foo<'a, P<'b>>,
y: Foo2<'a>,
data: HashMap<String, String>
}
impl<'a, 'b> Bar<'a, 'b> {
// fn test(&mut self) {
// // Cannot infer an appropriate lifetime.
// match self.data.get("foo") {
// Some(x) => {
// let p = P {x};
// self.x.foo(p);
// },
// None => {}
// }
// }
fn test2(&mut self) {
match self.data.get("foo") {
Some(x) => {
let p = P {x};
self.y.foo(p);
},
None => {}
}
}
}
Playground. I'm using rustc 1.19.0-nightly.
Why does test2
work but test
does not? How can I correctly make the generic struct Foo
?
I do not think this example involves Why can't I store a value and a reference to that value in the same struct? and is not a duplicate.