Merge pull request #2329 from dtolnay/safety

Revert Buf::as_str safety change from PR 2319
This commit is contained in:
David Tolnay 2022-11-27 17:05:41 -08:00 committed by GitHub
commit fabbd2b097
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 3 deletions

View File

@ -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,
}

View File

@ -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)
}