From d940fe1b4934f097b0333dcb87bec5ee308d39cd Mon Sep 17 00:00:00 2001 From: Mathias Date: Thu, 5 Sep 2024 14:24:44 +0200 Subject: [PATCH] Reuse existing Buf wrapper as replacement for std::io::Write --- serde/src/de/mod.rs | 5 ++--- serde/src/{de => }/format.rs | 0 serde/src/lib.rs | 2 ++ serde/src/ser/impls.rs | 43 +++--------------------------------- 4 files changed, 7 insertions(+), 43 deletions(-) rename serde/src/{de => }/format.rs (100%) diff --git a/serde/src/de/mod.rs b/serde/src/de/mod.rs index d6b9f5ab4..a87558ec5 100644 --- a/serde/src/de/mod.rs +++ b/serde/src/de/mod.rs @@ -118,7 +118,6 @@ use crate::lib::*; pub mod value; -mod format; mod ignored_any; mod impls; pub(crate) mod size_hint; @@ -1374,7 +1373,7 @@ pub trait Visitor<'de>: Sized { E: Error, { let mut buf = [0u8; 58]; - let mut writer = format::Buf::new(&mut buf); + let mut writer = crate::format::Buf::new(&mut buf); fmt::Write::write_fmt(&mut writer, format_args!("integer `{}` as i128", v)).unwrap(); Err(Error::invalid_type( Unexpected::Other(writer.as_str()), @@ -1436,7 +1435,7 @@ pub trait Visitor<'de>: Sized { E: Error, { let mut buf = [0u8; 57]; - let mut writer = format::Buf::new(&mut buf); + let mut writer = crate::format::Buf::new(&mut buf); fmt::Write::write_fmt(&mut writer, format_args!("integer `{}` as u128", v)).unwrap(); Err(Error::invalid_type( Unexpected::Other(writer.as_str()), diff --git a/serde/src/de/format.rs b/serde/src/format.rs similarity index 100% rename from serde/src/de/format.rs rename to serde/src/format.rs diff --git a/serde/src/lib.rs b/serde/src/lib.rs index 32509ca85..f40f6a60e 100644 --- a/serde/src/lib.rs +++ b/serde/src/lib.rs @@ -310,6 +310,8 @@ mod integer128; pub mod de; pub mod ser; +mod format; + #[doc(inline)] pub use crate::de::{Deserialize, Deserializer}; #[doc(inline)] diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index aae7ec453..235c9cdeb 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -769,31 +769,6 @@ impl Serialize for SystemTime { //////////////////////////////////////////////////////////////////////////////// -#[cfg(any(feature = "std", not(no_core_net)))] -struct Wrapper<'a> { - pub buf: &'a mut [u8], - pub offset: usize, -} - -#[cfg(any(feature = "std", not(no_core_net)))] -impl<'a> fmt::Write for Wrapper<'a> { - fn write_str(&mut self, s: &str) -> fmt::Result { - if self.offset > self.buf.len() { - return Err(fmt::Error); - } - let remaining_buf = &mut self.buf[self.offset..]; - let raw_s = s.as_bytes(); - let write_num = core::cmp::min(raw_s.len(), remaining_buf.len()); - remaining_buf[..write_num].copy_from_slice(&raw_s[..write_num]); - self.offset += raw_s.len(); - if write_num < raw_s.len() { - Err(fmt::Error) - } else { - Ok(()) - } - } -} - /// Serialize a value that implements `Display` as a string, when that string is /// statically known to never have more than a constant `MAX_LEN` bytes. /// @@ -802,21 +777,9 @@ impl<'a> fmt::Write for Wrapper<'a> { macro_rules! serialize_display_bounded_length { ($value:expr, $max:expr, $serializer:expr) => {{ let mut buffer = [0u8; $max]; - let written_len = { - let mut w = Wrapper { - buf: &mut buffer, - offset: 0, - }; - write!(&mut w, "{}", $value).unwrap(); - w.offset - }; - let written = &buffer[..written_len]; - - // write! only provides fmt::Formatter to Display implementations, which - // has methods write_str and write_char but no method to write arbitrary - // bytes. Therefore `written` must be valid UTF-8. - let written_str = str::from_utf8(written).expect("must be valid UTF-8"); - $serializer.serialize_str(written_str) + let mut writer = crate::format::Buf::new(&mut buffer); + write!(&mut writer, "{}", $value).unwrap(); + $serializer.serialize_str(writer.as_str()) }}; }