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

ch20-02-multithreaded call_box no longer needed #2208

Closed
rafales opened this issue Jan 5, 2020 · 1 comment
Closed

ch20-02-multithreaded call_box no longer needed #2208

rafales opened this issue Jan 5, 2020 · 1 comment

Comments

@rafales
Copy link

rafales commented Jan 5, 2020

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 }
    }
}
@carols10cents
Copy link
Member

Thanks! This was fixed in #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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants