I am trying to understand how frameworks like actix or axum can accept as function callbacks other functions with a variable number of parameters, for example here: Extractors .
In the source code of axum I found this macro: axum/mod.rs at be68227d739aece4210d4e7cb7d419da4d4afe30 · tokio-rs/axum · GitHub
but I am not able to replicate its behaviour.
Here my attempt:
pub trait RestType<I, O> {}
pub trait IntoResponse{}
pub trait FromRequest<T>{}
pub trait Handler<I, O, B, T> {
fn route<REST: RestType<I, O>>(self, rest: &REST);
}
impl <I: Serialize + DeserializeOwned + Send + 'static, O: Serialize + DeserializeOwned + Send + 'static, B, F, R, E, P1> Handler<I, O, B, (P1)> for F
where
F: 'static + Send + Sync + FnOnce(I, P1) -> R,
R: Future<Output = Result<O, E>> + Send,
E: IntoResponse + Send + 'static,
B: Send + 'static,
P1: FromRequest<B>
{
fn route<REST: RestType<I, O>>(self, rest: &REST) {
todo!()
}
}
impl <I: Serialize + DeserializeOwned + Send + 'static, O: Serialize + DeserializeOwned + Send + 'static, B, F, R, E, P1, P2> Handler<I, O, B, (P1, P2)> for F
where
F: 'static + Send + Sync + FnOnce(I, P1) -> R,
R: Future<Output = Result<O, E>> + Send,
E: IntoResponse + Send + 'static,
B: Send + 'static,
P1: FromRequest<B> + Send,
P2: FromRequest<B> + Send,
{
fn route<REST: RestType<I, O>>(self, rest: &REST) {
todo!()
}
}
This code does not compile because of conflicting implementations; anyway, I cannot understand what I am doing wrong.
The part less clear to me is that it does compile if I remove the T
generic from the FromRequest<T>
trait.
Could anyone help me understand it?