BeforeMiddleware 实现需要 core::ops::Fn 实现
BeforeMiddleware implementation requires core::ops::Fn implementation
我正在尝试为我拥有的 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
...
但是 the documentation 说 link_before
函数只需要 BeforeMiddleware
。
有谁知道我为什么会看到这个错误以及如何解决它?
编辑:
我实际上使用的是静态 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 0.2.6 编译的。
我正在尝试为我拥有的 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
...
但是 the documentation 说 link_before
函数只需要 BeforeMiddleware
。
有谁知道我为什么会看到这个错误以及如何解决它?
编辑:
我实际上使用的是静态 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 0.2.6 编译的。