-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(router): Add chainable routes #288
base: master
Are you sure you want to change the base?
Conversation
} | ||
|
||
impl<'a, D: Sync + Send + 'static> ChainablePath<'a, D> for Nickel<D> { | ||
fn route(&'static mut self, path: &str) -> ChainableRoute<'a, D> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should work with &'a mut self
rather than &static ..
@Ryman Thanks very much for the help. Seems to be working now. Please take a look again when you have a chance and let me know if you have any feedback. I'm still very much new to rust. So, any feedback is appreciated. One question I had was whether Rust can distinguish between trait methods with the same name but different arity. When I try to import both my chainable router impls and the HttpRouter ones, I get a compilation error that the "get" method for example exists in both traits. So I'm thinking it can't tell that they have different signatures/arities. Maybe this is just a limitation of rust at the moment? |
Yep, there's no such thing as method overloading in Rust and it's probably unlikely to be implemented. However, you can always just shift syntax to specifically tell the compiler which method to invoke. So, instead of writing this which would be forbidden: use TraitWithGet;
use OtherTraitWithGet;
some_thing.get();
some_thing.get(true); //compiler error You could write: use TraitWithGet;
use OtherTraitWithGet;
TraitWithGet::get(some_thing);
OtherTraitWithGet::get(some_thing, true); I didn't test it out but I'm pretty sure it should work. UPDATE Corrected syntax according to @Ryman 's comment. Stupid me ;) |
What @cburgdorf is referring to is UFCS but it should be You shouldn't really be running into the method name clash unless you've implemented both traits for a given type though? (has the code changed?) Inference should be able to see that the return from |
Right, that's true. I confused myself. I had an intermediate implementation where I was just adding the last path added with |
@Ryman @cburgdorf Is there anything I can do to improve this? Any feedback is appreciated as I'm new with rust. |
use router::http_router::{HttpRouter}; | ||
|
||
pub struct ChainableRoute<'a, D: Sync + Send + 'static = ()> { | ||
path: String, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be &'a str
, or you might need to introduce a new lifetime 'b
if the route
chaining is to work (haven't tried)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I'm trying to hack around my limitations in understanding lifetimes. I'll try that and see if I can get it working with an &str.
@mattbanister Sorry for the delay, I've left some feedback :) At a higher level though, do you think you might want to publish this as a crate? I think it builds nicely around the core so I think it would be a good example of how to make extensions to nickel if you wished to do so! |
Hi @Ryman Thanks for the feedback. I think that would be great to make it a separate crate. I'll work on that and your other comments this week. I really appreciate the help! |
This doesn't work yet but I'm putting up the PR because I could use some help with it.