Book: ch20-02-multithreaded call_box no longer needed

Created on 5 Jan 2020  路  1Comment  路  Source: rust-lang/book

There is a huge section about working around this problem:

error[E0161]: cannot move a value of type std::ops::FnOnce() +
std::marker::Send: the size of std::ops::FnOnce() + std::marker::Send cannot be
statically determined
  --> src/lib.rs:63:17
   |
63 |                 (*job)();
   |                 ^^^^^^

Currently (rustc 1.40.0 (73528e339 2019-12-16)) you can just do job() and it works perfectly, no need for FnBox and call_box workaround anymore.

Final code for Worker impl:

type Job = Box<dyn FnOnce() + Send + 'static>;

// --snip --

impl Worker {
    fn new(id: u32, 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();
        });
        Worker { id, thread }
    }
}

Most helpful comment

Thanks! This was fixed in https://github.com/rust-lang/book/pull/1906, but the book rides the release trains. The fix is available on https://doc.rust-lang.org/beta/book/ch20-02-multithreaded.html, and it will be available on https://doc.rust-lang.org/stable/book/ch20-02-multithreaded.html with the release of Rust 1.41.

>All comments

Thanks! This was fixed in https://github.com/rust-lang/book/pull/1906, but the book rides the release trains. The fix is available on https://doc.rust-lang.org/beta/book/ch20-02-multithreaded.html, and it will be available on https://doc.rust-lang.org/stable/book/ch20-02-multithreaded.html with the release of Rust 1.41.

Was this page helpful?
0 / 5 - 0 ratings