diff --git a/serde/src/de/format.rs b/serde/src/de/format.rs index aa4d53f98..f14580b8d 100644 --- a/serde/src/de/format.rs +++ b/serde/src/de/format.rs @@ -7,13 +7,13 @@ pub(super) struct Buf<'a> { } impl<'a> Buf<'a> { - pub(super) fn new(bytes: &'a mut [u8]) -> Self { + pub fn new(bytes: &'a mut [u8]) -> Self { Buf { bytes, offset: 0 } } - pub(super) unsafe fn as_str(&self) -> &str { + pub fn as_str(&self) -> &str { let slice = &self.bytes[..self.offset]; - str::from_utf8_unchecked(slice) + unsafe { str::from_utf8_unchecked(slice) } } } diff --git a/serde/src/de/mod.rs b/serde/src/de/mod.rs index 43084a486..d9dafbe1e 100644 --- a/serde/src/de/mod.rs +++ b/serde/src/de/mod.rs @@ -1376,11 +1376,7 @@ pub trait Visitor<'de>: Sized { let mut buf = [0u8; 58]; let mut writer = format::Buf::new(&mut buf); fmt::Write::write_fmt(&mut writer, format_args!("integer `{}` as i128", v)).unwrap(); - - // Safety: This is safe because we only wrote UTF-8 into the buffer. - let s = unsafe { writer.as_str() }; - - Err(Error::invalid_type(Unexpected::Other(s), &self)) + Err(Error::invalid_type(Unexpected::Other(writer.as_str()), &self)) } } @@ -1442,11 +1438,7 @@ pub trait Visitor<'de>: Sized { let mut buf = [0u8; 57]; let mut writer = format::Buf::new(&mut buf); fmt::Write::write_fmt(&mut writer, format_args!("integer `{}` as u128", v)).unwrap(); - - // Safety: This is safe because we only wrote UTF-8 into the buffer. - let s = unsafe { writer.as_str() }; - - Err(Error::invalid_type(Unexpected::Other(s), &self)) + Err(Error::invalid_type(Unexpected::Other(writer.as_str()), &self)) } }