I am still having problems with lifetimes in Rust. I want to use a closure that implements a trait that returns a reference, but I can't figure out how to make the compiler happy.
This is kind of an extension of this question I asked recently which I got working, but now when I add a closure to the program it won't compile.
I think part of my problem is I don't really understand the difference between declaring a lifetime in an impl
block, and using for
in a where
clause.
If I can get it to work using lifetimes in an impl block or lifetimes in a for
block, I would be happy, but I would also like to know the difference between them.
Edit: The last 2 links are links to a Rust playground with code I can't get to compile. The first link is also shown below:
pub struct Response<'a>{
data: &'a str
}
pub enum Action<'a>{
Next(Response<'a>),
Done
}
pub trait Handler<'a>: Send + Sync{
fn handle(&self, res: Response<'a>) -> Action<'a>;
}
impl<'a, T> Handler<'a> for T
where T: Send + Sync + Fn(Response<'a>) -> Action<'a> {
fn handle(&self, res: Response<'a>) -> Action<'a>{
(*self)(res)
}
}
fn main(){
println!("running");
do_something_with_handler(|res: Response| -> Action{
//this closure should implement Handler
//Action::Done //This compiles if Done is returned instead of Next
Action::Next(res)
});
}
pub fn do_something_with_handler<'a, T>(handler: T) where T: Handler<'a> + 'static{
//do something here
}
// This isn't used in this example, but I want to make sure the trait
// can still be implemented for arbitrary types
struct SomethingElse;
impl<'a> Handler<'a> for SomethingElse{
fn handle(&self, res: Response<'a>) -> Action<'a>{
Action::Next(res)
}
}