Skip to content

Commit dd608c9

Browse files
committed
clean: remove lazy_static
1 parent 952de99 commit dd608c9

File tree

11 files changed

+69
-82
lines changed

11 files changed

+69
-82
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ bevy_render = { git = "https://github.com/Schmarni-Dev/bevy", branch = "render_t
7171
[dependencies]
7272
# small utility thingys
7373
nanoid = "0.4.0"
74-
lazy_static = "1.5.0"
7574
rand = "0.9.2"
7675
rustc-hash = "2.0.0"
7776
slotmap = "1.0.7"

codegen/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,15 +357,15 @@ fn generate_alias_info(aspect: &Aspect) -> TokenStream {
357357
.fold(quote!(), |a, b| quote!(#a + #b));
358358

359359
quote! {
360-
lazy_static::lazy_static! {
361-
#[allow(clippy::all)]
362-
pub static ref #aspect_alias_info_name: crate::nodes::alias::AliasInfo = crate::nodes::alias::AliasInfo {
360+
#[allow(clippy::all)]
361+
pub static #aspect_alias_info_name: std::sync::LazyLock<crate::nodes::alias::AliasInfo> = std::sync::LazyLock::new(|| {
362+
crate::nodes::alias::AliasInfo {
363363
server_signals: vec![#local_signals],
364364
server_methods: vec![#local_methods],
365365
client_signals: vec![#remote_signals],
366366
}
367-
#inherits;
368-
}
367+
#inherits
368+
});
369369
}
370370
}
371371

src/core/client.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use crate::{
1212
};
1313
use color_eyre::eyre::{Result, eyre};
1414
use global_counter::primitive::exact::CounterU32;
15-
use lazy_static::lazy_static;
1615
use parking_lot::Mutex;
1716
use rustc_hash::FxHashMap;
1817
use stardust_xr_wire::messenger::{self, MessageSenderHandle};
@@ -21,17 +20,18 @@ use std::{
2120
fs,
2221
iter::FromIterator,
2322
path::PathBuf,
24-
sync::{Arc, OnceLock},
23+
sync::{Arc, LazyLock, OnceLock},
2524
time::Instant,
2625
};
2726
use tokio::{net::UnixStream, sync::watch, task::JoinHandle};
2827
use tracing::info;
2928

3029
pub static CLIENTS: OwnedRegistry<Client> = OwnedRegistry::new();
3130

32-
lazy_static! {
33-
static ref INTERNAL_CLIENT_MESSAGE_TIMES: (watch::Sender<Instant>, watch::Receiver<Instant>) = watch::channel(Instant::now());
34-
pub static ref INTERNAL_CLIENT: Arc<Client> = CLIENTS.add(Client {
31+
static INTERNAL_CLIENT_MESSAGE_TIMES: LazyLock<(watch::Sender<Instant>, watch::Receiver<Instant>)> =
32+
LazyLock::new(|| watch::channel(Instant::now()));
33+
pub static INTERNAL_CLIENT: LazyLock<Arc<Client>> = LazyLock::new(|| {
34+
CLIENTS.add(Client {
3535
pid: None,
3636
// env: None,
3737
exe: None,
@@ -47,8 +47,8 @@ lazy_static! {
4747
root: OnceLock::new(),
4848
base_resource_prefixes: Default::default(),
4949
state: OnceLock::default(),
50-
});
51-
}
50+
})
51+
});
5252
pub fn tick_internal_client() {
5353
let _ = INTERNAL_CLIENT_MESSAGE_TIMES.0.send(Instant::now());
5454
}
@@ -63,7 +63,7 @@ pub fn get_env(pid: i32) -> Result<FxHashMap<String, String>, std::io::Error> {
6363
}
6464
pub fn state(env: &FxHashMap<String, String>) -> Option<Arc<ClientStateParsed>> {
6565
let token = env.get("STARDUST_STARTUP_TOKEN")?;
66-
CLIENT_STATES.lock().get(token).cloned()
66+
CLIENT_STATES.get(token).as_deref().cloned()
6767
}
6868

6969
pub struct Client {

src/core/client_state.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@ use crate::{
33
core::Id,
44
nodes::{Node, root::ClientState, spatial::Spatial},
55
};
6+
use dashmap::DashMap;
67
use glam::Mat4;
7-
use parking_lot::Mutex;
88
use rustc_hash::FxHashMap;
99
use serde::{Deserialize, Serialize};
1010
use std::{
1111
path::{Path, PathBuf},
1212
process::Command,
13-
sync::Arc,
13+
sync::{Arc, LazyLock},
1414
};
1515

16-
lazy_static::lazy_static! {
17-
pub static ref CLIENT_STATES: Mutex<FxHashMap<String, Arc<ClientStateParsed>>> = Default::default();
18-
}
16+
pub static CLIENT_STATES: LazyLock<DashMap<String, Arc<ClientStateParsed>>> =
17+
LazyLock::new(Default::default);
1918

2019
#[derive(Debug, Serialize, Deserialize)]
2120
pub struct LaunchInfo {
@@ -62,7 +61,7 @@ impl ClientStateParsed {
6261

6362
pub fn token(self) -> String {
6463
let token = nanoid::nanoid!();
65-
CLIENT_STATES.lock().insert(token.clone(), Arc::new(self));
64+
CLIENT_STATES.insert(token.clone(), Arc::new(self));
6665
token
6766
}
6867
pub fn from_file(file: &Path) -> Option<Self> {

src/nodes/fields.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use bevy::gizmos::gizmos::Gizmos;
2222
use bevy::gizmos::primitives::dim3::GizmoPrimitive3d;
2323
use bevy::math::primitives::{Cylinder, Torus};
2424
use color_eyre::eyre::OptionExt;
25+
use dashmap::DashMap;
2526
use glam::{Vec3, Vec3A, Vec3Swizzles, vec2, vec3, vec3a};
2627
use parking_lot::Mutex;
27-
use rustc_hash::FxHashMap;
2828
use stardust_xr_wire::values::Vector3;
2929
use std::sync::{Arc, LazyLock, Weak};
3030
use zbus::interface;
@@ -135,9 +135,7 @@ static FIELD_REGISTRY_DEBUG_GIZMOS: Registry<Field> = Registry::new();
135135

136136
stardust_xr_server_codegen::codegen_field_protocol!();
137137

138-
lazy_static::lazy_static! {
139-
pub static ref EXPORTED_FIELDS: Mutex<FxHashMap<u64, Weak<Node>>> = Mutex::new(FxHashMap::default());
140-
}
138+
pub static EXPORTED_FIELDS: LazyLock<DashMap<u64, Weak<Node>>> = LazyLock::new(DashMap::new);
141139

142140
pub trait FieldTrait: Send + Sync + 'static {
143141
fn spatial_ref(&self) -> &Spatial;
@@ -271,7 +269,7 @@ impl FieldAspect for Field {
271269

272270
async fn export_field(node: Arc<Node>, _calling_client: Arc<Client>) -> Result<Id> {
273271
let id = rand::random();
274-
EXPORTED_FIELDS.lock().insert(id, Arc::downgrade(&node));
272+
EXPORTED_FIELDS.insert(id, Arc::downgrade(&node));
275273
Ok(id.into())
276274
}
277275
}
@@ -370,7 +368,6 @@ impl InterfaceAspect for Interface {
370368
uid: Id,
371369
) -> Result<Id> {
372370
let node = EXPORTED_FIELDS
373-
.lock()
374371
.get(&uid.0)
375372
.and_then(|s| s.upgrade())
376373
.map(|s| {

src/nodes/items/camera.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,32 @@ use crate::{
1616
},
1717
};
1818
use glam::Mat4;
19-
use lazy_static::lazy_static;
2019
use mint::{ColumnMatrix4, Vector2};
2120
use parking_lot::Mutex;
2221
use stardust_xr_server_foundation::bail;
2322

2423
use stardust_xr_wire::flex::{deserialize, serialize};
2524
use std::sync::Arc;
25+
use std::sync::LazyLock;
2626

2727
stardust_xr_server_codegen::codegen_item_camera_protocol!();
28-
lazy_static! {
29-
pub(super) static ref ITEM_TYPE_INFO_CAMERA: TypeInfo = TypeInfo {
30-
type_name: "camera",
31-
alias_info: CAMERA_ITEM_ASPECT_ALIAS_INFO.clone(),
32-
ui_node_id: INTERFACE_NODE_ID,
33-
ui: Default::default(),
34-
items: Registry::new(),
35-
acceptors: Registry::new(),
36-
add_acceptor_aspect: |node| {
37-
node.add_aspect(CameraItemAcceptor);
38-
},
39-
add_ui_aspect: |node| {
40-
node.add_aspect(CameraItemUi);
41-
},
42-
new_acceptor_fn: |node, acceptor, acceptor_field| {
43-
let _ = camera_item_ui_client::create_acceptor(node, acceptor, acceptor_field);
44-
}
45-
};
46-
}
28+
pub(super) static ITEM_TYPE_INFO_CAMERA: LazyLock<TypeInfo> = LazyLock::new(|| TypeInfo {
29+
type_name: "camera",
30+
alias_info: CAMERA_ITEM_ASPECT_ALIAS_INFO.clone(),
31+
ui_node_id: INTERFACE_NODE_ID,
32+
ui: Default::default(),
33+
items: Registry::new(),
34+
acceptors: Registry::new(),
35+
add_acceptor_aspect: |node| {
36+
node.add_aspect(CameraItemAcceptor);
37+
},
38+
add_ui_aspect: |node| {
39+
node.add_aspect(CameraItemUi);
40+
},
41+
new_acceptor_fn: |node, acceptor, acceptor_field| {
42+
let _ = camera_item_ui_client::create_acceptor(node, acceptor, acceptor_field);
43+
},
44+
});
4745

4846
struct FrameInfo {
4947
proj_matrix: Mat4,

src/nodes/items/panel.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ use crate::{
1919
},
2020
};
2121
use glam::Mat4;
22-
use lazy_static::lazy_static;
2322
use mint::Vector2;
2423
use parking_lot::Mutex;
2524
use slotmap::{DefaultKey, Key, KeyData, SlotMap};
2625
use stardust_xr_server_foundation::bail;
27-
use std::sync::{Arc, Weak};
26+
use std::sync::{Arc, LazyLock, Weak};
2827
use tracing::debug;
2928

3029
stardust_xr_server_codegen::codegen_item_panel_protocol!();
@@ -38,26 +37,25 @@ impl Default for Geometry {
3837
}
3938
impl Copy for Geometry {}
4039

41-
lazy_static! {
42-
pub static ref KEYMAPS: Mutex<SlotMap<DefaultKey, String>> = Mutex::new(SlotMap::default());
43-
pub static ref ITEM_TYPE_INFO_PANEL: TypeInfo = TypeInfo {
44-
type_name: "panel",
45-
alias_info: PANEL_ITEM_ASPECT_ALIAS_INFO.clone(),
46-
ui_node_id: INTERFACE_NODE_ID,
47-
ui: Default::default(),
48-
items: Registry::new(),
49-
acceptors: Registry::new(),
50-
add_acceptor_aspect: |node| {
51-
node.add_aspect(PanelItemUi);
52-
},
53-
add_ui_aspect: |node| {
54-
node.add_aspect(PanelItemAcceptor);
55-
},
56-
new_acceptor_fn: |node, acceptor, acceptor_field| {
57-
let _ = panel_item_ui_client::create_acceptor(node, acceptor, acceptor_field);
58-
}
59-
};
60-
}
40+
pub static KEYMAPS: LazyLock<Mutex<SlotMap<DefaultKey, String>>> =
41+
LazyLock::new(|| Mutex::new(SlotMap::default()));
42+
pub static ITEM_TYPE_INFO_PANEL: LazyLock<TypeInfo> = LazyLock::new(|| TypeInfo {
43+
type_name: "panel",
44+
alias_info: PANEL_ITEM_ASPECT_ALIAS_INFO.clone(),
45+
ui_node_id: INTERFACE_NODE_ID,
46+
ui: Default::default(),
47+
items: Registry::new(),
48+
acceptors: Registry::new(),
49+
add_acceptor_aspect: |node| {
50+
node.add_aspect(PanelItemUi);
51+
},
52+
add_ui_aspect: |node| {
53+
node.add_aspect(PanelItemAcceptor);
54+
},
55+
new_acceptor_fn: |node, acceptor, acceptor_field| {
56+
let _ = panel_item_ui_client::create_acceptor(node, acceptor, acceptor_field);
57+
},
58+
});
6159

6260
pub trait Backend: Send + Sync + 'static {
6361
fn start_data(&self) -> Result<PanelItemInitData>;

src/nodes/spatial.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ use bevy::prelude::Transform as BevyTransform;
1111
use bevy::prelude::*;
1212
use bevy::render::primitives::Aabb;
1313
use color_eyre::eyre::OptionExt;
14+
use dashmap::DashMap;
1415
use glam::{Mat4, Quat, Vec3};
1516
use mint::Vector3;
1617
use parking_lot::{Mutex, RwLock};
17-
use rustc_hash::FxHashMap;
1818
use stardust_xr_server_foundation::bail;
1919
use std::fmt::Debug;
2020
use std::pin::Pin;
2121
use std::sync::atomic::Ordering;
22-
use std::sync::{Arc, OnceLock, Weak};
22+
use std::sync::{Arc, LazyLock, OnceLock, Weak};
2323
use std::{f32, ptr};
2424

2525
pub struct SpatialNodePlugin;
@@ -107,9 +107,9 @@ impl Transform {
107107
Mat4::from_scale_rotation_translation(scale.into(), rotation.into(), position.into())
108108
}
109109
}
110-
lazy_static::lazy_static! {
111-
pub static ref EXPORTED_SPATIALS: Mutex<FxHashMap<u64, Weak<Node>>> = Mutex::new(FxHashMap::default());
112-
}
110+
111+
// uses dashmap because it's concurrent and doesn't use fxhash because exported
112+
pub static EXPORTED_SPATIALS: LazyLock<DashMap<u64, Weak<Node>>> = LazyLock::new(DashMap::new);
113113

114114
pub struct Spatial {
115115
pub node: Weak<Node>,
@@ -386,7 +386,7 @@ impl SpatialAspect for Spatial {
386386
// legit gotta find a way to remove old ones, this just keeps the node alive
387387
async fn export_spatial(node: Arc<Node>, _calling_client: Arc<Client>) -> Result<Id> {
388388
let id = rand::random();
389-
EXPORTED_SPATIALS.lock().insert(id, Arc::downgrade(&node));
389+
EXPORTED_SPATIALS.insert(id, Arc::downgrade(&node));
390390
Ok(id.into())
391391
}
392392
}
@@ -494,7 +494,6 @@ impl InterfaceAspect for Interface {
494494
uid: Id,
495495
) -> Result<Id> {
496496
let node = EXPORTED_SPATIALS
497-
.lock()
498497
.get(&uid.0)
499498
.and_then(|s| s.upgrade())
500499
.map(|s| {

src/objects/input/mouse_pointer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl MousePointer {
401401
.await
402402
.ok()?
403403
.ok()?;
404-
let field_node = EXPORTED_FIELDS.lock().get(&uid)?.upgrade()?;
404+
let field_node = EXPORTED_FIELDS.get(&uid)?.upgrade()?;
405405
let field = field_node.get_aspect::<Field>();
406406
Some((field, keyboard_proxy))
407407
}
@@ -410,7 +410,7 @@ impl MousePointer {
410410
.await
411411
.ok()?
412412
.ok()?;
413-
let field_node = EXPORTED_FIELDS.lock().get(&uid)?.upgrade()?;
413+
let field_node = EXPORTED_FIELDS.get(&uid)?.upgrade()?;
414414
let field = field_node.get_aspect::<Field>();
415415
Some((field, keyboard_proxy))
416416
}

0 commit comments

Comments
 (0)