1

我正在尝试使用一个简单的 Iron 示例:

extern crate iron;
extern crate router;

use iron::prelude::*;
use iron::status;

use router::Router;

fn main() {

    let mut router = Router::new();

    router.get("/controller", controller);

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

    Iron::new(router).http("localhost:3000").unwrap();
}

我维护以下 Cargo.toml:

[package]
name = "simple_router"
version = "0.1.0"
authors = ["Thijs"]

[dependencies]
iron="*"
router = "*"

我可以看到以下错误:

type mismatch: the type `fn(&mut iron::Request<'_, '_>) -> std::result::Result<iron::Response, iron::IronError> {main::controller}` implements the trait `for<'r, 'r, 'r> std::ops::Fn<(&'r mut iron::Request<'r, 'r>,)>`, but the trait `for<'r, 'r, 'r> std::ops::Fn<(&'r mut iron::request::Request<'r, 'r>,)>` is required (expected struct `iron::request::Request`, found struct `iron::Request`) [E0281]at line 13 col 12

运行时cargo build我得到以下信息:

  Compiling SimpleRouter v0.1.0 (file:///home/kolbein/workspaces/simplerust/SimpleRouter)
src/main.rs:13:12: 13:15 error: type mismatch: the type `fn(&mut iron::Request<'_, '_>) -> std::result::Result<iron::Response, iron::IronError> {main::controller}` implements the trait `for<'r, 'r, 'r> std::ops::Fn<(&'r mut iron::Request<'r, 'r>,)>`, but the trait `for<'r, 'r, 'r> std::ops::Fn<(&'r mut iron::request::Request<'r, 'r>,)>` is required (expected struct `iron::request::Request`, found struct `iron::Request`) [E0281]
src/main.rs:13     router.get("/controller", controller);
                          ^~~
src/main.rs:13:12: 13:15 help: run `rustc --explain E0281` to see a detailed explanation
src/main.rs:13:12: 13:15 note: required because of the requirements on the impl of `iron::middleware::Handler` for `fn(&mut iron::Request<'_, '_>) -> std::result::Result<iron::Response, iron::IronError> {main::controller}`
src/main.rs:13:12: 13:15 error: type mismatch: the type `fn(&mut iron::Request<'_, '_>) -> std::result::Result<iron::Response, iron::IronError> {main::controller}` implements the trait `for<'r, 'r, 'r> std::ops::FnOnce<(&'r mut iron::Request<'r, 'r>,)>`, but the trait `for<'r, 'r, 'r> std::ops::FnOnce<(&'r mut iron::request::Request<'r, 'r>,)>` is required (expected struct `iron::request::Request`, found struct `iron::Request`) [E0281]
src/main.rs:13     router.get("/controller", controller);
                          ^~~
src/main.rs:13:12: 13:15 help: run `rustc --explain E0281` to see a detailed explanation
src/main.rs:13:12: 13:15 note: required because of the requirements on the impl of `iron::middleware::Handler` for `fn(&mut iron::Request<'_, '_>) -> std::result::Result<iron::Response, iron::IronError> {main::controller}`
src/main.rs:19:5: 19:14 error: the trait bound `for<'r, 'r, 'r> router::Router: std::ops::Fn<(&'r mut iron::Request<'r, 'r>,)>` is not satisfied [E0277]
src/main.rs:19     Iron::new(router).http("localhost:3000").unwrap();
                   ^~~~~~~~~
src/main.rs:19:5: 19:14 help: run `rustc --explain E0277` to see a detailed explanation
src/main.rs:19:5: 19:14 note: required because of the requirements on the impl of `iron::Handler` for `router::Router`
src/main.rs:19:5: 19:14 note: required by `iron::Iron::new`
src/main.rs:19:5: 19:14 error: the trait bound `for<'r, 'r, 'r> router::Router: std::ops::FnOnce<(&'r mut iron::Request<'r, 'r>,)>` is not satisfied [E0277]
src/main.rs:19     Iron::new(router).http("localhost:3000").unwrap();
                   ^~~~~~~~~
src/main.rs:19:5: 19:14 help: run `rustc --explain E0277` to see a detailed explanation
src/main.rs:19:5: 19:14 note: required because of the requirements on the impl of `iron::Handler` for `router::Router`
src/main.rs:19:5: 19:14 note: required by `iron::Iron::new`
src/main.rs:19:23: 19:27 error: no method named `http` found for type `iron::Iron<router::Router>` in the current scope
src/main.rs:19     Iron::new(router).http("localhost:3000").unwrap();
                                     ^~~~
src/main.rs:19:23: 19:27 note: the method `http` exists but the following trait bounds were not satisfied: `router::Router : iron::Handler`
error: aborting due to 5 previous errors
error: Could not compile `SimpleRouter`.

您将在下面找到 Cargo.lock 文件:

[root]
name = "simple_router"
version = "0.1.0"
dependencies = [
 "iron 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "router 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "conduit-mime-types"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "cookie"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
 "url 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "error"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "traitobject 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
 "typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "hpack"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "httparse"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "hyper"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "cookie 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
 "httparse 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
 "language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
 "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
 "mime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "num_cpus 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
 "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
 "solicit 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
 "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
 "traitobject 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
 "unicase 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "url 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "hyper"
version = "0.9.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "cookie 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
 "httparse 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
 "language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
 "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
 "mime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "num_cpus 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
 "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
 "solicit 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
 "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
 "traitobject 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
 "unicase 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "url 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "idna"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
 "unicode-bidi 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
 "unicode-normalization 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "iron"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "conduit-mime-types 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
 "error 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
 "hyper 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
 "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
 "modifier 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "num_cpus 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
 "plugin 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
 "typemap 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
 "url 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "iron"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "conduit-mime-types 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
 "error 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
 "hyper 0.9.10 (registry+https://github.com/rust-lang/crates.io-index)",
 "lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
 "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
 "modifier 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "num_cpus 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
 "plugin 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
 "typemap 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
 "url 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "kernel32-sys"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
 "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "language-tags"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "lazy_static"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "libc"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "log"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "matches"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "mime"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "modifier"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "num_cpus"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "plugin"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "typemap 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "rand"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "route-recognizer"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "router"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "iron 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "route-recognizer 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "rustc-serialize"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "rustc_version"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "semver"
version = "0.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "solicit"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "hpack 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "time"
version = "0.1.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
 "libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
 "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "traitobject"
version = "0.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "traitobject"
version = "0.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "typeable"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "typemap"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "unsafe-any 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "unicase"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "unicode-bidi"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "unicode-normalization"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "unsafe-any"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "traitobject 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "url"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
 "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
 "unicode-bidi 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
 "unicode-normalization 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
 "uuid 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "url"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "idna 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "uuid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "winapi"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"

我应该添加更多细节。我可以添加更多详细信息..

4

2 回答 2

0

我删除了Cargo.lock文件并重建,它按预期工作。

于 2016-07-29T13:18:09.230 回答
0

另请参阅无法访问 Iron 中的参数,因为 trait bound plugin::Plugin<iron::Request> 不满足

您正在包含对 Iron 0.3 的回复的旧版本router

[[package]]
name = "router"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "iron 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "route-recognizer 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
]

但随后直接使用 Iron 0.4:

[[package]]
name = "iron"
version = "0.4.0"

不同的 crate 版本充当完全独立的 crate(即使它们在错误消息中看起来相似)。以这种方式混合和匹配版本是无效的。

于 2016-07-29T13:23:18.097 回答