我正在尝试为Iron请求创建处理程序:
extern crate iron;
extern crate mount;
use iron::{Iron, Request, Response, IronResult, status};
use mount::Mount;
use iron::middleware::Handler;
struct Server {
message: String
}
impl Server {
pub fn start(&self){
let mut mount = Mount::new();
mount.mount("/", &self);
Iron::new(mount).http("0.0.0.0:3000").unwrap();
}
}
impl Handler for Server {
fn handle(&self, _req: &mut Request) -> IronResult<Response>{
Ok(Response::with((status::Ok, self.message)))
}
}
fn main() {
Server{message: "test".to_string()}.start();
}
但编译器响应是:
error[E0277]: the trait bound `for<'r, 'r, 'r> Server: std::ops::Fn<(&'r mut iron::Request<'r, 'r>,)>` is not satisfied
--> src/main.rs:15:15
|
15 | mount.mount("/", &self);
| ^^^^^ trait `for<'r, 'r, 'r> Server: std::ops::Fn<(&'r mut iron::Request<'r, 'r>,)>` not satisfied
|
= note: required because of the requirements on the impl of `std::ops::FnOnce<(&mut iron::Request<'_, '_>,)>` for `&Server`
= note: required because of the requirements on the impl of `iron::Handler` for `&&Server`
我无法理解Rust对我说的话。
答案 0 :(得分:2)
这是您的问题的复制品;你能发现问题吗?
trait Foo {}
struct Bar;
impl Foo for Bar {}
impl Bar {
fn thing(&self) {
requires_bar(self);
}
}
fn requires_bar<F>(foo: F) where F: Foo {}
fn main() {}
放弃?
你已经为你的结构实现了特性:
impl Handler for Server
但是,然后尝试使用对结构的引用的引用,不实现该特征:
pub fn start(&self) {
// ...
mount.mount("/", &self);
// ...
}
这样就行不通了。您需要重构代码或实现特征以引用结构。