Skip to content
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

Consider making Router Data not an Option #38

Closed
tv42 opened this issue Sep 10, 2021 · 5 comments
Closed

Consider making Router Data not an Option #38

tv42 opened this issue Sep 10, 2021 · 5 comments

Comments

@tv42
Copy link

tv42 commented Sep 10, 2021

It seems the only reason Router and RouteContext data method return an Option<Data> is because you've decided to implement Default for a router. Nothing seems to use that default. Could I convince you to remove the Default and make data be not optional? That would remove a .unwrap() from practically every handler of every app using data.

For inspiration/justification/examples:

@nilslice
Copy link
Contributor

Yes, that's most likely a leftover artifact from an earlier design... I will definitely take another look at this and reconsider. Unless there is another reason that currently escapes me, I agree it should not use Option.

The other thing I dislike about the Router but could not figure out a way around is the requirement to pass some data, even just unit type (), to satisfy the generic type parameter. Haven't had time to revisit that, but if you're digging around in that part of the code and happen to have an idea, I'm all ears!

@tv42
Copy link
Author

tv42 commented Sep 11, 2021

See Tide's ::with_state(x) vs ::new() for a way to hide the () from the caller who doesn't care. It'll still show up in types, of course.

https://github.com/http-rs/tide/blob/1d6f120c9e6c0e723bb8929dc0f601cd4f9d1449/src/server.rs#L59-L61

@nilslice
Copy link
Contributor

@tv42 - of course, seems so obvious in hindsight :) Thanks for pointing that out.

The only downside is that we still need to add Clone to the bounds on D for the data. I am still trying to get some notion of a "global" for the router to be created in the global scope, which would likely require this anyways. (Worker's Isolates can share global state if they are in the same colo and under other conditions)

I've got a branch up with this change if you would like to take a look: f95b3ad

@nilslice
Copy link
Contributor

This has been updated in #50 - thanks for the feedback @tv42 !

@nilslice
Copy link
Contributor

The only downside is that we still need to add Clone to the bounds on D for the data.

For posterity, Clone was not added as a bound to D here. Instead, a &D is returned from ctx.data() and if you need ownership, you can implement Clone on your types via derive or manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants