Best level to implement Drop? (TRPL listing 20-24ff)

Total Rust (though not programming) newbie here. In Ch 20 of The Rust Programming Language, the worker threads are cleaned up by implementing Drop on the ThreadPool struct. This seems logical in one sense as it's the owner. On the other hand, when I went to implement it myself, I did so on both ThreadPool & Worker, like so:

impl Drop for ThreadPool {
    fn drop(&mut self) {
        for _ in &self.workers {
            self.sender.send(Message::Terminate).unwrap();
        }
        self.workers.clear();
    }
}

impl Drop for Worker {
    fn drop(&mut self) {
        self.thread_handle.take().unwrap().join().unwrap();
    }
}

There wasn't any deep motivation behind this, just a reflexive OO-ish habit of pushing functionality close to the affected data structures. It does have one advantage, which is to skirt the whole issue of synchronising receiving the Termination message with the thread joins (discussed in the book p.491). It also makes Worker a little more general.

So two questions arise:

  • have I missed something here? My method works in a bit of facile testing, but was there a reason the book might have gone the other way?
  • is there an idiomatic Rust approach to this (am I importing out-of-place OOish habits)?

Thanks.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.