roadster-rs / roadster

"Batteries Included" web framework for Rust designed to get you moving fast 🏎️
Apache License 2.0
3 stars 0 forks source link

Custom state via FromRef instead of nesting in AppContext? #249

Closed spencewenski closed 3 days ago

spencewenski commented 6 days ago

This may be required in order to integrate with Leptos

spencewenski commented 6 days ago

Yeah, we should change how we have consumers provide custom state to follow the recommendation from Axum: https://docs.rs/axum/0.7.5/axum/extract/struct.State.html#for-library-authors. Not only would this be in-line with the recommendation, but I believe is required in order to allow consumers to use Leptos.

Example:

#[derive(Clone)]
struct RoadsterState {
    foo: String,
}

#[derive(Clone, FromRef)]
struct CustomState {
    bar: String,
    foo: RoadsterState,
}

fn baz<S>(s: S)
where
    RoadsterState: FromRef<S>,
{
    let roadster_state = RoadsterState::from_ref(&s);
}
spencewenski commented 6 days ago

but I believe is required in order to allow consumers to use Leptos.

This may not be 100% true -- we could potentially provide the required leptos state from the Roadster AppContext directly (and implement FromRef on AppContext). However, that would only solve this for Leptos, and not any other library that a consumer may want to use that had similar requirements.

spencewenski commented 6 days ago

This will likely be a breaking change.

spencewenski commented 6 days ago

where RoadsterState: FromRef<S>

I didn’t even know it was possible to write type bounds like this. This is pretty cool. We’re probably going to need to change a bunch of stuff to use this approach. I’m also not sure how this will impact our http service builder, and a bunch of other stuff… this is going to be a big change…

spencewenski commented 3 days ago

Completed in https://github.com/roadster-rs/roadster/pull/250