Skip to content

Commit

Permalink
Use immutable borrow of TcpStream when creating BufReader
Browse files Browse the repository at this point in the history
  • Loading branch information
bin-wang committed Aug 30, 2024
1 parent e7d217b commit 5d68320
Show file tree
Hide file tree
Showing 29 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-02/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-03/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-05/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-06/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-07/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-09/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-10/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-11/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-12/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-13/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-14/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-15/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-16/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-17/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-18/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-19/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-20/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-21/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-22/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-23/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-24/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
Expand Down
4 changes: 2 additions & 2 deletions src/ch20-01-single-threaded.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5d68320

Please sign in to comment.