Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use immutable borrow of TcpStream when creating BufReader #4024

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-25/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] {
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