[RustBook] [Solved] WebServer Multithreaded tutorial compilation errors

Hi,

I’m trying to follow the web server tutorial of 2018 rust book edition. i’m getting the following errors while trying to apply the Function boxing trick :

error[E0411]: expected trait, found self type `Self`
--> src/lib.rs:47:31
|
47 |     fn call_box(self: Box<dyn Self>);
|                               ^^^^ `Self` is only available in traits and impls

error[E0404]: expected trait, found type parameter `F`
--> src/lib.rs:51:31
|
51 |     fn call_box(self: Box<dyn F>) {
|                               ^ did you mean `Fn`?

for the following code :

trait FnBox {
    fn call_box(self: Box<dyn Self>);
}

impl<F: FnOnce() -> ()> FnBox for F {
    fn call_box(self: Box<dyn F>) {
        (*self)()
    }
}

Here is the complete file :

use std::sync::mpsc;
use std::sync::Arc;
use std::sync::Mutex;
use std::thread;


pub struct ThreadPool {
    workers: Vec<Worker>,
    sender: mpsc::Sender<Job>,
}

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<F>(&self, f: F)
    where
        F: FnOnce() + Send + 'static,
    {
        let job = Box::new(f);

        self.sender.send(job).unwrap();
    }
}

trait FnBox {
    fn call_box(self: Box<dyn Self>);
}

impl<F: FnOnce() -> ()> FnBox for F {
    fn call_box(self: Box<dyn F>) {
        (*self)()
    }
}

type Job = Box<dyn FnBox + Send + 'static>;

struct Worker {
    id: usize,
    thread: thread::JoinHandle<()>,
}

impl Worker {
    fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
        let thread = thread::spawn(move || loop {
            let job = receiver.lock().unwrap().recv().unwrap();

            println!("Worker {} got a job; executing.", id);

            job.call_box();
        });

        Worker { id, thread }
    }
}

I understand what the errors are saying but i don’t see why the tutorial code generates those. Am i using the wrong rust verstion (stable 1.28) or have i made a dumb mistake i can’t see ?

Thanks in advance

fn call_box(self: Box<dyn Self>);

That should be Box<Self>, not Box<dyn Self>; same for the impl instead of Box<dyn F>.

Thanks a lot that solved my problem. The github master version of the book was correct.

To be honest i'm still far away from being able to understand the code though...