Skip to content

Commit

Permalink
refactor: remove once_cell dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
technobaboo committed Feb 24, 2025
1 parent 12a99cf commit 5362563
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 150 deletions.
11 changes: 5 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ lto = "thin"

[dependencies]
# small utility thingys
once_cell = "1.19.0"
nanoid = "0.4.0"
lazy_static = "1.5.0"
rand = "0.8.5"
Expand Down
45 changes: 26 additions & 19 deletions src/core/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ use crate::{
use color_eyre::eyre::{Result, eyre};
use global_counter::primitive::exact::CounterU32;
use lazy_static::lazy_static;
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use rustc_hash::FxHashMap;
use stardust_xr::messenger::{self, MessageSenderHandle};
use std::{fmt::Debug, fs, iter::FromIterator, path::PathBuf, sync::Arc};
use std::{
fmt::Debug,
fs,
iter::FromIterator,
path::PathBuf,
sync::{Arc, OnceLock},
};
use tokio::{net::UnixStream, task::JoinHandle};
use tracing::info;

Expand All @@ -29,16 +34,16 @@ lazy_static! {
// env: None,
exe: None,

dispatch_join_handle: OnceCell::new(),
flush_join_handle: OnceCell::new(),
disconnect_status: OnceCell::new(),
dispatch_join_handle: OnceLock::new(),
flush_join_handle: OnceLock::new(),
disconnect_status: OnceLock::new(),

message_sender_handle: None,
id_counter: CounterU32::new(0),
scenegraph: Default::default(),
root: OnceCell::new(),
root: OnceLock::new(),
base_resource_prefixes: Default::default(),
state: OnceCell::default(),
state: OnceLock::default(),
});
}

