Skip to content

Commit

Permalink
Move BufReader to caller
Browse files Browse the repository at this point in the history
The original read_user_from_stream function from PR 1237 is a bad
pattern because it results in silently dropping any data left in the
buffer on return. If we want to read anything else from the same
TcpStream after the JSON object, the buffer needs to go in the caller.
  • Loading branch information
dtolnay committed Jan 28, 2025
1 parent 29122f9 commit dc29e48
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2582,9 +2582,8 @@ where
/// location: String,
/// }
///
/// fn read_user_from_stream(tcp_stream: TcpStream) -> Result<User, Box<dyn Error>> {
/// let buf_tcp_stream = BufReader::new(tcp_stream);
/// let mut de = serde_json::Deserializer::from_reader(buf_tcp_stream);
/// fn read_user_from_stream(stream: &mut BufReader<TcpStream>) -> Result<User, Box<dyn Error>> {
/// let mut de = serde_json::Deserializer::from_reader(stream);
/// let u = User::deserialize(&mut de)?;
///
/// Ok(u)
Expand All @@ -2595,8 +2594,9 @@ where
/// # fn fake_main() {
/// let listener = TcpListener::bind("127.0.0.1:4000").unwrap();
///
/// for stream in listener.incoming() {
/// println!("{:#?}", read_user_from_stream(stream.unwrap()));
/// for tcp_stream in listener.incoming() {
/// let mut buffered = BufReader::new(tcp_stream.unwrap());
/// println!("{:#?}", read_user_from_stream(&mut buffered));
/// }
/// }
/// ```
Expand Down

0 comments on commit dc29e48

Please sign in to comment.