Skip to content

Compatibility with specialization #25

Closed
@jhpratt

Description

@jhpratt

In Rocket, there's currently the following function:

impl<'r, R: Responder<'r> + Send + 'r, E: fmt::Debug + Send + 'r> Responder<'r> for Result<R, E> {
    default fn respond_to(self, req: &'r Request<'_>) -> response::ResultFuture<'r> {
        Box::pin(async move {
            match self {
                Ok(r) => r.respond_to(req).await,
                Err(e) => {
                    error_!("Response was a non-`Responder` `Err`: {:?}.", e);
                    Err(Status::InternalServerError)
                }
            }
        })
    }
}

I'm looking into rewriting the traits using this crate, and would like to write the function as

#[async_trait]
impl<'r, R: Responder<'r> + Send + 'r, E: fmt::Debug + Send + 'r> Responder<'r> for Result<R, E> {
    default async fn respond_to(self, req: &'r Request<'_>) -> response::Result<'r> {
        match self {
            Ok(r) => r.respond_to(req).await,
            Err(e) => {
                error_!("Response was a non-`Responder` `Err`: {:?}.", e);
                Err(Status::InternalServerError)
            }
        }
    }
}

When doing this, I get an error.

error: missing `fn`, `type`, or `const` for impl-item declaration
   --> core/lib/src/response/responder.rs:296:99
    |
296 |   impl<'r, R: Responder<'r> + Send + 'r, E: fmt::Debug + Send + 'r> Responder<'r> for Result<R, E> {
    |  ___________________________________________________________________________________________________^
297 | |     default async fn respond_to(self, req: &'r Request<'_>) -> response::Result<'r> {
    | |____^ missing `fn`, `type`, or `const`

I presume this is because of specialization, as it seems to work everywhere else.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions