refactor: remove lazy_static crate
The `lazy_static` crate was superseded by the `once_cell` crate which has been included in Rust's standard library since version `1.70`. Remove the `lazy_static` dependency and refactor all use cases to use `std::sync::OnceLock` instead. Co-authored-by: Henrik Friedrichsen <henrik@affekt.org>
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -1968,7 +1968,6 @@ dependencies = [
|
|||||||
"fern",
|
"fern",
|
||||||
"futures",
|
"futures",
|
||||||
"ioctl-rs",
|
"ioctl-rs",
|
||||||
"lazy_static",
|
|
||||||
"libc",
|
"libc",
|
||||||
"librespot-core",
|
"librespot-core",
|
||||||
"librespot-playback",
|
"librespot-playback",
|
||||||
|
|||||||
@@ -37,7 +37,6 @@ zbus = {version = "3.11.1", default-features = false, features = ["tokio"], opti
|
|||||||
fern = "0.6"
|
fern = "0.6"
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
ioctl-rs = {version = "0.2", optional = true}
|
ioctl-rs = {version = "0.2", optional = true}
|
||||||
lazy_static = "1.3.0"
|
|
||||||
libc = "0.2.142"
|
libc = "0.2.142"
|
||||||
librespot-core = "0.4.2"
|
librespot-core = "0.4.2"
|
||||||
librespot-playback = "0.4.2"
|
librespot-playback = "0.4.2"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::Arc;
|
use std::sync::{Arc, OnceLock};
|
||||||
|
|
||||||
use cursive::traits::Nameable;
|
use cursive::traits::Nameable;
|
||||||
use cursive::{Cursive, CursiveRunner};
|
use cursive::{Cursive, CursiveRunner};
|
||||||
@@ -55,13 +55,8 @@ pub struct UserDataInner {
|
|||||||
pub cmd: CommandManager,
|
pub cmd: CommandManager,
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static!(
|
/// The global Tokio runtime for running asynchronous tasks.
|
||||||
/// The global Tokio runtime for running asynchronous tasks.
|
pub static ASYNC_RUNTIME: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
|
||||||
pub static ref ASYNC_RUNTIME: tokio::runtime::Runtime = tokio::runtime::Builder::new_multi_thread()
|
|
||||||
.enable_all()
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
);
|
|
||||||
|
|
||||||
/// The representation of an ncspot application.
|
/// The representation of an ncspot application.
|
||||||
pub struct Application {
|
pub struct Application {
|
||||||
@@ -91,6 +86,15 @@ impl Application {
|
|||||||
// Things here may cause the process to abort; we must do them before creating curses
|
// Things here may cause the process to abort; we must do them before creating curses
|
||||||
// windows otherwise the error message will not be seen by a user
|
// windows otherwise the error message will not be seen by a user
|
||||||
|
|
||||||
|
ASYNC_RUNTIME
|
||||||
|
.set(
|
||||||
|
tokio::runtime::Builder::new_multi_thread()
|
||||||
|
.enable_all()
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let configuration = Arc::new(Config::new(configuration_file_path));
|
let configuration = Arc::new(Config::new(configuration_file_path));
|
||||||
let credentials = authentication::get_credentials(&configuration)?;
|
let credentials = authentication::get_credentials(&configuration)?;
|
||||||
let theme = configuration.build_theme();
|
let theme = configuration.build_theme();
|
||||||
@@ -134,7 +138,7 @@ impl Application {
|
|||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
let ipc = ipc::IpcSocket::new(
|
let ipc = ipc::IpcSocket::new(
|
||||||
ASYNC_RUNTIME.handle(),
|
ASYNC_RUNTIME.get().unwrap().handle(),
|
||||||
crate::config::cache_path("ncspot.sock"),
|
crate::config::cache_path("ncspot.sock"),
|
||||||
event_manager.clone(),
|
event_manager.clone(),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use crate::queue::RepeatSetting;
|
|||||||
use crate::spotify_url::SpotifyUrl;
|
use crate::spotify_url::SpotifyUrl;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
use strum_macros::Display;
|
use strum_macros::Display;
|
||||||
|
|
||||||
@@ -291,8 +292,12 @@ fn register_aliases(map: &mut HashMap<&str, &str>, cmd: &'static str, names: Vec
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
fn handle_aliases(input: &str) -> &str {
|
||||||
static ref ALIASES: HashMap<&'static str, &'static str> = {
|
// NOTE: There is probably a better way to write this than a static HashMap. The HashMap doesn't
|
||||||
|
// improve performance as there's far too few keys, and the use of static doesn't seem good.
|
||||||
|
static ALIASES: OnceLock<HashMap<&'static str, &'static str>> = OnceLock::new();
|
||||||
|
|
||||||
|
let aliases = ALIASES.get_or_init(|| {
|
||||||
let mut m = HashMap::new();
|
let mut m = HashMap::new();
|
||||||
|
|
||||||
register_aliases(&mut m, "quit", vec!["q", "x"]);
|
register_aliases(&mut m, "quit", vec!["q", "x"]);
|
||||||
@@ -302,13 +307,10 @@ lazy_static! {
|
|||||||
vec!["pause", "toggleplay", "toggleplayback"],
|
vec!["pause", "toggleplay", "toggleplayback"],
|
||||||
);
|
);
|
||||||
register_aliases(&mut m, "repeat", vec!["loop"]);
|
register_aliases(&mut m, "repeat", vec!["loop"]);
|
||||||
|
|
||||||
m
|
m
|
||||||
};
|
});
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_aliases(input: &str) -> &str {
|
if let Some(cmd) = aliases.get(input) {
|
||||||
if let Some(cmd) = ALIASES.get(input) {
|
|
||||||
handle_aliases(cmd)
|
handle_aliases(cmd)
|
||||||
} else {
|
} else {
|
||||||
input
|
input
|
||||||
|
|||||||
@@ -175,10 +175,8 @@ impl Default for UserState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
/// Configuration files are read/written relative to this directory.
|
||||||
/// Configuration files are read/written relative to this directory.
|
static BASE_PATH: RwLock<Option<PathBuf>> = RwLock::new(None);
|
||||||
pub static ref BASE_PATH: RwLock<Option<PathBuf>> = RwLock::new(None);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The complete configuration (state + user configuration) of ncspot.
|
/// The complete configuration (state + user configuration) of ncspot.
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
|
|||||||
@@ -3,8 +3,6 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate cursive;
|
extern crate cursive;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate lazy_static;
|
|
||||||
#[macro_use]
|
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|||||||
@@ -484,7 +484,7 @@ impl MprisManager {
|
|||||||
|
|
||||||
let (tx, rx) = mpsc::unbounded_channel::<()>();
|
let (tx, rx) = mpsc::unbounded_channel::<()>();
|
||||||
|
|
||||||
ASYNC_RUNTIME.spawn(async {
|
ASYNC_RUNTIME.get().unwrap().spawn(async {
|
||||||
let result = Self::serve(UnboundedReceiverStream::new(rx), root, player).await;
|
let result = Self::serve(UnboundedReceiverStream::new(rx), root, player).await;
|
||||||
if let Err(e) = result {
|
if let Err(e) = result {
|
||||||
log::error!("MPRIS error: {e}");
|
log::error!("MPRIS error: {e}");
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ impl Spotify {
|
|||||||
|
|
||||||
let (user_tx, user_rx) = oneshot::channel();
|
let (user_tx, user_rx) = oneshot::channel();
|
||||||
spotify.start_worker(Some(user_tx));
|
spotify.start_worker(Some(user_tx));
|
||||||
spotify.user = ASYNC_RUNTIME.block_on(user_rx).ok();
|
spotify.user = ASYNC_RUNTIME.get().unwrap().block_on(user_rx).ok();
|
||||||
let volume = cfg.state().volume;
|
let volume = cfg.state().volume;
|
||||||
spotify.set_volume(volume);
|
spotify.set_volume(volume);
|
||||||
|
|
||||||
@@ -95,7 +95,7 @@ impl Spotify {
|
|||||||
let events = self.events.clone();
|
let events = self.events.clone();
|
||||||
let volume = self.volume();
|
let volume = self.volume();
|
||||||
let credentials = self.credentials.clone();
|
let credentials = self.credentials.clone();
|
||||||
ASYNC_RUNTIME.spawn(Self::worker(
|
ASYNC_RUNTIME.get().unwrap().spawn(Self::worker(
|
||||||
worker_channel,
|
worker_channel,
|
||||||
events,
|
events,
|
||||||
rx,
|
rx,
|
||||||
@@ -122,6 +122,8 @@ impl Spotify {
|
|||||||
pub fn test_credentials(credentials: Credentials) -> Result<Session, SessionError> {
|
pub fn test_credentials(credentials: Credentials) -> Result<Session, SessionError> {
|
||||||
let config = Self::session_config();
|
let config = Self::session_config();
|
||||||
ASYNC_RUNTIME
|
ASYNC_RUNTIME
|
||||||
|
.get()
|
||||||
|
.unwrap()
|
||||||
.block_on(Session::connect(config, credentials, None, true))
|
.block_on(Session::connect(config, credentials, None, true))
|
||||||
.map(|r| r.0)
|
.map(|r| r.0)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ impl WebApi {
|
|||||||
.as_ref()
|
.as_ref()
|
||||||
{
|
{
|
||||||
channel.send(cmd).expect("can't send message to worker");
|
channel.send(cmd).expect("can't send message to worker");
|
||||||
let token_option = ASYNC_RUNTIME.block_on(token_rx).unwrap();
|
let token_option = ASYNC_RUNTIME.get().unwrap().block_on(token_rx).unwrap();
|
||||||
if let Some(token) = token_option {
|
if let Some(token) = token_option {
|
||||||
*self.api.token.lock().expect("can't writelock api token") = Some(Token {
|
*self.api.token.lock().expect("can't writelock api token") = Some(Token {
|
||||||
access_token: token.access_token,
|
access_token: token.access_token,
|
||||||
|
|||||||
Reference in New Issue
Block a user