Skip to content

Commit d33c1b5

Browse files
authored
Merge pull request #1237 from JonathanBrouwer/master
Improve documentation for buffering around functions taking a reader
2 parents 87f78da + 8c2d800 commit d33c1b5

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/de.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,17 @@ where
4545
/// Create a JSON deserializer from one of the possible serde_json input
4646
/// sources.
4747
///
48+
/// When reading from a source against which short reads are not efficient, such
49+
/// as a [`File`], you will want to apply your own buffering because serde_json
50+
/// will not buffer the input. See [`std::io::BufReader`].
51+
///
4852
/// Typically it is more convenient to use one of these methods instead:
4953
///
5054
/// - Deserializer::from_str
5155
/// - Deserializer::from_slice
5256
/// - Deserializer::from_reader
57+
///
58+
/// [`File`]: https://doc.rust-lang.org/std/fs/struct.File.html
5359
pub fn new(read: R) -> Self {
5460
Deserializer {
5561
read,
@@ -2568,6 +2574,7 @@ where
25682574
///
25692575
/// use std::error::Error;
25702576
/// use std::net::{TcpListener, TcpStream};
2577+
/// use std::io::BufReader;
25712578
///
25722579
/// #[derive(Deserialize, Debug)]
25732580
/// struct User {
@@ -2576,7 +2583,8 @@ where
25762583
/// }
25772584
///
25782585
/// fn read_user_from_stream(tcp_stream: TcpStream) -> Result<User, Box<dyn Error>> {
2579-
/// let mut de = serde_json::Deserializer::from_reader(tcp_stream);
2586+
/// let buf_tcp_stream = BufReader::new(tcp_stream);
2587+
/// let mut de = serde_json::Deserializer::from_reader(buf_tcp_stream);
25802588
/// let u = User::deserialize(&mut de)?;
25812589
///
25822590
/// Ok(u)

src/read.rs

+6
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ where
191191
R: io::Read,
192192
{
193193
/// Create a JSON input source to read from a std::io input stream.
194+
///
195+
/// When reading from a source against which short reads are not efficient, such
196+
/// as a [`File`], you will want to apply your own buffering because serde_json
197+
/// will not buffer the input. See [`std::io::BufReader`].
198+
///
199+
/// [`File`]: https://doc.rust-lang.org/std/fs/struct.File.html
194200
pub fn new(reader: R) -> Self {
195201
IoRead {
196202
iter: LineColIterator::new(reader.bytes()),

0 commit comments

Comments
 (0)