pantomime-rs / pantomime

MIT License
5 stars 1 forks source link

Deferred feature #21

Closed longshorej closed 5 years ago

longshorej commented 5 years ago

(name tbd)

Deferred would take a dispatcher::Thunk and invoke it when it's dropped. This can make bridging threads and actors a little nicer. I've had to implement this pattern a few times .

e.g.

#[allow(unused_variables)]
let deferred = Deferred::new(move || {
  // do some cleanup stuff
});

// other code

which would be an alternative to doing:

struct Hook { ... fields ... };

impl Drop for Hook {
  fn drop(&mut self) {
    // do some cleanup stuff
  }
}

#[allow(unused_variables)]
let deferred_ = Hook { .. };

// do stuff

note that in both cases, the "cleanup" code is invoked whether the thread panics or not.

longshorej commented 5 years ago

Fixed in https://github.com/pantomime-rs/pantomime/pull/23