From 13c4fb567e5c905daa33e38b1f27a1dcfd4059d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81kos=20Vandra-Meyer?= Date: Fri, 31 Jan 2025 09:12:46 +0100 Subject: [PATCH] implement Responder for Option<()> and Result<(), E: Error> --- actix-web/CHANGES.md | 1 + actix-web/src/response/responder.rs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index cee14dc4b..25a9b65ff 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -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 diff --git a/actix-web/src/response/responder.rs b/actix-web/src/response/responder.rs index 90d8f6e52..03f6cd087 100644 --- a/actix-web/src/response/responder.rs +++ b/actix-web/src/response/responder.rs @@ -116,6 +116,17 @@ impl Responder for Option { } } +//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 Responder for Result 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> 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 Responder for (R, StatusCode) { type Body = R::Body;