1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-24 00:01:11 +01:00

add System::try_current (#275)

This commit is contained in:
Rob Ede 2021-02-06 22:45:03 +00:00 committed by GitHub
parent eb4d29e15e
commit 32543809f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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. [#275]
[#274]: https://github.com/actix/actix-net/pull/274 [#274]: https://github.com/actix/actix-net/pull/274
[#275]: https://github.com/actix/actix-net/pull/275
## 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()) });
}