如何解决:预期的混凝土寿命,但找到了约束寿命参数 [英] How to fix: expected concrete lifetime, but found bound lifetime parameter

查看:130
本文介绍了如何解决:预期的混凝土寿命,但找到了约束寿命参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在就此发表自己的看法.我试图将其缩小到最小的可重现示例.

I'm currently pulling my hear out over this one. I tried to shrink it down to a minimal reproducible example.

struct Request;

struct ResponseWriter<'a> { 
    dummy: &'a () 
}

#[deriving(Clone)]
pub struct RouteStore{
    pub routes: Vec<Route>,
}

#[deriving(Clone)]
struct Route {
    path: String,
    handler: fn(request: &Request, response: &mut ResponseWriter)
}

impl RouteStore {
    pub fn new () -> RouteStore {
        RouteStore {
            routes: Vec::new()
        }
    }

    fn add_route (&mut self, path: String, handler: fn(request: &Request, response: &mut ResponseWriter)) -> () {
        let route = Route {
            path: path,
            handler: handler
        };
        self.routes.push(route);
    }
}

fn main () {

}

这给我留下了

error: mismatched types: expected `fn(&http::server::request::Request, &mut http::server::response::ResponseWriter<>)` but found `fn(&http::server::request::Request, &mut http::server::response::ResponseWriter<>)` (expected concrete lifetime, but found bound lifetime parameter )
src/so.rs:12     handler: fn(request: &Request, response: &mut ResponseWriter)
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

以前,我将fn存储在像HashMap<String, fn(request: &Request, response: &mut ResponseWriter)>这样的HashMap中.效果很好.

Previously I stored my fn in a HashMap like this HashMap<String, fn(request: &Request, response: &mut ResponseWriter)>. This worked fine.

但是现在我想稍微重构一下,并引入了Route结构并将其存储为Vec<Route>.但是突然间,地狱破裂了,我不知道如何解决它:-/

But now I want to refactor things a bit and introduced a Route struct and store things as Vec<Route>. But suddenly hell breaks loose and I don't know how to fix it :-/

对于好奇的人来说,这是我为Rust创建一个受expressjs启发的Web框架的一部分,该框架称为 Floor

For the curious ones, this is part of my effort to write an expressjs inspired web framework for Rust called Floor

推荐答案

在使您的示例变得更简单之后(摆脱了http依赖),并对IRC提出了一些建议(即有人指出问题出在如果您从Route中删除deriving(Clone)),则可以在下面看到一个固定版本(--cfg v2)

After making your example a bit more minimal (getting rid of the http dependency), and some suggestions on IRC (namely someone pointing out that the problem goes away if you remove the deriving(Clone) from the Route), you can see a fixed version below (--cfg v2)

#[deriving(Clone)]
pub struct RouteStore{
    pub routes: Vec<Route>,
}

#[cfg(v1)]
#[deriving(Clone)]
struct Route {
    path: String,
    handler: fn(response: &mut ())
}

#[cfg(v2)]
struct Route {
    path: String,
    handler: fn(response: &mut ())
}

#[cfg(v2)]
impl Clone for Route {
    fn clone(&self) -> Route {
        Route { path: self.path.clone(), handler: self.handler }
    }
}
impl RouteStore {
    pub fn new () -> RouteStore {
        RouteStore {
            routes: Vec::new()
        }
    }

    fn add_route (&mut self, path: String, handler: fn(response: &mut ())) -> () {
        let route = Route {
            path: path,
            handler: handler
        };
        self.routes.push(route);
    }
}

fn main () {

}

这里的问题是fn没有实现Clone.(好吧,它确实具有clone方法,但是返回了值似乎与该字段所需的值不兼容.上面的v2版本仅绕过了整个问题.)

The issue here is that an fn does not implement Clone. (edit: Well, it does have a clone method, but the returned value does not seem compatible with what that field needs. The v2 version above just side-steps the whole issue.)

因此,我怀疑您看到的错误来自deriving(Clone)注入的自动生成的克隆实现.

So I suspect the error you are seeing is coming from the auto-generated clone implementation that deriving(Clone) injects.

这篇关于如何解决:预期的混凝土寿命,但找到了约束寿命参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