Expand All @@ -59,16 +64,16 @@ pub struct Client {
pub pid: Option<i32>,
// env: Option<FxHashMap<String, String>>,
exe: Option<PathBuf>,
dispatch_join_handle: OnceCell<JoinHandle<Result<()>>>,
flush_join_handle: OnceCell<JoinHandle<Result<()>>>,
disconnect_status: OnceCell<Result<()>>,
dispatch_join_handle: OnceLock<JoinHandle<Result<()>>>,
flush_join_handle: OnceLock<JoinHandle<Result<()>>>,
disconnect_status: OnceLock<Result<()>>,

id_counter: CounterU32,
pub message_sender_handle: Option<MessageSenderHandle>,
pub scenegraph: Arc<Scenegraph>,
pub root: OnceCell<Arc<Root>>,
pub root: OnceLock<Arc<Root>>,
pub base_resource_prefixes: Mutex<Vec<PathBuf>>,
pub state: OnceCell<ClientState>,
pub state: OnceLock<ClientState>,
}
impl Client {
pub fn from_connection(connection: UnixStream) -> Result<Arc<Self>> {
Expand All @@ -95,16 +100,16 @@ impl Client {
// env,
exe: exe.clone(),

dispatch_join_handle: OnceCell::new(),
flush_join_handle: OnceCell::new(),
disconnect_status: OnceCell::new(),
dispatch_join_handle: OnceLock::new(),
flush_join_handle: OnceLock::new(),
disconnect_status: OnceLock::new(),

id_counter: CounterU32::new(256),
message_sender_handle: Some(messenger_tx.handle()),
scenegraph: scenegraph.clone(),
root: OnceCell::new(),
root: OnceLock::new(),
base_resource_prefixes: Default::default(),
state: OnceCell::default(),
state: OnceLock::default(),
});
let _ = client.scenegraph.client.set(Arc::downgrade(&client));
let _ = client.root.set(Root::create(&client, state.root)?);
Expand All @@ -128,7 +133,7 @@ impl Client {
.map(|exe| exe.to_string())
})
.unwrap_or_else(|| "??".to_string());
let _ = client.dispatch_join_handle.get_or_try_init(|| {
let _ = client.dispatch_join_handle.get_or_init(|| {
task::new(
|| {
format!(
Expand All @@ -147,8 +152,9 @@ impl Client {
}
},
)
.unwrap()
});
let _ = client.flush_join_handle.get_or_try_init(|| {
let _ = client.flush_join_handle.get_or_init(|| {
task::new(
|| format!("client flush pid={} exe={}", &pid_printable, &exe_printable,),
{
Expand All @@ -162,6 +168,7 @@ impl Client {
}
},
)
.unwrap()
});

Ok(client)
Expand Down
7 changes: 3 additions & 4 deletions src/core/destroy_queue.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use std::any::Any;
use std::{any::Any, sync::LazyLock};
use tokio::sync::mpsc::{self, unbounded_channel};

type Anything = Box<dyn Any + Send + Sync>;

static MAIN_DESTROY_QUEUE: Lazy<(
static MAIN_DESTROY_QUEUE: LazyLock<(
mpsc::UnboundedSender<Anything>,
Mutex<mpsc::UnboundedReceiver<Anything>>,
)> = Lazy::new(|| {
)> = LazyLock::new(|| {
let (tx, rx) = unbounded_channel();
(tx, Mutex::new(rx))
});
Expand Down
5 changes: 2 additions & 3 deletions src/core/scenegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::core::error::Result;
use crate::nodes::Node;
use crate::nodes::alias::get_original;
use crate::{core::client::Client, nodes::Message};
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use rustc_hash::FxHashMap;
use serde::Serialize;
Expand All @@ -11,13 +10,13 @@ use stardust_xr::scenegraph::ScenegraphError;
use stardust_xr::schemas::flex::serialize;
use std::future::Future;
use std::os::fd::OwnedFd;
use std::sync::{Arc, Weak};
use std::sync::{Arc, OnceLock, Weak};
use tokio::sync::oneshot;
use tracing::{debug, debug_span};

#[derive(Default)]
pub struct Scenegraph {
pub(super) client: OnceCell<Weak<Client>>,
pub(super) client: OnceLock<Weak<Client>>,
nodes: Mutex<FxHashMap<u64, Arc<Node>>>,
}

Expand Down
5 changes: 2 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ use core::client::Client;
use core::task;
use directories::ProjectDirs;
use objects::ServerObjects;
use once_cell::sync::OnceCell;
use session::{launch_start, save_session};
use stardust_xr::schemas::dbus::object_registry::ObjectRegistry;
use stardust_xr::server;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::{Arc, OnceLock};
use std::time::Duration;
use stereokit_rust::material::Material;
use stereokit_rust::shader::Shader;
Expand Down Expand Up @@ -70,7 +69,7 @@ struct CliArgs {
restore: Option<String>,
}

static STARDUST_INSTANCE: OnceCell<String> = OnceCell::new();
static STARDUST_INSTANCE: OnceLock<String> = OnceLock::new();

// #[tokio::main]
#[tokio::main(flavor = "current_thread")]
Expand Down
7 changes: 3 additions & 4 deletions src/nodes/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ use crate::core::resource::get_resource_file;
use crate::nodes::spatial::{SPATIAL_ASPECT_ALIAS_INFO, Spatial, Transform};
use color_eyre::eyre::eyre;
use glam::{Vec4Swizzles, vec3};
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use stardust_xr::values::ResourceID;

use std::ops::DerefMut;
use std::sync::Arc;
use std::sync::{Arc, OnceLock};
use std::{ffi::OsStr, path::PathBuf};
use stereokit_rust::sound::{Sound as SkSound, SoundInst};

Expand All @@ -24,7 +23,7 @@ pub struct Sound {

volume: f32,
pending_audio_path: PathBuf,
sk_sound: OnceCell<SkSound>,
sk_sound: OnceLock<SkSound>,
instance: Mutex<Option<SoundInst>>,
stop: Mutex<Option<()>>,
play: Mutex<Option<()>>,
Expand All @@ -41,7 +40,7 @@ impl Sound {
space: node.get_aspect::<Spatial>().unwrap().clone(),
volume: 1.0,
pending_audio_path,
sk_sound: OnceCell::new(),
sk_sound: OnceLock::new(),
instance: Mutex::new(None),
stop: Mutex::new(None),
play: Mutex::new(None),
Expand Down
11 changes: 5 additions & 6 deletions src/nodes/drawable/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ use crate::nodes::alias::{Alias, AliasList};
use crate::nodes::spatial::Spatial;
use color_eyre::eyre::eyre;
use glam::{Mat4, Vec2, Vec3};
use once_cell::sync::{Lazy, OnceCell};
use parking_lot::Mutex;
use rustc_hash::FxHashMap;
use stardust_xr::values::ResourceID;
use std::ffi::OsStr;
use std::hash::{Hash, Hasher};
use std::sync::{Arc, Weak};
use std::sync::{Arc, LazyLock, OnceLock, Weak};
use stereokit_rust::material::Transparency;
use stereokit_rust::maths::Bounds;
use stereokit_rust::sk::MainThreadToken;
Expand Down Expand Up @@ -98,9 +97,9 @@ impl MaterialRegistry {
}
}

static MATERIAL_REGISTRY: Lazy<MaterialRegistry> = Lazy::new(MaterialRegistry::default);
static MATERIAL_REGISTRY: LazyLock<MaterialRegistry> = LazyLock::new(MaterialRegistry::default);
static MODEL_REGISTRY: Registry<Model> = Registry::new();
static HOLDOUT_MATERIAL: OnceCell<Arc<MaterialWrapper>> = OnceCell::new();
static HOLDOUT_MATERIAL: OnceLock<Arc<MaterialWrapper>> = OnceLock::new();

impl MaterialParameter {
fn apply_to_material(&self, client: &Client, material: &Material, parameter_name: &str) {
Expand Down Expand Up @@ -333,7 +332,7 @@ impl ModelPartAspect for ModelPart {
pub struct Model {
space: Arc<Spatial>,
_resource_id: ResourceID,
sk_model: OnceCell<SKModel>,
sk_model: OnceLock<SKModel>,
parts: Mutex<Vec<Arc<ModelPart>>>,
}
impl Model {
Expand All @@ -348,7 +347,7 @@ impl Model {
let model = Arc::new(Model {
space: node.get_aspect::<Spatial>().unwrap().clone(),
_resource_id: resource_id,
sk_model: OnceCell::new(),
sk_model: OnceLock::new(),
parts: Mutex::new(Vec::default()),
});
MODEL_REGISTRY.add_raw(&model);
Expand Down
Loading

0 comments on commit 5362563

Please sign in to comment.