Skip to content

Commit

Permalink
Merge pull request #87 from dtolnay/just-itoa
Browse files Browse the repository at this point in the history
Speed up integer printing
  • Loading branch information
dtolnay authored Jun 27, 2016
2 parents e670e13 + bb717a3 commit 0d1e16b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ serde = "^0.7.0"
num-traits = "~0.1.32"
clippy = { version = "^0.*", optional = true }
linked-hash-map = { version = "0.0.11", optional = true }
itoa = "0.1"
1 change: 1 addition & 0 deletions json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
extern crate num_traits;
extern crate core;
extern crate serde;
extern crate itoa;
#[cfg(feature = "preserve_order")]
extern crate linked_hash_map;

Expand Down
22 changes: 12 additions & 10 deletions json/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use std::num::FpCategory;
use serde::ser;
use super::error::{Error, ErrorCode, Result};

use itoa;

/// A structure for serializing Rust values into JSON.
pub struct Serializer<W, F=CompactFormatter> {
writer: W,
Expand Down Expand Up @@ -77,52 +79,52 @@ impl<W, F> ser::Serializer for Serializer<W, F>

#[inline]
fn serialize_isize(&mut self, value: isize) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
fn serialize_i8(&mut self, value: i8) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
fn serialize_i16(&mut self, value: i16) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
fn serialize_i32(&mut self, value: i32) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
fn serialize_i64(&mut self, value: i64) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
fn serialize_usize(&mut self, value: usize) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
fn serialize_u8(&mut self, value: u8) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
fn serialize_u16(&mut self, value: u16) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
fn serialize_u32(&mut self, value: u32) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
fn serialize_u64(&mut self, value: u64) -> Result<()> {
write!(&mut self.writer, "{}", value).map_err(From::from)
itoa::write(&mut self.writer, value).map_err(From::from)
}

#[inline]
Expand Down

0 comments on commit 0d1e16b

Please sign in to comment.