Skip to content

Add bounds to constructors #916

Closed
Closed
@Diegovsky

Description

@Diegovsky

Feature Request

Motivation

Currently on axum, most generic types have unbounded constructors (e.g: Html, HandleErrorLayer) but their relevant trait implementations (IntoResponse for Html, Layer for HandleErrorLayer) have bounds, leaving confusing error messages when using them with non conforming types (such as using a Cow<'a, str> with Html).

Proposal

Add redundant bounds to constructors, also making the transition easier when rust's Implied Trait Bounds land.

Example of Status Quo

Say I'm writing an error handling middleware. According to the docs, this could work:

pub async fn error_function(err: BoxError) -> () {
    todo!()
}

#[tokio::main]
async main() {
    let app = Router::new()
        .layer(
        tower::ServiceBuilder::new()
            .layer(HandleErrorLayer::new(error_function)),
    );


    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();

}

However I get this error on line 8 which should appear on HandleErrorLayer::new instead:
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions