Skip to content

Commit

Permalink
refactor(http)!: avoid copying the reponse body on utf8 error (twilig…
Browse files Browse the repository at this point in the history
…ht-rs#2299)

The `TextFuture` now returns `std::str::Utf8Error` as source error if
the body is not a valid UTF-8 string.

There is no need to provide the bytes with the error kind variant and as
part of the source error.
  • Loading branch information
nickelc authored Nov 27, 2023
1 parent 3db2801 commit 0fbb8b9
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions twilight-http/src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,14 +558,12 @@ impl Future for TextFuture {
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match Pin::new(&mut self.0).poll(cx) {
Poll::Ready(Ok(bytes)) => Poll::Ready(String::from_utf8(bytes).map_err(|source| {
// This is a very cold path. Converting a response body to a
// UTF-8 valid string should basically never fail anyway; it's
// worth it to have the context readily available for the user.
let copy = source.as_bytes().to_owned();
let utf8_error = source.utf8_error();
let bytes = source.into_bytes();

DeserializeBodyError {
kind: DeserializeBodyErrorType::BodyNotUtf8 { bytes: copy },
source: Some(Box::new(source)),
kind: DeserializeBodyErrorType::BodyNotUtf8 { bytes },
source: Some(Box::new(utf8_error)),
}
})),
Poll::Ready(Err(source)) => Poll::Ready(Err(source)),
Expand Down

0 comments on commit 0fbb8b9

Please sign in to comment.