Created
December 9, 2024 14:34
-
-
Save pplanel/9af17f4ae7cc4d611d527c621c1837ed to your computer and use it in GitHub Desktop.
axum like router with state
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(warnings)] | |
use std::marker::PhantomData; | |
fn foo() { | |
fn handler(State(s): State<AppState>) { | |
todo!() | |
} | |
let state = AppState {}; | |
let app: Router<AppState, Body> = Router::new().route("/", get(handler)).with_state(state); | |
} | |
#[derive(Clone)] | |
struct AppState {} | |
struct Body {} | |
#[derive(Debug)] | |
struct Request<Body> { | |
_marker: PhantomData<Body>, | |
} | |
struct Router<State, Body> { | |
_marker: std::marker::PhantomData<(State, Body)>, | |
} | |
impl<State, Body> Router<State, Body> { | |
fn new() -> Self { | |
todo!() | |
} | |
fn route(self, path: &str, service: MethodRouter<State, Body>) -> Self { | |
todo!() | |
} | |
fn with_state(self, state: State) -> Self { | |
todo!() | |
} | |
} | |
trait Service<R> {} | |
struct MethodRouter<State, Body> { | |
_marker: std::marker::PhantomData<(State, Body)>, | |
} | |
fn get<H, T, State, Body>(handler: H) -> MethodRouter<State, Body> | |
where | |
H: Handler<T, State, Body>, | |
{ | |
todo!() | |
} | |
trait Handler<HandlerInput, State, Body> {} | |
trait FromRequest<State, Body> {} | |
impl<State, Body, F> Handler<(), State, Body> for F where F: Fn() {} | |
impl<State, Body, F, T1> Handler<(T1,), State, Body> for F | |
where | |
F: Fn(T1), | |
T1: FromRequest<State, Body>, | |
{ | |
} | |
impl<State, Body, F, T1, T2> Handler<(T1, T2), State, Body> for F | |
where | |
F: Fn(T1, T2), | |
T1: FromRequest<State, Body>, | |
T2: FromRequest<State, Body>, | |
{ | |
} | |
struct State<S>(S); | |
impl<S, B> FromRequest<S, B> for State<S> {} | |
impl<S, B> FromRequest<S, B> for () {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment