diff --git a/listings/ch21-web-server/no-listing-05-fix-worker-new/404.html b/listings/ch21-web-server/no-listing-05-final-code/404.html similarity index 100% rename from listings/ch21-web-server/no-listing-05-fix-worker-new/404.html rename to listings/ch21-web-server/no-listing-05-final-code/404.html diff --git a/listings/ch21-web-server/no-listing-05-fix-worker-new/Cargo.lock b/listings/ch21-web-server/no-listing-05-final-code/Cargo.lock similarity index 100% rename from listings/ch21-web-server/no-listing-05-fix-worker-new/Cargo.lock rename to listings/ch21-web-server/no-listing-05-final-code/Cargo.lock diff --git a/listings/ch21-web-server/no-listing-05-fix-worker-new/Cargo.toml b/listings/ch21-web-server/no-listing-05-final-code/Cargo.toml similarity index 100% rename from listings/ch21-web-server/no-listing-05-fix-worker-new/Cargo.toml rename to listings/ch21-web-server/no-listing-05-final-code/Cargo.toml diff --git a/listings/ch21-web-server/no-listing-05-fix-worker-new/hello.html b/listings/ch21-web-server/no-listing-05-final-code/hello.html similarity index 100% rename from listings/ch21-web-server/no-listing-05-fix-worker-new/hello.html rename to listings/ch21-web-server/no-listing-05-final-code/hello.html diff --git a/listings/ch21-web-server/no-listing-07-final-code/src/lib.rs b/listings/ch21-web-server/no-listing-05-final-code/src/lib.rs similarity index 100% rename from listings/ch21-web-server/no-listing-07-final-code/src/lib.rs rename to listings/ch21-web-server/no-listing-05-final-code/src/lib.rs diff --git a/listings/ch21-web-server/no-listing-06-fix-threadpool-drop/src/main.rs b/listings/ch21-web-server/no-listing-05-final-code/src/main.rs similarity index 100% rename from listings/ch21-web-server/no-listing-06-fix-threadpool-drop/src/main.rs rename to listings/ch21-web-server/no-listing-05-final-code/src/main.rs diff --git a/listings/ch21-web-server/no-listing-05-fix-worker-new/src/lib.rs b/listings/ch21-web-server/no-listing-05-fix-worker-new/src/lib.rs deleted file mode 100644 index 31237a61a6..0000000000 --- a/listings/ch21-web-server/no-listing-05-fix-worker-new/src/lib.rs +++ /dev/null @@ -1,85 +0,0 @@ -use std::{ - sync::{Arc, Mutex, mpsc}, - thread, -}; - -pub struct ThreadPool { - workers: Vec, - sender: mpsc::Sender, -} - -type Job = Box; - -impl ThreadPool { - /// Create a new ThreadPool. - /// - /// The size is the number of threads in the pool. - /// - /// # Panics - /// - /// The `new` function will panic if the size is zero. - pub fn new(size: usize) -> ThreadPool { - assert!(size > 0); - - let (sender, receiver) = mpsc::channel(); - - let receiver = Arc::new(Mutex::new(receiver)); - - let mut workers = Vec::with_capacity(size); - - for id in 0..size { - workers.push(Worker::new(id, Arc::clone(&receiver))); - } - - ThreadPool { workers, sender } - } - - pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static, - { - let job = Box::new(f); - - self.sender.send(job).unwrap(); - } -} - -impl Drop for ThreadPool { - fn drop(&mut self) { - for worker in &mut self.workers { - println!("Shutting down worker {}", worker.id); - - worker.thread.join().unwrap(); - } - } -} - -struct Worker { - id: usize, - thread: Option>, -} - -// ANCHOR: here -impl Worker { - fn new(id: usize, receiver: Arc>>) -> Worker { - // --snip-- - - // ANCHOR_END: here - let thread = thread::spawn(move || { - loop { - let job = receiver.lock().unwrap().recv().unwrap(); - - println!("Worker {id} got a job; executing."); - - job(); - } - }); - - // ANCHOR: here - Worker { - id, - thread: Some(thread), - } - } -} -// ANCHOR_END: here diff --git a/listings/ch21-web-server/no-listing-05-fix-worker-new/src/main.rs b/listings/ch21-web-server/no-listing-05-fix-worker-new/src/main.rs deleted file mode 100644 index ca3608da70..0000000000 --- a/listings/ch21-web-server/no-listing-05-fix-worker-new/src/main.rs +++ /dev/null @@ -1,43 +0,0 @@ -use hello::ThreadPool; -use std::{ - fs, - io::{BufReader, prelude::*}, - net::{TcpListener, TcpStream}, - thread, - time::Duration, -}; - -fn main() { - let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); - let pool = ThreadPool::new(4); - - for stream in listener.incoming() { - let stream = stream.unwrap(); - - pool.execute(|| { - handle_connection(stream); - }); - } -} - -fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&stream); - let request_line = buf_reader.lines().next().unwrap().unwrap(); - - let (status_line, filename) = match &request_line[..] { - "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), - "GET /sleep HTTP/1.1" => { - thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK", "hello.html") - } - _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), - }; - - let contents = fs::read_to_string(filename).unwrap(); - let length = contents.len(); - - let response = - format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); - - stream.write_all(response.as_bytes()).unwrap(); -} diff --git a/listings/ch21-web-server/no-listing-06-fix-threadpool-drop/404.html b/listings/ch21-web-server/no-listing-06-fix-threadpool-drop/404.html deleted file mode 100644 index 88d8e9152d..0000000000 --- a/listings/ch21-web-server/no-listing-06-fix-threadpool-drop/404.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - Hello! - - -

Oops!

-

Sorry, I don't know what you're asking for.

- - diff --git a/listings/ch21-web-server/no-listing-06-fix-threadpool-drop/Cargo.lock b/listings/ch21-web-server/no-listing-06-fix-threadpool-drop/Cargo.lock deleted file mode 100644 index f2d069f462..0000000000 --- a/listings/ch21-web-server/no-listing-06-fix-threadpool-drop/Cargo.lock +++ /dev/null @@ -1,6 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -[[package]] -name = "hello" -version = "0.1.0" - diff --git a/listings/ch21-web-server/no-listing-06-fix-threadpool-drop/Cargo.toml b/listings/ch21-web-server/no-listing-06-fix-threadpool-drop/Cargo.toml deleted file mode 100644 index f6f3649e20..0000000000 --- a/listings/ch21-web-server/no-listing-06-fix-threadpool-drop/Cargo.toml +++ /dev/null @@ -1,6 +0,0 @@ -[package] -name = "hello" -version = "0.1.0" -edition = "2024" - -[dependencies] diff --git a/listings/ch21-web-server/no-listing-06-fix-threadpool-drop/hello.html b/listings/ch21-web-server/no-listing-06-fix-threadpool-drop/hello.html deleted file mode 100644 index fe442d6b9b..0000000000 --- a/listings/ch21-web-server/no-listing-06-fix-threadpool-drop/hello.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - Hello! - - -

Hello!

-

Hi from Rust

- - diff --git a/listings/ch21-web-server/no-listing-06-fix-threadpool-drop/src/lib.rs b/listings/ch21-web-server/no-listing-06-fix-threadpool-drop/src/lib.rs deleted file mode 100644 index 9fb67e0186..0000000000 --- a/listings/ch21-web-server/no-listing-06-fix-threadpool-drop/src/lib.rs +++ /dev/null @@ -1,83 +0,0 @@ -use std::{ - sync::{Arc, Mutex, mpsc}, - thread, -}; - -pub struct ThreadPool { - workers: Vec, - sender: mpsc::Sender, -} - -type Job = Box; - -impl ThreadPool { - /// Create a new ThreadPool. - /// - /// The size is the number of threads in the pool. - /// - /// # Panics - /// - /// The `new` function will panic if the size is zero. - pub fn new(size: usize) -> ThreadPool { - assert!(size > 0); - - let (sender, receiver) = mpsc::channel(); - - let receiver = Arc::new(Mutex::new(receiver)); - - let mut workers = Vec::with_capacity(size); - - for id in 0..size { - workers.push(Worker::new(id, Arc::clone(&receiver))); - } - - ThreadPool { workers, sender } - } - - pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static, - { - let job = Box::new(f); - - self.sender.send(job).unwrap(); - } -} - -// ANCHOR: here -impl Drop for ThreadPool { - fn drop(&mut self) { - for worker in &mut self.workers { - println!("Shutting down worker {}", worker.id); - - if let Some(thread) = worker.thread.take() { - thread.join().unwrap(); - } - } - } -} -// ANCHOR_END: here - -struct Worker { - id: usize, - thread: Option>, -} - -impl Worker { - fn new(id: usize, receiver: Arc>>) -> Worker { - let thread = thread::spawn(move || { - loop { - let job = receiver.lock().unwrap().recv().unwrap(); - - println!("Worker {id} got a job; executing."); - - job(); - } - }); - - Worker { - id, - thread: Some(thread), - } - } -} diff --git a/listings/ch21-web-server/no-listing-07-final-code/404.html b/listings/ch21-web-server/no-listing-07-final-code/404.html deleted file mode 100644 index 88d8e9152d..0000000000 --- a/listings/ch21-web-server/no-listing-07-final-code/404.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - Hello! - - -

Oops!

-

Sorry, I don't know what you're asking for.

- - diff --git a/listings/ch21-web-server/no-listing-07-final-code/Cargo.lock b/listings/ch21-web-server/no-listing-07-final-code/Cargo.lock deleted file mode 100644 index f2d069f462..0000000000 --- a/listings/ch21-web-server/no-listing-07-final-code/Cargo.lock +++ /dev/null @@ -1,6 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -[[package]] -name = "hello" -version = "0.1.0" - diff --git a/listings/ch21-web-server/no-listing-07-final-code/Cargo.toml b/listings/ch21-web-server/no-listing-07-final-code/Cargo.toml deleted file mode 100644 index f6f3649e20..0000000000 --- a/listings/ch21-web-server/no-listing-07-final-code/Cargo.toml +++ /dev/null @@ -1,6 +0,0 @@ -[package] -name = "hello" -version = "0.1.0" -edition = "2024" - -[dependencies] diff --git a/listings/ch21-web-server/no-listing-07-final-code/hello.html b/listings/ch21-web-server/no-listing-07-final-code/hello.html deleted file mode 100644 index fe442d6b9b..0000000000 --- a/listings/ch21-web-server/no-listing-07-final-code/hello.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - Hello! - - -

Hello!

-

Hi from Rust

- - diff --git a/listings/ch21-web-server/no-listing-07-final-code/src/main.rs b/listings/ch21-web-server/no-listing-07-final-code/src/main.rs deleted file mode 100644 index f68c11e0b2..0000000000 --- a/listings/ch21-web-server/no-listing-07-final-code/src/main.rs +++ /dev/null @@ -1,45 +0,0 @@ -use hello::ThreadPool; -use std::{ - fs, - io::{BufReader, prelude::*}, - net::{TcpListener, TcpStream}, - thread, - time::Duration, -}; - -fn main() { - let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); - let pool = ThreadPool::new(4); - - for stream in listener.incoming().take(2) { - let stream = stream.unwrap(); - - pool.execute(|| { - handle_connection(stream); - }); - } - - println!("Shutting down."); -} - -fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&stream); - let request_line = buf_reader.lines().next().unwrap().unwrap(); - - let (status_line, filename) = match &request_line[..] { - "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), - "GET /sleep HTTP/1.1" => { - thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK", "hello.html") - } - _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), - }; - - let contents = fs::read_to_string(filename).unwrap(); - let length = contents.len(); - - let response = - format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); - - stream.write_all(response.as_bytes()).unwrap(); -} diff --git a/src/ch21-03-graceful-shutdown-and-cleanup.md b/src/ch21-03-graceful-shutdown-and-cleanup.md index cfce45283b..ff30ee4568 100644 --- a/src/ch21-03-graceful-shutdown-and-cleanup.md +++ b/src/ch21-03-graceful-shutdown-and-cleanup.md @@ -205,7 +205,7 @@ Here’s the full code for reference: ```rust,ignore -{{#rustdoc_include ../listings/ch21-web-server/no-listing-07-final-code/src/main.rs}} +{{#rustdoc_include ../listings/ch21-web-server/no-listing-05-final-code/src/main.rs}} ``` @@ -213,7 +213,7 @@ Here’s the full code for reference: ```rust,noplayground -{{#rustdoc_include ../listings/ch21-web-server/no-listing-07-final-code/src/lib.rs}} +{{#rustdoc_include ../listings/ch21-web-server/no-listing-05-final-code/src/lib.rs}} ```