Skip to content

Commit 0196e97

Browse files
committed
Use OnceLock instead of once_cell::Lazy
1 parent 4287d0a commit 0196e97

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

crates/ark/src/test/dummy_frontend.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@ use std::ops::DerefMut;
33
use std::sync::Arc;
44
use std::sync::Mutex;
55
use std::sync::MutexGuard;
6+
use std::sync::OnceLock;
67

78
use amalthea::test::dummy_frontend::DummyFrontend;
8-
use once_cell::sync::Lazy;
99

1010
use crate::interface::RMain;
1111
use crate::interface::SessionMode;
1212

1313
// There can be only one frontend per process. Needs to be in a mutex because
1414
// the frontend wraps zmq sockets which are unsafe to send across threads.
1515
//
16-
// This is using `Lazy` from the `once_cell` crate instead of other standard
17-
// types because the former provides a way of checking whether it has been
18-
// initialized already.
19-
static FRONTEND: Lazy<Arc<Mutex<DummyFrontend>>> =
20-
Lazy::new(|| Arc::new(Mutex::new(DummyArkFrontend::init())));
16+
// This is using `OnceLock` because it provides a way of checking whether the
17+
// value has been initialized already. Also we'll need to parameterize
18+
// initialization in the future.
19+
static FRONTEND: OnceLock<Arc<Mutex<DummyFrontend>>> = OnceLock::new();
2120

2221
/// Wrapper around `DummyFrontend` that checks sockets are empty on drop
2322
pub struct DummyArkFrontend {
@@ -27,12 +26,16 @@ pub struct DummyArkFrontend {
2726
impl DummyArkFrontend {
2827
pub fn lock() -> Self {
2928
Self {
30-
guard: FRONTEND.lock().unwrap(),
29+
guard: Self::get_frontend().lock().unwrap(),
3130
}
3231
}
3332

33+
fn get_frontend() -> &'static Arc<Mutex<DummyFrontend>> {
34+
FRONTEND.get_or_init(|| Arc::new(Mutex::new(DummyArkFrontend::init())))
35+
}
36+
3437
fn init() -> DummyFrontend {
35-
if Lazy::get(&FRONTEND).is_some() {
38+
if FRONTEND.get().is_some() {
3639
panic!("Can't spawn Ark more than once");
3740
}
3841

0 commit comments

Comments
 (0)