我无法弄清楚这段代码的生命周期参数。无论我尝试什么,通常都会导致编译器错误:“期望绑定生命周期参数 'a',找到具体生命周期”或类似“考虑使用如下示例所示的显式生命周期参数”(而示例并没有帮助)或“方法与特征不兼容”。
Request
、Response
和Action
是简化版本,以使此示例最小化。
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 for T
where
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");
}
for<
”没有出现过,而在编译器中(排除测试),它只被使用了33次。难怪我以前没见过它。如果有人对此有疑问,我能找到的唯一文档是该RFC。 - Veedrac