rust - Rust BeforeMiddleware实现需要核心:: ops:: fn实现

  显示原文与译文双语对照的内容
93 5

我正在尝试为一个 struct 实现 BeforeMiddleware 特性。我已经编写了以下代码:

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

我得到以下错误:

> 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
...

但是文档link_before 函数只需要一个 BeforeMiddleware

谁知道我看到这个错误的原因以及如何修复它?

编辑:

我实际上使用了 static auth,在它不静态的情况下问题消失了。

时间:原作者:0个回答

109 0

这工作很正常:

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 0.2.6.

原作者:
...