diff --git a/actix-rt/CHANGES.md b/actix-rt/CHANGES.md index 05613e81..74e94aed 100644 --- a/actix-rt/CHANGES.md +++ b/actix-rt/CHANGES.md @@ -1,6 +1,9 @@ # Changes ## Unreleased - 2021-xx-xx +* Add `Arbiter::handle` to get a handle of an owned Arbiter. [#274] + +[#274]: https://github.com/actix/actix-net/pull/274 ## 2.0.1 - 2021-02-06 diff --git a/actix-rt/src/arbiter.rs b/actix-rt/src/arbiter.rs index 7eae662a..9ff1419d 100644 --- a/actix-rt/src/arbiter.rs +++ b/actix-rt/src/arbiter.rs @@ -172,13 +172,18 @@ impl Arbiter { hnd } + /// Return a handle to the this Arbiter's message sender. + pub fn handle(&self) -> ArbiterHandle { + ArbiterHandle::new(self.tx.clone()) + } + /// Return a handle to the current thread's Arbiter's message sender. /// /// # Panics /// Panics if no Arbiter is running on the current thread. pub fn current() -> ArbiterHandle { HANDLE.with(|cell| match *cell.borrow() { - Some(ref addr) => addr.clone(), + Some(ref hnd) => hnd.clone(), None => panic!("Arbiter is not running."), }) } diff --git a/actix-rt/tests/tests.rs b/actix-rt/tests/tests.rs index 56b5e8a6..5a292b31 100644 --- a/actix-rt/tests/tests.rs +++ b/actix-rt/tests/tests.rs @@ -122,6 +122,28 @@ fn arbiter_spawn_fn_runs() { arbiter.join().unwrap(); } +#[test] +fn arbiter_handle_spawn_fn_runs() { + let sys = System::new(); + + let (tx, rx) = channel::(); + + let arbiter = Arbiter::new(); + let handle = arbiter.handle(); + drop(arbiter); + + handle.spawn_fn(move || { + tx.send(42).unwrap(); + System::current().stop() + }); + + let num = rx.recv_timeout(Duration::from_secs(2)).unwrap(); + assert_eq!(num, 42); + + handle.stop(); + sys.run().unwrap(); +} + #[test] fn arbiter_drop_no_panic_fn() { let _ = System::new();