Skip to content

Commit a4eaa7f

Browse files
axos88robjtede
andauthored
implement Responder for Result<(), E: Error> (#3560)
* implement Responder for Option<()> and Result<(), E: Error> * chore: remove Option<()> impl * chore: fix changelog --------- Co-authored-by: Rob Ede <[email protected]>
1 parent 66e2afe commit a4eaa7f

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

actix-web/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
- Implement `Responder` for `Result<(), E: Into<Error>>`. Returning `Ok(())` responds with HTTP 204 No Content.
56
- On Windows, an error is now returned from `HttpServer::bind()` (or TLS variants) when binding to a socket that's already in use.
67
- Update `brotli` dependency to `7`.
78
- Minimum supported Rust version (MSRV) is now 1.75.

actix-web/src/response/responder.rs

+17
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,23 @@ where
131131
}
132132
}
133133

134+
// Note: see https://github.com/actix/actix-web/issues/1108 for reasoning why Responder is not
135+
// implemented for `()`, and https://github.com/actix/actix-web/pull/3560 for discussion about this
136+
// impl and the decision not to include a similar one for `Option<()>`.
137+
impl<E> Responder for Result<(), E>
138+
where
139+
E: Into<Error>,
140+
{
141+
type Body = BoxBody;
142+
143+
fn respond_to(self, _req: &HttpRequest) -> HttpResponse {
144+
match self {
145+
Ok(()) => HttpResponse::new(StatusCode::NO_CONTENT),
146+
Err(err) => HttpResponse::from_error(err.into()),
147+
}
148+
}
149+
}
150+
134151
impl<R: Responder> Responder for (R, StatusCode) {
135152
type Body = R::Body;
136153

0 commit comments

Comments
 (0)