Reuse existing Buf wrapper as replacement for std::io::Write

This commit is contained in:
Mathias
2024-09-05 14:24:44 +02:00
parent f2899a9e06
commit d940fe1b49
4 changed files with 7 additions and 43 deletions
+2 -3
View File
@@ -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()),
+2
View File
@@ -310,6 +310,8 @@ mod integer128;
pub mod de;
pub mod ser;
mod format;
#[doc(inline)]
pub use crate::de::{Deserialize, Deserializer};
#[doc(inline)]
+3 -40
View File
@@ -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())
}};
}