Skip to content

Commit e0747b9

Browse files
committed
use from_utf8_unchecked in the fmt::Display implementation of Value
`WriterFormatter` has been moved into it's own little module to enforce using it's `new` method to convey the unsafe requirements. the safety requirements that the serializer only emits valid utf8 is already assumed by the existing `to_string` methods. this speeds up formatting large `Value`s by about 33% in my testing
1 parent 7ff8ec6 commit e0747b9

1 file changed

Lines changed: 34 additions & 16 deletions

File tree

‎src/value/mod.rs‎

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
//! [from_reader]: https://docs.serde.rs/serde_json/de/fn.from_reader.html
9292
9393
use crate::error::Error;
94-
use crate::io;
9594
use crate::lib::*;
9695
use serde::de::DeserializeOwned;
9796
use serde::ser::Serialize;
@@ -191,27 +190,43 @@ impl Debug for Value {
191190
}
192191
}
193192

194-
struct WriterFormatter<'a, 'b: 'a> {
195-
inner: &'a mut fmt::Formatter<'b>,
196-
}
193+
// WriterFormatter is in it's own little module to enforce using the unsafe constructor
194+
mod writer_formatter {
195+
use crate::io;
196+
use crate::lib::*;
197+
198+
pub struct WriterFormatter<'a, 'b: 'a> {
199+
inner: &'a mut fmt::Formatter<'b>,
200+
}
197201

198-
impl<'a, 'b> io::Write for WriterFormatter<'a, 'b> {
199-
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
200-
fn io_error<E>(_: E) -> io::Error {
201-
// Error value does not matter because fmt::Display impl below just
202-
// maps it to fmt::Error
203-
io::Error::new(io::ErrorKind::Other, "fmt error")
202+
impl<'a, 'b: 'a> WriterFormatter<'a, 'b> {
203+
/// Safety: the caller needs to ensure that only valid utf8 bytes are written into this writer
204+
pub unsafe fn new(inner: &'a mut fmt::Formatter<'b>) -> Self {
205+
Self { inner }
204206
}
205-
let s = tri!(str::from_utf8(buf).map_err(io_error));
206-
tri!(self.inner.write_str(s).map_err(io_error));
207-
Ok(buf.len())
208207
}
209208

210-
fn flush(&mut self) -> io::Result<()> {
211-
Ok(())
209+
impl<'a, 'b> io::Write for WriterFormatter<'a, 'b> {
210+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
211+
fn io_error<E>(_: E) -> io::Error {
212+
// Error value does not matter because fmt::Display impl below just
213+
// maps it to fmt::Error
214+
io::Error::new(io::ErrorKind::Other, "fmt error")
215+
}
216+
// The safety requirements of passing in valid utf8 is passed trough to the caller trough the unsafe `new` method
217+
let s = unsafe { str::from_utf8_unchecked(buf) };
218+
tri!(self.inner.write_str(s).map_err(io_error));
219+
Ok(buf.len())
220+
}
221+
222+
fn flush(&mut self) -> io::Result<()> {
223+
Ok(())
224+
}
212225
}
213226
}
214227

228+
use writer_formatter::WriterFormatter;
229+
215230
impl fmt::Display for Value {
216231
/// Display a JSON value as a string.
217232
///
@@ -239,7 +254,10 @@ impl fmt::Display for Value {
239254
/// ```
240255
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
241256
let alternate = f.alternate();
242-
let mut wr = WriterFormatter { inner: f };
257+
let mut wr = unsafe {
258+
// The serializer only emits valid json with the default formatters
259+
WriterFormatter::new(f)
260+
};
243261
if alternate {
244262
// {:#}
245263
super::ser::to_writer_pretty(&mut wr, self).map_err(|_| fmt::Error)

0 commit comments

Comments
 (0)