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

Add route register functions for different HTTP methods with different kinds of access to state #31

Merged
merged 2 commits into from
Jun 18, 2022

Conversation

jbearer
Copy link
Member

@jbearer jbearer commented Jun 17, 2022

Also refactor higher order async functions and improve documentation.

commit 56dc3ab (HEAD -> feat/method-registers, origin/feat/method-registers)
Author: Jeb Bearer [email protected]
Date: Fri Jun 17 15:44:20 2022 -0700

Update route parsing to register routes based on `Route::patterns`.

A nice feature is that the new routing allows the route name to be
different from the first segment of the route. This lets us do RESTy
things like

GET /greeting
POST /greeting

being different routes (see the hello-world example).

Also implemented just enough of route parsing/request parsing to get
the example to work, although I expect this will be replaced soon.

commit 6a0cf94
Author: Jeb Bearer [email protected]
Date: Fri Jun 17 15:08:34 2022 -0700

Interfaces for specific HTTP methods with mutable or immutable access.

The main point of this change is to add methods (get, post, put, and
delete) that are slightly more typesafe than `at`, for registering
handlers for routes with specific HTTP methods. Instead of passing
the top-level state to the handlers for these routes, we treat the
state as something that acts like a RwLock, automatically acquire
the lock for reading or writing, depending on the method and then
pass either a &State or &mut State to the handler. This decreases
boilerplate and makes it harder to accidentally register a mutating
route handler for a pure route, like GET.

In addition, this is a pretty big refactor of the original interfaces
in order to clean up and settle on a pattern for dealing with async
functions and lifetimes.

* By using associated types to name or uniquely identify some types
  in function/trait bounds, I was able to eliminate a few phantom
  type parameters in some of the route types. This in turn stops
  "phantom constraints" from propagating all the way up the call
  stack, removing a few Send, Sync, and 'static requirements.
* I decoupled `State` from `RequestParams`, so route handlers now
  take two arguments. This makes the interfaces a bit more consistent
  between `at` (where the handler takes a `State` directly) and `get`,
  etc. (where the handler takes a `<State as ReadState>::State`.
* I ran into a Rust compiler bug which trips when we have Fn constraints
  that take a reference and return a future (which we do everywhere, now).
  There is a reasonable workaround that just requires a little bit more
  boxing. This should be thoroughly documented now.
* Improved documentation and expanded/added examples throughout.

jbearer added 2 commits June 17, 2022 15:21
The main point of this change is to add methods (get, post, put, and
delete) that are slightly more typesafe than `at`, for registering
handlers for routes with specific HTTP methods. Instead of passing
the top-level state to the handlers for these routes, we treat the
state as something that acts like a RwLock, automatically acquire
the lock for reading or writing, depending on the method and then
pass either a &State or &mut State to the handler. This decreases
boilerplate and makes it harder to accidentally register a mutating
route handler for a pure route, like GET.

In addition, this is a pretty big refactor of the original interfaces
in order to clean up and settle on a pattern for dealing with async
functions and lifetimes.

* By using associated types to name or uniquely identify some types
  in function/trait bounds, I was able to eliminate a few phantom
  type parameters in some of the route types. This in turn stops
  "phantom constraints" from propagating all the way up the call
  stack, removing a few Send, Sync, and 'static requirements.
* I decoupled `State` from `RequestParams`, so route handlers now
  take two arguments. This makes the interfaces a bit more consistent
  between `at` (where the handler takes a `State` directly) and `get`,
  etc. (where the handler takes a `<State as ReadState>::State`.
* I ran into a Rust compiler bug which trips when we have Fn constraints
  that take a reference and return a future (which we do everywhere, now).
  There is a reasonable workaround that just requires a little bit more
  boxing. This should be thoroughly documented now.
* Improved documentation and expanded/added examples throughout.
A nice feature is that the new routing allows the route name to be
different from the first segment of the route. This lets us do RESTy
things like

GET /greeting
POST /greeting

being different routes (see the hello-world example).

Also implemented just enough of route parsing/request parsing to get
the example to work, although I expect this will be replaced soon.
@jbearer jbearer requested review from pictographer and nyospe June 17, 2022 22:48
@jbearer jbearer self-assigned this Jun 17, 2022
Copy link
Contributor

@pictographer pictographer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow. This is awesome.

@pictographer pictographer merged commit c159183 into main Jun 18, 2022
pictographer added a commit that referenced this pull request Jun 18, 2022
@Ancient123 Ancient123 deleted the feat/method-registers branch November 29, 2022 15:59
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

Successfully merging this pull request may close these issues.

2 participants