Skip to content

Commit 3254380

Browse files
authored
add System::try_current (#275)
1 parent eb4d29e commit 3254380

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

actix-rt/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
## Unreleased - 2021-xx-xx
44
* Add `Arbiter::handle` to get a handle of an owned Arbiter. [#274]
5+
* Add `System::try_current` for situations where actix may or may not be running a System. [#275]
56

67
[#274]: https://github.com/actix/actix-net/pull/274
8+
[#275]: https://github.com/actix/actix-net/pull/275
79

810

911
## 2.0.1 - 2021-02-06

actix-rt/src/system.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ impl System {
100100
})
101101
}
102102

103+
/// Try to get current running system.
104+
///
105+
/// Returns `None` if no System has been started.
106+
///
107+
/// Contrary to `current`, this never panics.
108+
pub fn try_current() -> Option<System> {
109+
CURRENT.with(|cell| cell.borrow().clone())
110+
}
111+
103112
/// Get handle to a the System's initial [Arbiter].
104113
pub fn arbiter(&self) -> &ArbiterHandle {
105114
&self.arbiter_handle

actix-rt/tests/tests.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,13 @@ fn new_arbiter_with_tokio() {
288288

289289
assert_eq!(false, counter.load(Ordering::SeqCst));
290290
}
291+
292+
#[test]
293+
fn try_current_no_system() {
294+
assert!(System::try_current().is_none())
295+
}
296+
297+
#[test]
298+
fn try_current_with_system() {
299+
System::new().block_on(async { assert!(System::try_current().is_some()) });
300+
}

0 commit comments

Comments
 (0)