From f2bdd91abfb73944cb302bf7450843581ce27443 Mon Sep 17 00:00:00 2001 From: Gnome! Date: Sat, 2 Dec 2023 17:22:00 +0000 Subject: [PATCH] Deps: Replace OnceCell with std::sync::OnceLock (#207) --- Cargo.toml | 3 --- benches/base-mixing.rs | 2 +- src/config.rs | 18 +++++-------- src/driver/mod.rs | 2 +- src/driver/scheduler/mod.rs | 16 +++++++---- src/driver/test_impls.rs | 10 +++---- src/input/adapters/cached/compressed.rs | 14 ++++------ src/input/codecs/mod.rs | 35 +++++++++++++++---------- src/input/live_input.rs | 2 +- src/input/mod.rs | 6 ++--- src/manager.rs | 9 +++---- 11 files changed, 59 insertions(+), 58 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ee3a5d8ea..19fd8a305 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,6 @@ discortp = { default-features = false, features = ["discord", "pnet", "rtp"], op flume = { optional = true, version = "0.11" } futures = "0.3" nohash-hasher = { optional = true, version = "0.2.0" } -once_cell = { optional = true, version = "1" } parking_lot = { optional = true, version = "0.12" } pin-project = "1" rand = { optional = true, version = "0.8" } @@ -73,7 +72,6 @@ gateway = [ "dep:async-trait", "dep:dashmap", "dep:flume", - "dep:once_cell", "dep:parking_lot", "dep:tokio", "tokio?/sync", @@ -88,7 +86,6 @@ driver = [ "dep:reqwest", "dep:flume", "dep:nohash-hasher", - "dep:once_cell", "dep:parking_lot", "dep:rand", "dep:ringbuf", diff --git a/benches/base-mixing.rs b/benches/base-mixing.rs index 0eaf7cd8c..3e7d1c95e 100644 --- a/benches/base-mixing.rs +++ b/benches/base-mixing.rs @@ -88,7 +88,7 @@ fn make_src(src: &Vec, chans: u32, hz: u32) -> (Parsed, DecodeState) { let adapted: Input = songbird::input::RawAdapter::new(Cursor::new(src.clone()), hz, chans).into(); let promoted = match adapted { - Input::Live(l, _) => l.promote(&CODEC_REGISTRY, &PROBE), + Input::Live(l, _) => l.promote(get_codec_registry(), get_probe()), _ => panic!("Failed to create a guaranteed source."), }; let parsed = match promoted { diff --git a/src/config.rs b/src/config.rs index 49eb3c4b0..27aa27f09 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,12 +3,12 @@ use crate::driver::DecodeMode; #[cfg(feature = "driver")] use crate::{ driver::{ + get_default_scheduler, retry::Retry, tasks::disposal::DisposalThread, CryptoMode, MixMode, Scheduler, - DEFAULT_SCHEDULER, }, input::codecs::*, }; @@ -161,19 +161,15 @@ pub struct Config { /// Registry of the inner codecs supported by the driver, adding audiopus-based /// Opus codec support to all of Symphonia's default codecs. /// - /// Defaults to [`CODEC_REGISTRY`]. - /// - /// [`CODEC_REGISTRY`]: static@CODEC_REGISTRY + /// Defaults to [`get_codec_registry`]. pub codec_registry: &'static CodecRegistry, #[cfg(feature = "driver")] #[derivative(Debug = "ignore")] /// Registry of the muxers and container formats supported by the driver. /// - /// Defaults to [`PROBE`], which includes all of Symphonia's default format handlers + /// Defaults to [`get_probe`], which includes all of Symphonia's default format handlers /// and DCA format support. - /// - /// [`PROBE`]: static@PROBE pub format_registry: &'static Probe, #[cfg(feature = "driver")] @@ -191,7 +187,7 @@ pub struct Config { /// The scheduler is responsible for mapping idle and active [`Driver`] instances /// to threads. /// - /// If set to None, then songbird will initialise the [`DEFAULT_SCHEDULER`]. + /// If set to None, then songbird will use [`get_default_scheduler`]. /// /// [`Driver`]: crate::Driver pub scheduler: Option, @@ -233,9 +229,9 @@ impl Default for Config { #[cfg(feature = "driver")] driver_timeout: Some(Duration::from_secs(10)), #[cfg(feature = "driver")] - codec_registry: &CODEC_REGISTRY, + codec_registry: get_codec_registry(), #[cfg(feature = "driver")] - format_registry: &PROBE, + format_registry: get_probe(), #[cfg(feature = "driver")] disposer: None, #[cfg(feature = "driver")] @@ -359,7 +355,7 @@ impl Config { pub fn get_scheduler(&self) -> Scheduler { self.scheduler .as_ref() - .unwrap_or(&*DEFAULT_SCHEDULER) + .unwrap_or(get_default_scheduler()) .clone() } diff --git a/src/driver/mod.rs b/src/driver/mod.rs index 3e5b58b36..447257111 100644 --- a/src/driver/mod.rs +++ b/src/driver/mod.rs @@ -31,12 +31,12 @@ pub(crate) use crypto::CryptoState; pub use decode_mode::DecodeMode; pub use mix_mode::MixMode; pub use scheduler::{ + get_default_scheduler, Config as SchedulerConfig, Error as SchedulerError, LiveStatBlock, Mode as SchedulerMode, Scheduler, - DEFAULT_SCHEDULER, }; #[cfg(test)] pub use test_config::*; diff --git a/src/driver/scheduler/mod.rs b/src/driver/scheduler/mod.rs index cbaac6e13..792922e83 100644 --- a/src/driver/scheduler/mod.rs +++ b/src/driver/scheduler/mod.rs @@ -1,11 +1,14 @@ -use std::{error::Error as StdError, fmt::Display, num::NonZeroUsize, sync::Arc}; +use std::{ + error::Error as StdError, + fmt::Display, + num::NonZeroUsize, + sync::{Arc, OnceLock}, +}; use flume::{Receiver, RecvError, Sender}; -use once_cell::sync::Lazy; - -use crate::{constants::TIMESTEP_LENGTH, Config as DriverConfig}; use super::tasks::message::{Interconnect, MixerMessage}; +use crate::{constants::TIMESTEP_LENGTH, Config as DriverConfig}; mod config; mod idle; @@ -34,7 +37,10 @@ const DEFAULT_MIXERS_PER_THREAD: NonZeroUsize = match NonZeroUsize::new(16) { /// /// [`Config::default`]: crate::Config::default /// [`ScheduleMode`]: Mode -pub static DEFAULT_SCHEDULER: Lazy = Lazy::new(Scheduler::default); +pub fn get_default_scheduler() -> &'static Scheduler { + static DEFAULT_SCHEDULER: OnceLock = OnceLock::new(); + DEFAULT_SCHEDULER.get_or_init(Scheduler::default) +} /// A reference to a shared group of threads used for running idle and active /// audio threads. diff --git a/src/driver/test_impls.rs b/src/driver/test_impls.rs index 5366980a6..89d025ab4 100644 --- a/src/driver/test_impls.rs +++ b/src/driver/test_impls.rs @@ -5,7 +5,7 @@ use crate::{ driver::crypto::KEY_SIZE, input::{ cached::Compressed, - codecs::{CODEC_REGISTRY, PROBE}, + codecs::{get_codec_registry, get_probe}, RawAdapter, }, test_utils, @@ -96,7 +96,7 @@ impl Mixer { for _ in 0..num_tracks { let input: Input = RawAdapter::new(Cursor::new(floats.clone()), 48_000, 2).into(); let promoted = match input { - Input::Live(l, _) => l.promote(&CODEC_REGISTRY, &PROBE), + Input::Live(l, _) => l.promote(get_codec_registry(), get_probe()), Input::Lazy(_) => panic!("Failed to create a guaranteed source."), }; let (_, ctx) = Track::from(Input::Live(promoted.unwrap(), None)).into_context(); @@ -113,7 +113,7 @@ impl Mixer { let input: Input = RawAdapter::new(Cursor::new(floats.clone()), 48_000, 2).into(); let promoted = match input { - Input::Live(l, _) => l.promote(&CODEC_REGISTRY, &PROBE), + Input::Live(l, _) => l.promote(get_codec_registry(), get_probe()), Input::Lazy(_) => panic!("Failed to create a guaranteed source."), }; let mut track = Track::from(Input::Live(promoted.unwrap(), None)); @@ -132,7 +132,7 @@ impl Mixer { let floats = test_utils::make_sine((i / 5) * STEREO_FRAME_SIZE, true); let input: Input = RawAdapter::new(Cursor::new(floats.clone()), 48_000, 2).into(); let promoted = match input { - Input::Live(l, _) => l.promote(&CODEC_REGISTRY, &PROBE), + Input::Live(l, _) => l.promote(get_codec_registry(), get_probe()), Input::Lazy(_) => panic!("Failed to create a guaranteed source."), }; let (_, ctx) = Track::from(Input::Live(promoted.unwrap(), None)).into_context(); @@ -160,7 +160,7 @@ impl Mixer { src.raw.load_all(); let promoted = match src.into() { - Input::Live(l, _) => l.promote(&CODEC_REGISTRY, &PROBE), + Input::Live(l, _) => l.promote(get_codec_registry(), get_probe()), Input::Lazy(_) => panic!("Failed to create a guaranteed source."), }; let (_, ctx) = Track::from(Input::Live(promoted.unwrap(), None)).into_context(); diff --git a/src/input/adapters/cached/compressed.rs b/src/input/adapters/cached/compressed.rs index 398cd614b..47ae98792 100644 --- a/src/input/adapters/cached/compressed.rs +++ b/src/input/adapters/cached/compressed.rs @@ -2,7 +2,7 @@ use super::{compressed_cost_per_sec, default_config, CodecCacheError, ToAudioByt use crate::{ constants::*, input::{ - codecs::{dca::*, CODEC_REGISTRY, PROBE}, + codecs::{dca::*, get_codec_registry, get_probe}, AudioStream, Input, LiveInput, @@ -53,17 +53,13 @@ use tracing::{debug, trace}; pub struct Config { /// Registry of audio codecs supported by the driver. /// - /// Defaults to [`CODEC_REGISTRY`], which adds audiopus-based Opus codec support + /// Defaults to [`get_codec_registry`], which adds audiopus-based Opus codec support /// to all of Symphonia's default codecs. - /// - /// [`CODEC_REGISTRY`]: static@CODEC_REGISTRY pub codec_registry: &'static CodecRegistry, /// Registry of the muxers and container formats supported by the driver. /// - /// Defaults to [`PROBE`], which includes all of Symphonia's default format handlers + /// Defaults to [`get_probe`], which includes all of Symphonia's default format handlers /// and DCA format support. - /// - /// [`PROBE`]: static@PROBE pub format_registry: &'static Probe, /// Configuration for the inner streamcatcher instance. /// @@ -74,8 +70,8 @@ pub struct Config { impl Default for Config { fn default() -> Self { Self { - codec_registry: &CODEC_REGISTRY, - format_registry: &PROBE, + codec_registry: get_codec_registry(), + format_registry: get_probe(), streamcatcher: ScConfig::default(), } } diff --git a/src/input/codecs/mod.rs b/src/input/codecs/mod.rs index a71d079b2..eda86075d 100644 --- a/src/input/codecs/mod.rs +++ b/src/input/codecs/mod.rs @@ -4,26 +4,33 @@ pub(crate) mod dca; mod opus; mod raw; +use std::sync::OnceLock; + pub use self::{dca::DcaReader, opus::OpusDecoder, raw::*}; -use once_cell::sync::Lazy; use symphonia::{ core::{codecs::CodecRegistry, probe::Probe}, default::*, }; /// Default Symphonia [`CodecRegistry`], including the (audiopus-backed) Opus codec. -pub static CODEC_REGISTRY: Lazy = Lazy::new(|| { - let mut registry = CodecRegistry::new(); - register_enabled_codecs(&mut registry); - registry.register_all::(); - registry -}); +pub fn get_codec_registry() -> &'static CodecRegistry { + static CODEC_REGISTRY: OnceLock = OnceLock::new(); + CODEC_REGISTRY.get_or_init(|| { + let mut registry = CodecRegistry::new(); + register_enabled_codecs(&mut registry); + registry.register_all::(); + registry + }) +} /// Default Symphonia Probe, including DCA format support. -pub static PROBE: Lazy = Lazy::new(|| { - let mut probe = Probe::default(); - probe.register_all::(); - probe.register_all::(); - register_enabled_formats(&mut probe); - probe -}); +pub fn get_probe() -> &'static Probe { + static PROBE: OnceLock = OnceLock::new(); + PROBE.get_or_init(|| { + let mut probe = Probe::default(); + probe.register_all::(); + probe.register_all::(); + register_enabled_formats(&mut probe); + probe + }) +} diff --git a/src/input/live_input.rs b/src/input/live_input.rs index 7da560d2e..42b3d1ab1 100644 --- a/src/input/live_input.rs +++ b/src/input/live_input.rs @@ -160,7 +160,7 @@ mod tests { // finds the audio on a non-default track via `LiveInput::promote`. let input = Input::from(File::new(FILE_VID_TARGET)); input - .make_playable_async(&CODEC_REGISTRY, &PROBE) + .make_playable_async(get_codec_registry(), get_probe()) .await .unwrap(); } diff --git a/src/input/mod.rs b/src/input/mod.rs index a8e981a60..e69d3e2aa 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -49,8 +49,8 @@ //! [`OpusDecoder`]: codecs::OpusDecoder //! [`DcaReader`]: codecs::DcaReader //! [`RawReader`]: codecs::RawReader -//! [format]: static@codecs::PROBE -//! [codec registries]: static@codecs::CODEC_REGISTRY +//! [format]: codecs::get_probe +//! [codec registries]: codecs::get_codec_registry mod adapters; mod audiostream; @@ -147,7 +147,7 @@ use tokio::runtime::Handle as TokioHandle; /// // /// // We can access it on a live track using `TrackHandle::action()`. /// in_memory_input = in_memory_input -/// .make_playable_async(&CODEC_REGISTRY, &PROBE) +/// .make_playable_async(get_codec_registry(), get_probe()) /// .await /// .expect("WAV support is included, and this file is good!"); /// diff --git a/src/manager.rs b/src/manager.rs index 5f7e066a3..7521937a1 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -13,7 +13,6 @@ use async_trait::async_trait; use dashmap::DashMap; #[cfg(feature = "serenity")] use futures::channel::mpsc::UnboundedSender as Sender; -use once_cell::sync::OnceCell; use parking_lot::RwLock as PRwLock; #[cfg(feature = "serenity")] use serenity::{ @@ -23,7 +22,7 @@ use serenity::{ voice::VoiceState, }, }; -use std::sync::Arc; +use std::sync::{Arc, OnceLock}; use tokio::sync::Mutex; #[cfg(feature = "serenity")] use tracing::debug; @@ -44,7 +43,7 @@ struct ClientData { /// [`Call`]: Call #[derive(Debug)] pub struct Songbird { - client_data: OnceCell, + client_data: OnceLock, calls: DashMap>>, sharder: Sharder, config: PRwLock, @@ -71,7 +70,7 @@ impl Songbird { #[must_use] pub fn serenity_from_config(config: Config) -> Arc { Arc::new(Self { - client_data: OnceCell::new(), + client_data: OnceLock::new(), calls: DashMap::new(), sharder: Sharder::Serenity(SerenitySharder::default()), config: config.initialise_disposer().into(), @@ -110,7 +109,7 @@ impl Songbird { U: Into, { Self { - client_data: OnceCell::with_value(ClientData { + client_data: OnceLock::from(ClientData { shard_count: sender_map.shard_count(), user_id: user_id.into(), }),