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

Implement Deref for extractors #54

Closed
jakobhellermann opened this issue Jul 31, 2021 · 0 comments · Fixed by #56
Closed

Implement Deref for extractors #54

jakobhellermann opened this issue Jul 31, 2021 · 0 comments · Fixed by #56

Comments

@jakobhellermann
Copy link

If the extract::* types like Json would implement std::ops::Deref you could write

#[derive(Deserialize, Serialize)]
struct Request {
    foo: u8,
}

async fn handler(req: Json<Request>) {
    println!("{}", req.foo);
}

instead of

async fn handler(Json(req): Json<Request>) {
    println!("{}", req.foo);
}

or

async fn handler(req: Json<Request>) {
    let req = req.0;
    println!("{}", req.foo);
}

which IMO is easier to read.

The disadvantage is that it introduces some "Magic" that might be difficult to understand for newer rust developers.

Prior art

The bevy game engine can write ECS systems similar to how axum's request handlers work (example) and there you can write e.g.

fn system(resource: Res<Foo>) {
  println!("{}", resource.foo);
}

which works really well and leads to very readably systems.

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 a pull request may close this issue.

1 participant