From c648c0e8daa4256d937e5eb12b7f2823f3d19031 Mon Sep 17 00:00:00 2001 From: Bin Wang Date: Fri, 30 Aug 2024 13:41:54 -0400 Subject: [PATCH] Use immutable borrow of `TcpStream` when creating `BufReader` --- listings/ch20-web-server/listing-20-02/src/main.rs | 2 +- listings/ch20-web-server/listing-20-03/src/main.rs | 2 +- listings/ch20-web-server/listing-20-05/src/main.rs | 2 +- listings/ch20-web-server/listing-20-06/src/main.rs | 2 +- listings/ch20-web-server/listing-20-07/src/main.rs | 2 +- listings/ch20-web-server/listing-20-09/src/main.rs | 2 +- listings/ch20-web-server/listing-20-10/src/main.rs | 2 +- listings/ch20-web-server/listing-20-11/src/main.rs | 2 +- listings/ch20-web-server/listing-20-12/src/main.rs | 2 +- listings/ch20-web-server/listing-20-13/src/main.rs | 2 +- listings/ch20-web-server/listing-20-14/src/main.rs | 2 +- listings/ch20-web-server/listing-20-15/src/main.rs | 2 +- listings/ch20-web-server/listing-20-16/src/main.rs | 2 +- listings/ch20-web-server/listing-20-17/src/main.rs | 2 +- listings/ch20-web-server/listing-20-18/src/main.rs | 2 +- listings/ch20-web-server/listing-20-19/src/main.rs | 2 +- listings/ch20-web-server/listing-20-20/src/main.rs | 2 +- listings/ch20-web-server/listing-20-21/src/main.rs | 2 +- listings/ch20-web-server/listing-20-22/src/main.rs | 2 +- listings/ch20-web-server/listing-20-23/src/main.rs | 2 +- listings/ch20-web-server/listing-20-24/src/main.rs | 2 +- listings/ch20-web-server/listing-20-25/src/main.rs | 2 +- .../no-listing-01-define-threadpool-struct/src/main.rs | 2 +- .../no-listing-02-impl-threadpool-new/src/main.rs | 2 +- .../ch20-web-server/no-listing-03-define-execute/src/main.rs | 2 +- .../no-listing-04-update-worker-definition/src/main.rs | 2 +- .../ch20-web-server/no-listing-05-fix-worker-new/src/main.rs | 2 +- .../no-listing-06-fix-threadpool-drop/src/main.rs | 2 +- listings/ch20-web-server/no-listing-07-final-code/src/main.rs | 2 +- src/ch20-01-single-threaded.md | 4 ++-- 30 files changed, 31 insertions(+), 31 deletions(-) diff --git a/listings/ch20-web-server/listing-20-02/src/main.rs b/listings/ch20-web-server/listing-20-02/src/main.rs index f139846c6d..1eff2eef3a 100644 --- a/listings/ch20-web-server/listing-20-02/src/main.rs +++ b/listings/ch20-web-server/listing-20-02/src/main.rs @@ -14,7 +14,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let http_request: Vec<_> = buf_reader .lines() .map(|result| result.unwrap()) diff --git a/listings/ch20-web-server/listing-20-03/src/main.rs b/listings/ch20-web-server/listing-20-03/src/main.rs index c72d4a9c67..17c8401d4d 100644 --- a/listings/ch20-web-server/listing-20-03/src/main.rs +++ b/listings/ch20-web-server/listing-20-03/src/main.rs @@ -15,7 +15,7 @@ fn main() { // ANCHOR: here fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let http_request: Vec<_> = buf_reader .lines() .map(|result| result.unwrap()) diff --git a/listings/ch20-web-server/listing-20-05/src/main.rs b/listings/ch20-web-server/listing-20-05/src/main.rs index d4b78b640e..437d9b626e 100644 --- a/listings/ch20-web-server/listing-20-05/src/main.rs +++ b/listings/ch20-web-server/listing-20-05/src/main.rs @@ -19,7 +19,7 @@ fn main() { // ANCHOR: here fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let http_request: Vec<_> = buf_reader .lines() .map(|result| result.unwrap()) diff --git a/listings/ch20-web-server/listing-20-06/src/main.rs b/listings/ch20-web-server/listing-20-06/src/main.rs index 5523a42d7c..843d846766 100644 --- a/listings/ch20-web-server/listing-20-06/src/main.rs +++ b/listings/ch20-web-server/listing-20-06/src/main.rs @@ -17,7 +17,7 @@ fn main() { // --snip-- fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); if request_line == "GET / HTTP/1.1" { diff --git a/listings/ch20-web-server/listing-20-07/src/main.rs b/listings/ch20-web-server/listing-20-07/src/main.rs index a14b7d538c..e19467b83b 100644 --- a/listings/ch20-web-server/listing-20-07/src/main.rs +++ b/listings/ch20-web-server/listing-20-07/src/main.rs @@ -15,7 +15,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); if request_line == "GET / HTTP/1.1" { diff --git a/listings/ch20-web-server/listing-20-09/src/main.rs b/listings/ch20-web-server/listing-20-09/src/main.rs index ffc51e803e..0f9356ab02 100644 --- a/listings/ch20-web-server/listing-20-09/src/main.rs +++ b/listings/ch20-web-server/listing-20-09/src/main.rs @@ -19,7 +19,7 @@ fn main() { fn handle_connection(mut stream: TcpStream) { // --snip-- // ANCHOR_END: here - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); // ANCHOR: here diff --git a/listings/ch20-web-server/listing-20-10/src/main.rs b/listings/ch20-web-server/listing-20-10/src/main.rs index 5a18b45c02..0679ec6002 100644 --- a/listings/ch20-web-server/listing-20-10/src/main.rs +++ b/listings/ch20-web-server/listing-20-10/src/main.rs @@ -24,7 +24,7 @@ fn handle_connection(mut stream: TcpStream) { // --snip-- // ANCHOR_END: here - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); // ANCHOR: here diff --git a/listings/ch20-web-server/listing-20-11/src/main.rs b/listings/ch20-web-server/listing-20-11/src/main.rs index 1181357b0e..075a0d9de6 100644 --- a/listings/ch20-web-server/listing-20-11/src/main.rs +++ b/listings/ch20-web-server/listing-20-11/src/main.rs @@ -21,7 +21,7 @@ fn main() { // ANCHOR_END: here fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/listing-20-12/src/main.rs b/listings/ch20-web-server/listing-20-12/src/main.rs index 21b9a80f10..bde3e29d10 100644 --- a/listings/ch20-web-server/listing-20-12/src/main.rs +++ b/listings/ch20-web-server/listing-20-12/src/main.rs @@ -22,7 +22,7 @@ fn main() { // ANCHOR_END: here fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/listing-20-13/src/main.rs b/listings/ch20-web-server/listing-20-13/src/main.rs index 79efb28a2a..1f075fde59 100644 --- a/listings/ch20-web-server/listing-20-13/src/main.rs +++ b/listings/ch20-web-server/listing-20-13/src/main.rs @@ -21,7 +21,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/listing-20-14/src/main.rs b/listings/ch20-web-server/listing-20-14/src/main.rs index 79efb28a2a..1f075fde59 100644 --- a/listings/ch20-web-server/listing-20-14/src/main.rs +++ b/listings/ch20-web-server/listing-20-14/src/main.rs @@ -21,7 +21,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/listing-20-15/src/main.rs b/listings/ch20-web-server/listing-20-15/src/main.rs index 79efb28a2a..1f075fde59 100644 --- a/listings/ch20-web-server/listing-20-15/src/main.rs +++ b/listings/ch20-web-server/listing-20-15/src/main.rs @@ -21,7 +21,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/listing-20-16/src/main.rs b/listings/ch20-web-server/listing-20-16/src/main.rs index 79efb28a2a..1f075fde59 100644 --- a/listings/ch20-web-server/listing-20-16/src/main.rs +++ b/listings/ch20-web-server/listing-20-16/src/main.rs @@ -21,7 +21,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/listing-20-17/src/main.rs b/listings/ch20-web-server/listing-20-17/src/main.rs index 79efb28a2a..1f075fde59 100644 --- a/listings/ch20-web-server/listing-20-17/src/main.rs +++ b/listings/ch20-web-server/listing-20-17/src/main.rs @@ -21,7 +21,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/listing-20-18/src/main.rs b/listings/ch20-web-server/listing-20-18/src/main.rs index 79efb28a2a..1f075fde59 100644 --- a/listings/ch20-web-server/listing-20-18/src/main.rs +++ b/listings/ch20-web-server/listing-20-18/src/main.rs @@ -21,7 +21,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/listing-20-19/src/main.rs b/listings/ch20-web-server/listing-20-19/src/main.rs index 79efb28a2a..1f075fde59 100644 --- a/listings/ch20-web-server/listing-20-19/src/main.rs +++ b/listings/ch20-web-server/listing-20-19/src/main.rs @@ -21,7 +21,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/listing-20-20/src/main.rs b/listings/ch20-web-server/listing-20-20/src/main.rs index 79efb28a2a..1f075fde59 100644 --- a/listings/ch20-web-server/listing-20-20/src/main.rs +++ b/listings/ch20-web-server/listing-20-20/src/main.rs @@ -21,7 +21,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/listing-20-21/src/main.rs b/listings/ch20-web-server/listing-20-21/src/main.rs index 79efb28a2a..1f075fde59 100644 --- a/listings/ch20-web-server/listing-20-21/src/main.rs +++ b/listings/ch20-web-server/listing-20-21/src/main.rs @@ -21,7 +21,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/listing-20-22/src/main.rs b/listings/ch20-web-server/listing-20-22/src/main.rs index 79efb28a2a..1f075fde59 100644 --- a/listings/ch20-web-server/listing-20-22/src/main.rs +++ b/listings/ch20-web-server/listing-20-22/src/main.rs @@ -21,7 +21,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/listing-20-23/src/main.rs b/listings/ch20-web-server/listing-20-23/src/main.rs index b6aa046d1b..acd703f274 100644 --- a/listings/ch20-web-server/listing-20-23/src/main.rs +++ b/listings/ch20-web-server/listing-20-23/src/main.rs @@ -23,7 +23,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/listing-20-24/src/main.rs b/listings/ch20-web-server/listing-20-24/src/main.rs index b6aa046d1b..acd703f274 100644 --- a/listings/ch20-web-server/listing-20-24/src/main.rs +++ b/listings/ch20-web-server/listing-20-24/src/main.rs @@ -23,7 +23,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/listing-20-25/src/main.rs b/listings/ch20-web-server/listing-20-25/src/main.rs index 86e8d9e78c..ee0f1efe74 100644 --- a/listings/ch20-web-server/listing-20-25/src/main.rs +++ b/listings/ch20-web-server/listing-20-25/src/main.rs @@ -25,7 +25,7 @@ fn main() { // ANCHOR_END: here fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/main.rs b/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/main.rs index f7b42167f9..e68f72c700 100644 --- a/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/main.rs +++ b/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/main.rs @@ -23,7 +23,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/main.rs b/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/main.rs index 79efb28a2a..1f075fde59 100644 --- a/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/main.rs +++ b/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/main.rs @@ -21,7 +21,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/no-listing-03-define-execute/src/main.rs b/listings/ch20-web-server/no-listing-03-define-execute/src/main.rs index 79efb28a2a..1f075fde59 100644 --- a/listings/ch20-web-server/no-listing-03-define-execute/src/main.rs +++ b/listings/ch20-web-server/no-listing-03-define-execute/src/main.rs @@ -21,7 +21,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/no-listing-04-update-worker-definition/src/main.rs b/listings/ch20-web-server/no-listing-04-update-worker-definition/src/main.rs index 79efb28a2a..1f075fde59 100644 --- a/listings/ch20-web-server/no-listing-04-update-worker-definition/src/main.rs +++ b/listings/ch20-web-server/no-listing-04-update-worker-definition/src/main.rs @@ -21,7 +21,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/no-listing-05-fix-worker-new/src/main.rs b/listings/ch20-web-server/no-listing-05-fix-worker-new/src/main.rs index 79efb28a2a..1f075fde59 100644 --- a/listings/ch20-web-server/no-listing-05-fix-worker-new/src/main.rs +++ b/listings/ch20-web-server/no-listing-05-fix-worker-new/src/main.rs @@ -21,7 +21,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/main.rs b/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/main.rs index b6aa046d1b..acd703f274 100644 --- a/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/main.rs +++ b/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/main.rs @@ -23,7 +23,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/listings/ch20-web-server/no-listing-07-final-code/src/main.rs b/listings/ch20-web-server/no-listing-07-final-code/src/main.rs index b6aa046d1b..acd703f274 100644 --- a/listings/ch20-web-server/no-listing-07-final-code/src/main.rs +++ b/listings/ch20-web-server/no-listing-07-final-code/src/main.rs @@ -23,7 +23,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&mut stream); + let buf_reader = BufReader::new(&stream); let request_line = buf_reader.lines().next().unwrap().unwrap(); let (status_line, filename) = match &request_line[..] { diff --git a/src/ch20-01-single-threaded.md b/src/ch20-01-single-threaded.md index 9e7fe865b1..b474d926fe 100644 --- a/src/ch20-01-single-threaded.md +++ b/src/ch20-01-single-threaded.md @@ -143,8 +143,8 @@ connection, we now call the new `handle_connection` function and pass the `stream` to it. In the `handle_connection` function, we create a new `BufReader` instance that -wraps a mutable reference to the `stream`. `BufReader` adds buffering by -managing calls to the `std::io::Read` trait methods for us. +wraps a reference to the `stream`. `BufReader` adds buffering by managing calls +to the `std::io::Read` trait methods for us. We create a variable named `http_request` to collect the lines of the request the browser sends to our server. We indicate that we want to collect these