-
Notifications
You must be signed in to change notification settings - Fork 47
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
[Feature Request] More granular app state #207
Comments
Good idea. What about codegen only for |
This is how I imagine the implementation: // desugaring of codegen :
impl AsRef<String> for MyState {}
impl AsRef<PayloadConfig> for MyState {}
impl<'r, 'a, S, S2> FromRequest<'a, WebRequest<'r, S>> for StateRef<'a, S2>
where
S: AsRef<S2>,
{} It shouldn't touch xitca_http. Indeed it can be implemented with no change to xitca-web either, but I think it's important to standardize a trait for such use case. |
In fact this works. There is a catch though. At least one of the handler have to infer the State type by extract it. // New StateRef implementation.
impl<'a, 'r, S, T> FromRequest<'a, WebRequest<'r, S>> for StateRef<'a, T>
where
T: 'static,
S: 'static + Borrow<T>,
{
type Type<'b> = StateRef<'b, T>;
type Error = Infallible;
type Future = impl Future<Output = Result<Self, Self::Error>> where WebRequest<'r, S>: 'a;
#[inline]
fn from_request(req: &'a WebRequest<'r, S>) -> Self::Future {
async move { Ok(StateRef(req.state().borrow())) }
}
} // typed state and codegen for borrow fields.
#[derive(Clone, Debug)]
struct State {
field1: String,
field2: u32,
}
impl Borrow<String> for State {
fn borrow(&self) -> &String {
&self.field1
}
}
impl Borrow<u32> for State {
fn borrow(&self) -> &u32 {
&self.field2
}
} // pass state to app.
App::with_current_thread_state(State {
field1: String::from("state"),
field2: 996,
})
// extract fields work fine.
async fn handler(
StateRef(state): StateRef<'_, String>,
StateRef(state2): StateRef<'_, u32>,
StateRef(state3): StateRef<'_, State>,
req: &WebRequest<'_, State>,
) -> String {
assert_eq!("state", state);
assert_eq!(&996, state2);
assert_eq!(state, req.state().field1.as_str());
state.to_string()
} |
I don't understand these std borrow traits. AsRef is basically the same as Borrow from what I see. and judging by the StateRef naming it could be better to use AsRef. |
I think Borrow is suitable too. AFAIK |
Implemented with #208 This issue should remain open as it's still half way for this feature with two questions left:
|
I imagine the following api for the same granularity and modularity of actix-web app state, but statically checked.
This can leverage the current
BorrowReq
trait.The text was updated successfully, but these errors were encountered: