Skip to content

Commit

Permalink
Merge pull request #2329 from dtolnay/safety
Browse files Browse the repository at this point in the history
Revert Buf::as_str safety change from PR 2319
  • Loading branch information
dtolnay committed Nov 28, 2022
2 parents 4ea403c + 6814f97 commit fabbd2b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion serde/src/de/format.rs
@@ -1,7 +1,7 @@
use lib::fmt::{self, Write};
use lib::str;

pub struct Buf<'a> {
pub(super) struct Buf<'a> {
bytes: &'a mut [u8],
offset: usize,
}
Expand Down
5 changes: 3 additions & 2 deletions serde/src/ser/impls.rs
Expand Up @@ -736,8 +736,9 @@ impl Serialize for net::Ipv4Addr {
// Skip over delimiters that we initialized buf with
written += format_u8(*oct, &mut buf[written + 1..]) + 1;
}
// We've only written ASCII bytes to the buffer, so it is valid UTF-8
serializer.serialize_str(unsafe { str::from_utf8_unchecked(&buf[..written]) })
// Safety: We've only written ASCII bytes to the buffer, so it is valid UTF-8
let buf = unsafe { str::from_utf8_unchecked(&buf[..written]) };
serializer.serialize_str(buf)
} else {
self.octets().serialize(serializer)
}
Expand Down

0 comments on commit fabbd2b

Please sign in to comment.