implement Responder for Option<()> and Result<(), E: Error>

This commit is contained in:
Ákos Vandra-Meyer 2025-01-31 09:12:46 +01:00
parent 91e29c0ce4
commit 13c4fb567e
2 changed files with 24 additions and 0 deletions

View File

@ -5,6 +5,7 @@
- On Windows, an error is now returned from `HttpServer::bind()` (or TLS variants) when binding to a socket that's already in use.
- Update `brotli` dependency to `7`.
- Minimum supported Rust version (MSRV) is now 1.75.
- Implemented responder for `Option<()>` and `Result<(), E: Error> to respond with `204 No Content`
## 4.9.0

View File

@ -116,6 +116,17 @@ impl<R: Responder> Responder for Option<R> {
}
}
//Note see https://github.com/actix/actix-web/issues/1108 on why Responder is not implemented for ()
impl Responder for Option<()> {
type Body = BoxBody;
fn respond_to(self, _req: &HttpRequest) -> HttpResponse {
match self {
Some(()) => HttpResponse::NoContent().finish(),
None => HttpResponse::NotFound().finish(),
}
}
}
impl<R, E> Responder for Result<R, E>
where
R: Responder,
@ -131,6 +142,18 @@ where
}
}
//Note see https://github.com/actix/actix-web/issues/1108 on why Responder is not implemented for ()
impl<E: Into<Error>> Responder for Result<(), E> {
type Body = BoxBody;
fn respond_to(self, _req: &HttpRequest) -> HttpResponse {
match self {
Ok(()) => HttpResponse::new(StatusCode::NO_CONTENT),
Err(e) => HttpResponse::from_error(e.into()),
}
}
}
impl<R: Responder> Responder for (R, StatusCode) {
type Body = R::Body;