rust - Expected bound lifetime parameter, found concrete lifetime -
i can't figure out lifetime parameters code. try results in compiler error: "expected bound lifetime parameter 'a
, found concrete lifetime" or "consider using explicit lifetime parameter shown" (and example shown doesn't help) or "method not compatible trait".
request
, response
, , action
simplified versions keep example minimal.
struct request { data: string, } struct response<'a> { data: &'a str, } pub enum action<'a> { next(response<'a>), done, } pub trait handler: send + sync { fn handle<'a>(&self, req: request, res: response<'a>) -> action<'a>; } impl<'a, t> handler t t: send + sync + fn(request, response<'a>) -> action<'a>, { fn handle(&self, req: request, res: response<'a>) -> action<'a> { (*self)(req, res) } } fn main() { println!("running"); }
your trait function definition this:
fn handle<'a>(&self, req: request, res: response<'a>) -> action<'a>;
note 'a
specified caller , can , not tied self
in way.
your trait implementation definition this:
fn handle(&self, req: request, res: response<'a>) -> action<'a>;
'a
not here specified caller, instead tied type implementing trait for. trait implementation not match trait definition.
here need:
trait handler: send + sync { fn handle<'a>(&self, req: request, res: response<'a>) -> action<'a>; } impl<t> handler t t: send + sync + for<'a> fn(request, response<'a>) -> action<'a>, { fn handle<'a>(&self, req: request, res: response<'a>) -> action<'a> { (*self)(req, res) } }
the key point change in t
bound: for<'a> fn(request, response<'a>) -> action<'a>
. means: “given arbitrary lifetime parameter 'a
, t
must satisfy fn(request, response<'a>) -> action<'a>
; or, “t
must, 'a
, satisfy fn(request, response<'a>) -> action<'a>
.
Comments
Post a Comment