简体   繁体   English

预期的约束寿命参数,找到具体的寿命

[英]Expected bound lifetime parameter, found concrete lifetime

I can't figure out the lifetime parameters for this code. 我无法弄清楚这段代码的生命周期参数。 Everything I try usually results in a compiler error: "Expected bound lifetime parameter 'a , found concrete lifetime" or something like "consider using an explicit lifetime parameter as shown" (and the example shown doesn't help) or "method not compatible with trait". 我尝试的所有内容通常会导致编译器错误:“预期的绑定生命周期参数'a ,找到具体的生命周期”或类似“考虑使用显示的显式生命周期参数”(并且显示的示例没有帮助)或“方法不兼容”特质“。

Request , Response , and Action are simplified versions to keep this example minimal. RequestResponseAction是简化版本,以使此示例保持最小。

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");
}

Rust Playground 铁锈游乐场

Your trait function definition is this: 你的特质函数定义是这样的:

fn handle<'a>(&self, req: Request, res: Response<'a>) -> Action<'a>;

Note that 'a is specified by the caller and can be anything and is not necessarily tied to self in any way. 请注意, 'a由调用者指定,可以是任何内容,并且不一定与self绑定。

Your trait implementation definition is this: 你的特质实现定义是这样的:

fn handle(&self, req: Request, res: Response<'a>) -> Action<'a>;

'a is not here specified by the caller, but is instead tied to the type you are implementing the trait for. 'a不在此处由调用者指定,而是与您实现该特征的类型相关联。 Thus the trait implementation does not match the trait definition. 因此,特征实现与特征定义不匹配。

Here is what you need: 这是你需要的:

trait Handler: Send + Sync {
    fn handle<'a>(&self, req: Request, res: Response<'a>) -> Action<'a>;
}

impl<T> Handler for T
where
    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 is the change in the T bound: for<'a> Fn(Request, Response<'a>) -> Action<'a> . 关键点是T界限的变化: for<'a> Fn(Request, Response<'a>) -> Action<'a> This means: “given an arbitrary lifetime parameter 'a , T must satisfy Fn(Request, Response<'a>) -> Action<'a> ; 这意味着:“给定一个任意的生命周期参数'aT必须满足Fn(Request, Response<'a>) -> Action<'a> ; or, “ T must, for all 'a , satisfy Fn(Request, Response<'a>) -> Action<'a> . 或者,“ T必须,对于所有'a ,满足Fn(Request, Response<'a>) -> Action<'a>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 预期的约束寿命参数,找到的混凝土寿命[E0271] - Expected bound lifetime parameter, found concrete lifetime [E0271] 如何解决:预期的混凝土寿命,但找到了约束寿命参数 - How to fix: expected concrete lifetime, but found bound lifetime parameter StreamExt.scan() 方法上的“预期绑定生命周期参数,找到具体生命周期” - “expected bound lifetime parameter, found concrete lifetime” on StreamExt .scan() method 锈蚀寿命误差预期具体寿命但发现约束寿命 - Rust lifetime error expected concrete lifetime but found bound lifetime 函数引用:预期的约束生命周期参数,找到具体的生命周期[E0271] - Function references: expected bound lifetime parameter , found concrete lifetime [E0271] 预期绑定生命周期参数,在尝试传递选项时找到具体生命周期<fnonce></fnonce> - Expected bound lifetime parameter, found concrete lifetime when trying to pass an Option<FnOnce> 预期的混凝土寿命,在结构中存储fn时找到约束寿命参数 - Expected concrete lifetime, found bound lifetime parameter when storing a fn in a struct 当使用参数的生命周期作为特征参数时,“预期的关联类型,找到`u32`” - “Expected associated type, found `u32`” when using the lifetime of a parameter as trait parameter in where bound 从闭包填充集合时,键入不匹配“绑定生命周期参数”与“具体生命周期” - Type mismatch “bound lifetime parameter” vs “concrete lifetime” when filling a collection from a closure 为什么此特性/实现不兼容-约束寿命与具体寿命 - Why is this trait/implementation incompatible - bound lifetime vs concrete lifetime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM