Suppose that I've written something roughly like this:
pub trait Tr {
fn form(a: &dyn Fn(usize)) -> Self;
}
pub struct Str<'t> {
func: &'t dyn Fn(usize),
}
impl<'t> Tr for Str<'t> {
fn form(a: &dyn Fn(usize)) -> Self {
let res: Self = Str { func: a };
return res;
}
}
Now, if there is no clear specification as to the lifetime of the parameter a
in the implemented method, there would be an error.
But rust would still report an error, complaining that res
doesn't have the expected type Self
when a lifetime is made explicit as well, just because the Self
at the return type of the method form
may not have the same lifetime as that of a
.
How do I implement a method with Self
as the return type for a type with a lifetime paramter -- when the method should construct a new instance of Self
and return it (like what is intended to be done in the above example)? I'm trying to get around a similar problem in a project, in which the specific trait cannot be made into a trait object. Please forgive me if this is a dumb question, I'm only starting to learn rust XD.