Skip to content

add a TypedMethod trait/derive similar to TypedPath#3610

Open
Zizico2 wants to merge 1 commit intotokio-rs:mainfrom
Zizico2:main
Open

add a TypedMethod trait/derive similar to TypedPath#3610
Zizico2 wants to merge 1 commit intotokio-rs:mainfrom
Zizico2:main

Conversation

@Zizico2
Copy link

@Zizico2 Zizico2 commented Jan 1, 2026

Motivation

Defining an entire path in the same place, on the same struct (missing query params?) seems useful to me. It also matches what actix-web, rocket or poem macros provide.

Solution

A TypedMethod trait/derive easily allows the method to be co-located with the path.

Usage:

#[tokio::main]
async fn main() {
    let app = Router::new().typed(get_user);

    let listener = TcpListener::bind("127.0.0.1:3000")
        .await
        .expect("failed to bind 127.0.0.1:3000");

    println!("Listening on http://127.0.0.1:3000");

    axum::serve(listener, app).await.expect("server error");
}

async fn get_user(UsersMember { id }: UsersMember) -> String {
    format!("User {id} is ready")
}

#[derive(TypedMethod, TypedPath, Deserialize)]
#[typed_method(MethodFilter::GET)]
#[typed_path("/users/{id}")]
struct UsersMember {
    id: u32,
}

Would this be considered? It's missing comments and tests if needed.

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 this pull request may close these issues.

1 participant