Skip to content

Commit b6c855d

Browse files
committed
Touch up PR 751
1 parent e81e7d9 commit b6c855d

File tree

1 file changed

+26
-41
lines changed

1 file changed

+26
-41
lines changed

src/value/mod.rs

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
//! [from_reader]: https://docs.serde.rs/serde_json/de/fn.from_reader.html
9292
9393
use crate::error::Error;
94+
use crate::io;
9495
use crate::lib::*;
9596
use serde::de::DeserializeOwned;
9697
use serde::ser::Serialize;
@@ -190,43 +191,6 @@ impl Debug for Value {
190191
}
191192
}
192193

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-
}
201-
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 }
206-
}
207-
}
208-
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-
}
225-
}
226-
}
227-
228-
use writer_formatter::WriterFormatter;
229-
230194
impl fmt::Display for Value {
231195
/// Display a JSON value as a string.
232196
///
@@ -253,11 +217,32 @@ impl fmt::Display for Value {
253217
/// "{\n \"city\": \"London\",\n \"street\": \"10 Downing Street\"\n}");
254218
/// ```
255219
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
220+
struct WriterFormatter<'a, 'b: 'a> {
221+
inner: &'a mut fmt::Formatter<'b>,
222+
}
223+
224+
impl<'a, 'b> io::Write for WriterFormatter<'a, 'b> {
225+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
226+
// Safety: the serializer below only emits valid utf8 when using
227+
// the default formatter.
228+
let s = unsafe { str::from_utf8_unchecked(buf) };
229+
tri!(self.inner.write_str(s).map_err(io_error));
230+
Ok(buf.len())
231+
}
232+
233+
fn flush(&mut self) -> io::Result<()> {
234+
Ok(())
235+
}
236+
}
237+
238+
fn io_error(_: fmt::Error) -> io::Error {
239+
// Error value does not matter because Display impl just maps it
240+
// back to fmt::Error.
241+
io::Error::new(io::ErrorKind::Other, "fmt error")
242+
}
243+
256244
let alternate = f.alternate();
257-
let mut wr = unsafe {
258-
// The serializer only emits valid json with the default formatters
259-
WriterFormatter::new(f)
260-
};
245+
let mut wr = WriterFormatter { inner: f };
261246
if alternate {
262247
// {:#}
263248
super::ser::to_writer_pretty(&mut wr, self).map_err(|_| fmt::Error)

0 commit comments

Comments
 (0)