Skip to content

Commit be9bc23

Browse files
committed
NFC: simplify internals of http::response::try_from_incoming
1 parent ec2b222 commit be9bc23

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/http/response.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use http::StatusCode;
22
use wasip2::http::types::IncomingResponse;
33

44
use crate::http::body::{Body, BodyHint};
5-
use crate::http::error::{Context, Error};
5+
use crate::http::error::Error;
66
use crate::http::fields::{HeaderMap, header_map_from_wasi};
77

88
pub use http::response::{Builder, Response};
@@ -22,10 +22,17 @@ pub(crate) fn try_from_incoming(incoming: IncomingResponse) -> Result<Response<B
2222
let body = Body::from_incoming(incoming_body, hint);
2323

2424
let mut builder = Response::builder().status(status);
25-
26-
if let Some(headers_mut) = builder.headers_mut() {
27-
*headers_mut = headers;
28-
}
29-
30-
builder.body(body).context("building response")
25+
// The [`http::response::Builder`] keeps internal state of whether the
26+
// builder has errored, which is only reachable by passing
27+
// [`Builder::header`] an erroring `TryInto<HeaderName>` or
28+
// `TryInto<HeaderValue>`. Since the `Builder::header` method is never
29+
// used, we know `Builder::headers_mut` will never give the None case, nor
30+
// will `Builder::body` give the error case. So, rather than treat those
31+
// as control flow, we unwrap if this invariant is ever broken because
32+
// that would only be possible due to some unrecoverable bug in wstd,
33+
// rather than incorrect use or invalid input.
34+
*builder.headers_mut().expect("builder has not errored") = headers;
35+
Ok(builder
36+
.body(body)
37+
.expect("response builder should not error"))
3138
}

0 commit comments

Comments
 (0)