add system::try_current

This commit is contained in:
Rob Ede 2021-02-06 22:11:56 +00:00
parent eb4d29e15e
commit a7acacbff7
No known key found for this signature in database
GPG Key ID: C2A3B36E841A91E6
3 changed files with 21 additions and 0 deletions

View File

@ -2,8 +2,10 @@
## Unreleased - 2021-xx-xx ## Unreleased - 2021-xx-xx
* Add `Arbiter::handle` to get a handle of an owned Arbiter. [#274] * Add `Arbiter::handle` to get a handle of an owned Arbiter. [#274]
* Add `System::try_current` for situations where actix may or may not be running a System. [#???]
[#274]: https://github.com/actix/actix-net/pull/274 [#274]: https://github.com/actix/actix-net/pull/274
[#???]: https://github.com/actix/actix-net/pull/???
## 2.0.1 - 2021-02-06 ## 2.0.1 - 2021-02-06

View File

@ -100,6 +100,15 @@ impl System {
}) })
} }
/// Try to get current running system.
///
/// Returns `None` if no System has been started.
///
/// Contrary to `current`, this never panics.
pub fn try_current() -> Option<System> {
CURRENT.with(|cell| cell.borrow().clone())
}
/// Get handle to a the System's initial [Arbiter]. /// Get handle to a the System's initial [Arbiter].
pub fn arbiter(&self) -> &ArbiterHandle { pub fn arbiter(&self) -> &ArbiterHandle {
&self.arbiter_handle &self.arbiter_handle

View File

@ -288,3 +288,13 @@ fn new_arbiter_with_tokio() {
assert_eq!(false, counter.load(Ordering::SeqCst)); assert_eq!(false, counter.load(Ordering::SeqCst));
} }
#[test]
fn try_current_no_system() {
assert!(System::try_current().is_none())
}
#[test]
fn try_current_with_system() {
System::new().block_on(async { assert!(System::try_current().is_some()) });
}