From 1050f6b80851ba2bbef25992da2646bd79b04882 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Wed, 9 Nov 2022 22:41:28 +0000 Subject: [PATCH] Change comment to // Safety: ... This changes a comment to be explicit on how it's safe we can avoid validating UTF-8. --- serde/src/ser/impls.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index 8e865558..ccef4d29 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -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) }