trait Handler {
fn handle<'a>(&self, input: &'a str) -> Result<&'a str, HandlerError>;
}
fn process_handler<'a>(
handler: Box<dyn Handler + 'a>,
input: &'a str,
) -> Result<&'a str, HandlerError> {
handler.handle(input)
} trait Handler {
fn handle<'a>(&self, input: &'a str) -> Result<&'a str, HandlerError>;
}
fn process_handler<'a>(
handler: Box<dyn Handler + 'a>,
input: &'a str,
) -> Result<&'a str, HandlerError> {
handler.handle(input)
}In the places where you need to add lifetime annotations, it's certainly useful to be able to see them in the types, rather than relegate them to the documentation like in C++; cf. all the places where C++'s STL has to mention iterator and reference invalidation.
The handler function isn't actually unnecessary, or at least, it isn't superfluous: by default, the signature would include 'a on self as well, and that's probably not what you actually want.
I do think that the example basically boils down to the lifetime syntax though, and yes, while it's a bit odd at first, every other thing that was tried was worse.
To clarify, I meant the 'a in `Box<dyn Handler + 'a>` in the definition of `process_handler` is unnecessary. I'm not saying that the <'a> parameter in the definition of Handler::handle is unnecessary, which seems to be what you think I said, unless I misunderstood.
You choose as your example a pretty advanced use case.