Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add direct support for i128 and u128 via itoa #940

Merged
merged 1 commit into from Oct 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/ser.rs
Expand Up @@ -121,7 +121,7 @@ where
serde_if_integer128! {
fn serialize_i128(self, value: i128) -> Result<()> {
self.formatter
.write_number_str(&mut self.writer, &value.to_string())
.write_i128(&mut self.writer, value)
.map_err(Error::io)
}
}
Expand Down Expand Up @@ -165,7 +165,7 @@ where
serde_if_integer128! {
fn serialize_u128(self, value: u128) -> Result<()> {
self.formatter
.write_number_str(&mut self.writer, &value.to_string())
.write_u128(&mut self.writer, value)
.map_err(Error::io)
}
}
Expand Down Expand Up @@ -974,7 +974,7 @@ where
tri!(self
.ser
.formatter
.write_number_str(&mut self.ser.writer, &value.to_string())
.write_i128(&mut self.ser.writer, value)
.map_err(Error::io));
tri!(self
.ser
Expand Down Expand Up @@ -1071,7 +1071,7 @@ where
tri!(self
.ser
.formatter
.write_number_str(&mut self.ser.writer, &value.to_string())
.write_u128(&mut self.ser.writer, value)
.map_err(Error::io));
tri!(self
.ser
Expand Down Expand Up @@ -1661,6 +1661,17 @@ pub trait Formatter {
writer.write_all(s.as_bytes())
}

/// Writes an integer value like `123` to the specified writer.
#[inline]
fn write_i128<W>(&mut self, writer: &mut W, value: i128) -> io::Result<()>
where
W: ?Sized + io::Write,
{
let mut buffer = itoa::Buffer::new();
let s = buffer.format(value);
writer.write_all(s.as_bytes())
}

/// Writes an integer value like `123` to the specified writer.
#[inline]
fn write_u8<W>(&mut self, writer: &mut W, value: u8) -> io::Result<()>
Expand Down Expand Up @@ -1705,6 +1716,17 @@ pub trait Formatter {
writer.write_all(s.as_bytes())
}

/// Writes an integer value like `123` to the specified writer.
#[inline]
fn write_u128<W>(&mut self, writer: &mut W, value: u128) -> io::Result<()>
where
W: ?Sized + io::Write,
{
let mut buffer = itoa::Buffer::new();
let s = buffer.format(value);
writer.write_all(s.as_bytes())
}

/// Writes a floating point value like `-31.26e+12` to the specified writer.
#[inline]
fn write_f32<W>(&mut self, writer: &mut W, value: f32) -> io::Result<()>
Expand Down