Rust 在中间件实现需要core::ops::Fn实现之前

Rust 在中间件实现需要core::ops::Fn实现之前,rust,web-frameworks,iron,Rust,Web Frameworks,Iron,我正在尝试为我拥有的结构实现beforembeddleware特性。我编写了以下代码: impl BeforeMiddleware for Auth { fn before(&self, _: &mut Request) -> IronResult<()> { println!("before called"); Ok(()) } fn catch(&self, _: &mut Reque

我正在尝试为我拥有的
结构实现
beforembeddleware
特性。我编写了以下代码:

impl BeforeMiddleware for Auth {
    fn before(&self, _: &mut Request) -> IronResult<()> {
        println!("before called");
        Ok(())
    }

    fn catch(&self, _: &mut Request, err: IronError) -> IronResult<()> {
        println!("catch called");
        Err(err)
    }
}
impl-before身份验证中间件{
fn在请求之前(&self、&mut请求)->IronResult{
println!(“前称”);
好(())
}
fn catch(&self,quot:&mut请求,err:IronError)->IronResult{
println!(“称为捕获”);
厄尔(厄尔)
}
}
我得到以下错误:

> cargo build
...
src/handlers/mod.rs:38:11: 38:28 error: the trait `for<'r, 'r, 'r> core::ops::Fn<(&'r mut iron::request::Request<'r, 'r>,)>` is not implemented for the type `auth::Auth` [E0277]
src/handlers/mod.rs:38     chain.link_before(auth);
                                 ^~~~~~~~~~~~~~~~~
src/handlers/mod.rs:38:11: 38:28 help: run `rustc --explain E0277` to see a detailed explanation
src/handlers/mod.rs:38:11: 38:28 error: the trait `for<'r, 'r, 'r> core::ops::FnOnce<(&'r mut iron::request::Request<'r, 'r>,)>` is not implemented for the type `auth::Auth` [E0277]
src/handlers/mod.rs:38     chain.link_before(auth);
                                 ^~~~~~~~~~~~~~~~~
src/handlers/mod.rs:38:11: 38:28 help: run `rustc --explain E0277` to see a detailed explanation
error: aborting due to 2 previous errors
...
>货物建造
...
src/handlers/mod.rs:38:11:38:28错误:未为类型'auth::auth`[E0277]实现特性'for'
src/handlers/mod.rs:38-chain.link\u-before(auth);
^~~~~~~~~~~~~~~~~
src/handlers/mod.rs:38:11:38:28帮助:运行'rustc--explain E0277'查看详细说明
src/handlers/mod.rs:38:11:38:28错误:未为类型'auth::auth`[E0277]实现特性'for'
src/handlers/mod.rs:38-chain.link\u-before(auth);
^~~~~~~~~~~~~~~~~
src/handlers/mod.rs:38:11:38:28帮助:运行'rustc--explain E0277'查看详细说明
错误:由于之前的两个错误而中止
...
但是表示
链接\u before
函数只需要
before中间件

有人知道我为什么会看到这个错误以及如何修复它吗

编辑:

实际上,我使用的是静态的
auth
,将其设置为非静态后,问题就消失了。

这很好:

extern crate iron;

use iron::{Chain, BeforeMiddleware, IronResult, Request, Response, IronError};
use iron::status;

struct Auth;

impl BeforeMiddleware for Auth {
    fn before(&self, _: &mut Request) -> IronResult<()> {
        println!("before called");
        Ok(())
    }

    fn catch(&self, _: &mut Request, err: IronError) -> IronResult<()> {
        println!("catch called");
        Err(err)
    }
}

fn main() {
    fn hello_world(_: &mut Request) -> IronResult<Response> {
        Ok(Response::with((status::Ok, "Hello World!")))
    }

    let mut c = Chain::new(hello_world);
    let auth = Auth;
    c.link_before(auth);
}
外部板条箱铁;
使用iron::{Chain,BeforeMiddleware,IronResult,Request,Response,IronError};
使用铁:状态;
结构验证;
用于身份验证的impl BEFORE中间件{
fn在请求之前(&self、&mut请求)->IronResult{
println!(“前称”);
好(())
}
fn catch(&self,quot:&mut请求,err:IronError)->IronResult{
println!(“称为捕获”);
厄尔(厄尔)
}
}
fn main(){
fn hello_world(u:&mut Request)->ironsult{
Ok(回复::with((状态::Ok,“你好,世界!”))
}
让mut c=Chain::new(hello_world);
设auth=auth;
c、 前链接(auth);
}
这是对iron 0.2.6的编译。

这很好:

extern crate iron;

use iron::{Chain, BeforeMiddleware, IronResult, Request, Response, IronError};
use iron::status;

struct Auth;

impl BeforeMiddleware for Auth {
    fn before(&self, _: &mut Request) -> IronResult<()> {
        println!("before called");
        Ok(())
    }

    fn catch(&self, _: &mut Request, err: IronError) -> IronResult<()> {
        println!("catch called");
        Err(err)
    }
}

fn main() {
    fn hello_world(_: &mut Request) -> IronResult<Response> {
        Ok(Response::with((status::Ok, "Hello World!")))
    }

    let mut c = Chain::new(hello_world);
    let auth = Auth;
    c.link_before(auth);
}
外部板条箱铁;
使用iron::{Chain,BeforeMiddleware,IronResult,Request,Response,IronError};
使用铁:状态;
结构验证;
用于身份验证的impl BEFORE中间件{
fn在请求之前(&self、&mut请求)->IronResult{
println!(“前称”);
好(())
}
fn catch(&self,quot:&mut请求,err:IronError)->IronResult{
println!(“称为捕获”);
厄尔(厄尔)
}
}
fn main(){
fn hello_world(u:&mut Request)->ironsult{
Ok(回复::with((状态::Ok,“你好,世界!”))
}
让mut c=Chain::new(hello_world);
设auth=auth;
c、 前链接(auth);
}

这是针对iron 0.2.6编译的。

我在问题中输入的代码与我使用的不完全相同,唯一的区别是
auth
是静态的。在使其非静态后,它就工作了。谢谢。我在问题中输入的代码与我使用的不完全一样,唯一的区别是
auth
是静态的。在使其非静态后,它就工作了。谢谢