Skip to content

Commit

Permalink
Reuse existing Buf wrapper as replacement for std::io::Write
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasKoch committed Sep 5, 2024
1 parent f2899a9 commit d940fe1
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 43 deletions.
5 changes: 2 additions & 3 deletions serde/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ use crate::lib::*;

pub mod value;

mod format;
mod ignored_any;
mod impls;
pub(crate) mod size_hint;
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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()),
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ mod integer128;
pub mod de;
pub mod ser;

mod format;

#[doc(inline)]
pub use crate::de::{Deserialize, Deserializer};
#[doc(inline)]
Expand Down
43 changes: 3 additions & 40 deletions serde/src/ser/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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())
}};
}

Expand Down

0 comments on commit d940fe1

Please sign in to comment.