iron / persistent

Middleware for sharing data between requests
43 stars 29 forks source link

Example in README does not compile #32

Closed TheLQ closed 10 years ago

TheLQ commented 10 years ago

The example on the front page does not compile anymore

src/web.rs:20:21: 20:53 error: type `&mut iron::request::Request` does not implement any method in scope named `get`
src/web.rs:20     let mutex = req.get::<Write<HitCounter, uint>>().unwrap();
                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/web.rs:23:5: 23:11 error: the type of this value must be known in this context
src/web.rs:23     *count += 1;
                  ^~~~~~
error: aborting due to 2 previous errors

I've tried several fixes but the magic of typemap makes it really unintuitive.

Attempt 1 based on TypeMap's README: let mutex = req.extensions.find::<Write<HitCounter, uint>>().unwrap();

src/web.rs:20:17: 20:65 error: incorrect number of type parameters given for this method [E0036]
src/web.rs:20     let mutex = req.extensions.find::<Write<HitCounter, uint>>().unwrap();
                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Attempt 2: let mutex = req.extensions::<Write<HitCounter, uint>>.find().unwrap();

src/web.rs:21:21: 21:26 error: the type of this value must be known in this context
src/web.rs:21     let mut count = mutex.lock();
                                  ^~~~~
src/web.rs:23:5: 23:11 error: the type of this value must be known in this context
src/web.rs:23     *count += 1;
                  ^~~~~~
error: aborting due to 2 previous errors

Every other attempt fails with a similar error. My guess is the the type information is lost in all the casting and magic, but I have no idea where it is or how to fix it

reem commented 10 years ago

Did you import iron::Plugin? It provides the get method on Request. Perhaps this could be clarified in the README.

TheLQ commented 10 years ago

Welp, it works now, thanks!

I agree the import really needs to be more prominent. Playing "will it compile?" games with the compiler will sort out all the other imports but iron::plugin == Request.get() isn't very intuitive

reem commented 10 years ago

Finding which traits give you which methods is always pretty annoying, I agree.

reem commented 10 years ago

Just added a clarifying note to the README, so closing.