So again I am struggling with lifetimes.
I have a basic example below
I have a pipeline object that contains two vectors. The first holds Element’s which is a trait and the second
ElementPads which are normal structs. I have added lifetimes on the Pipeline struct for both types. I also have two functions one to find a Element and one to find a ElementPad.
This throws the error
cannot infer an appropriate lifetime for lifetime parameter 'a
due to conflicting requirements
at this line
let element : &Element = try!(self.find_element(name));
Why is this ?
the return on that function is
PipelineResult<(&'a Element, &'b ElementPad)>
so should the compiler be able to infer this ?
Thanks
pub type ElementResult<T> = Result<T, String>;
pub struct ElementPad {
pub name : String,
}
pub trait Element {
fn get_name(&self) -> String;
fn get_pad(&self, name : &str) -> ElementResult<&ElementPad>;
}
pub type PipelineResult<T> = Result<T, String>;
pub struct Pipeline<'a, 'b> {
elements : Vec<&'a Element>,
element_pads : Vec<&'b ElementPad>,
}
impl<'a, 'b> Pipeline<'a, 'b> {
fn find_element(&self, name : &str) -> PipelineResult<&'a Element> {
let option = self.elements.iter().find(|&r| r.get_name() == name.to_string());
match option {
Some(e) => Ok(*e),
None => {
warn!("Element with name {} not found in pipeline", name);
return Err("Error".to_string())
},
}
}
// Comment this and things are fine
fn find_element_pad(&self, name : &str, pad_name : &str) -> PipelineResult<(&'a Element, &'b ElementPad)> {
let element : &Element = try!(self.find_element(name));
let pad = try!(element.get_pad(pad_name));
Ok((element, pad))
}
}