Unable to compile Iron example: expected struct `i

2019-07-31 07:48发布

I'm trying to get a simple Iron example to work:

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

I maintain the following Cargo.toml:

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

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

I can see the following 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]at line 13 col 12

when running cargo build I got the following:

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

Below you'll find the Cargo.lock file:

[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"

I should add more details. What more details can I add..

标签: rust iron
2条回答
The star\"
2楼-- · 2019-07-31 08:22

I removed the Cargo.lock file and rebuilt and it works as expected.

查看更多
贪生不怕死
3楼-- · 2019-07-31 08:26

See also Cannot access parameters in Iron because the trait bound plugin::Plugin<iron::Request> is not satisfied

You are including an old version of router that replies on Iron 0.3:

[[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)",
]

But then are using Iron 0.4 directly:

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

Different crate versions act as completely separate crates (even though they look similar in error messages). It's not valid to mix and match the versions in this manner.

查看更多
登录 后发表回答