Skip to content

ch20-02-multithreaded call_box no longer needed #2208

Closed
@rafales

Description

@rafales

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 }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions