|
91 | 91 | //! [from_reader]: https://docs.serde.rs/serde_json/de/fn.from_reader.html |
92 | 92 |
|
93 | 93 | use crate::error::Error; |
94 | | -use crate::io; |
95 | 94 | use crate::lib::*; |
96 | 95 | use serde::de::DeserializeOwned; |
97 | 96 | use serde::ser::Serialize; |
@@ -191,27 +190,43 @@ impl Debug for Value { |
191 | 190 | } |
192 | 191 | } |
193 | 192 |
|
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 | + } |
197 | 201 |
|
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 } |
204 | 206 | } |
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()) |
208 | 207 | } |
209 | 208 |
|
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 | + } |
212 | 225 | } |
213 | 226 | } |
214 | 227 |
|
| 228 | +use writer_formatter::WriterFormatter; |
| 229 | + |
215 | 230 | impl fmt::Display for Value { |
216 | 231 | /// Display a JSON value as a string. |
217 | 232 | /// |
@@ -239,7 +254,10 @@ impl fmt::Display for Value { |
239 | 254 | /// ``` |
240 | 255 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
241 | 256 | 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 | + }; |
243 | 261 | if alternate { |
244 | 262 | // {:#} |
245 | 263 | super::ser::to_writer_pretty(&mut wr, self).map_err(|_| fmt::Error) |
|
0 commit comments