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

Made DefaultBodyLimit const friendly #2875

Merged
merged 2 commits into from
Aug 16, 2024
Merged

Made DefaultBodyLimit const friendly #2875

merged 2 commits into from
Aug 16, 2024

Conversation

StNicolay
Copy link
Contributor

Derived Copy for DefaultBodyLimit
Made DefaultBodyLimit::max and DefaultBodyLimit::disabled const

Motivation

Making DefaultBodyLimit more const friendly will reduce the code duplication for projects trying to add this layer to multiple endpoints but not to the entire route or router by removing the unnecessary clone calls

Solution

By deriving Copy and making those methods const we allow the following code to be used:

const LIMIT: DefaultBodyLimit = DefaultBodyLimit::disable();

Router::new()
    .route(
        "/files",
        get(file::download)
            .post(file::upload.layer(LIMIT))
            .delete(file::delete)
            .patch(file::modify.layer(LIMIT)),
    )

instead of

let limit = DefaultBodyLimit::disable();


Router::new()
    .route(
        "/files",
        get(file::download)
            .post(file::upload.layer(limit.clone()))
            .delete(file::delete)
            .patch(file::modify.layer(limit.clone())),
    )

Derived Copy for DefaultBodyLimit
Made DefaultBodyLimit::max and DefaultBodyLimit::disabled const
Copy link
Member

@jplatte jplatte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for the PR!

Can you add a changelog entry for it?

axum-core/src/extract/default_body_limit.rs Show resolved Hide resolved
@jplatte jplatte merged commit 6f50c6e into tokio-rs:main Aug 16, 2024
18 checks passed
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.

2 participants