summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Andrew Walbran <qwandor@google.com> 2024-09-03 14:44:30 +0100
committer Abhishek Pandit-Subedi <abhishekpandit@google.com> 2024-09-05 10:59:00 -0700
commitc747e371e19302ef8ce40dcfca5464c0c91d768d (patch)
tree8cec649e9293e75df0297799be03eff68d7754ab
parent7862ca675e465ef5ec36335d26bc3734579521c1 (diff)
Use std::sync::LazyLock rather than lazy_static.
Bug: 364211748 Test: atest libbt_common_inline_tests Change-Id: I034e6143df298e80565c720bdbf440d4d9939c3f
-rw-r--r--floss/hcidoc/Cargo.toml1
-rw-r--r--floss/hcidoc/src/groups/controllers.rs23
-rw-r--r--system/gd/rust/common/Android.bp3
-rw-r--r--system/gd/rust/common/Cargo.toml1
-rw-r--r--system/gd/rust/common/src/init_flags.rs35
-rw-r--r--system/gd/rust/linux/service/Cargo.toml1
-rw-r--r--system/gd/rust/linux/service/src/main.rs7
-rw-r--r--system/gd/rust/linux/stack/Cargo.toml1
-rw-r--r--system/gd/rust/linux/stack/src/uuid.rs36
-rw-r--r--system/gd/rust/topshim/Android.bp1
-rw-r--r--system/gd/rust/topshim/Cargo.toml1
-rw-r--r--system/gd/rust/topshim/facade/Android.bp1
-rw-r--r--system/gd/rust/topshim/facade/src/main.rs5
-rw-r--r--system/gd/rust/topshim/src/topstack.rs23
14 files changed, 60 insertions, 79 deletions
diff --git a/floss/hcidoc/Cargo.toml b/floss/hcidoc/Cargo.toml
index 920e4af12d..761d6647f3 100644
--- a/floss/hcidoc/Cargo.toml
+++ b/floss/hcidoc/Cargo.toml
@@ -9,4 +9,3 @@ clap = "4.0"
chrono = "0.4"
num-derive = "0.3"
num-traits = "0.2"
-lazy_static = "1.0"
diff --git a/floss/hcidoc/src/groups/controllers.rs b/floss/hcidoc/src/groups/controllers.rs
index 3790a1fb8b..5ba037a01e 100644
--- a/floss/hcidoc/src/groups/controllers.rs
+++ b/floss/hcidoc/src/groups/controllers.rs
@@ -1,9 +1,9 @@
///! Rule group for tracking controller related issues.
use chrono::NaiveDateTime;
-use lazy_static::lazy_static;
use std::collections::HashSet;
use std::convert::Into;
use std::io::Write;
+use std::sync::LazyLock;
use crate::engine::{Rule, RuleGroup, Signal};
use crate::parser::{NewIndex, Packet, PacketChild};
@@ -23,16 +23,17 @@ impl Into<&'static str> for ControllerSignal {
}
}
-lazy_static! {
- static ref KNOWN_CONTROLLER_NAMES: [String; 6] = [
- String::from("Bluemoon Universal Bluetooth Host Controller"), // AC7625
- String::from("MTK MT7961 #1"), // MT7921LE/MT7921LS
- String::from("MTK MT7922 #1"), // MT7922
- String::from("RTK_BT_5.0"), // RTL8822CE
- String::from("RT_BT"), // RTL8852AE
- String::from(""), // AC9260/AC9560/AX200/AX201/AX203/AX211/MVL8897/QCA6174A3/QCA6174A5/QC_WCN6856
- ];
-}
+static KNOWN_CONTROLLER_NAMES: LazyLock<[String; 6]> = LazyLock::new(|| {
+ [
+ String::from("Bluemoon Universal Bluetooth Host Controller"), // AC7625
+ String::from("MTK MT7961 #1"), // MT7921LE/MT7921LS
+ String::from("MTK MT7922 #1"), // MT7922
+ String::from("RTK_BT_5.0"), // RTL8822CE
+ String::from("RT_BT"), // RTL8852AE
+ String::from(""), // AC9260/AC9560/AX200/AX201/AX203/AX211/MVL8897/QCA6174A3/QCA6174A5/QC_WCN6856
+ ]
+});
+
const KNOWN_CONTROLLER_MANUFACTURERS: [u16; 5] = [
2, // Intel.
29, // Qualcomm
diff --git a/system/gd/rust/common/Android.bp b/system/gd/rust/common/Android.bp
index 344997581c..188225ecf2 100644
--- a/system/gd/rust/common/Android.bp
+++ b/system/gd/rust/common/Android.bp
@@ -11,7 +11,6 @@ rust_library {
name: "libbt_common",
defaults: ["libbt_common_defaults"],
rustlibs: [
- "liblazy_static",
"liblog_rust",
],
target: {
@@ -38,7 +37,6 @@ rust_defaults {
crate_name: "bt_common",
srcs: ["src/lib.rs"],
rustlibs: [
- "liblazy_static",
"liblog_rust",
],
proc_macros: [
@@ -55,7 +53,6 @@ rust_test_host {
rustlibs: [
"libbt_common",
"libenv_logger",
- "liblazy_static",
"liblog_rust",
],
proc_macros: [
diff --git a/system/gd/rust/common/Cargo.toml b/system/gd/rust/common/Cargo.toml
index 90bf346e67..70fc874baa 100644
--- a/system/gd/rust/common/Cargo.toml
+++ b/system/gd/rust/common/Cargo.toml
@@ -22,7 +22,6 @@ edition = "2018"
cxx = "1.0"
env_logger = "0.8"
futures = "0.3.13"
-lazy_static = "1.4"
log = "0.4"
nix = { version = "0.27.1", features = ["time", "user"] }
tokio = { version = "1.0", features = ['bytes', 'macros', 'net', 'rt-multi-thread', 'time'] }
diff --git a/system/gd/rust/common/src/init_flags.rs b/system/gd/rust/common/src/init_flags.rs
index 6cbcf4c109..87e16fa6a5 100644
--- a/system/gd/rust/common/src/init_flags.rs
+++ b/system/gd/rust/common/src/init_flags.rs
@@ -1,8 +1,8 @@
-use lazy_static::lazy_static;
use log::{error, info};
use paste::paste;
use std::collections::{BTreeMap, HashMap};
use std::fmt;
+use std::sync::LazyLock;
use std::sync::Mutex;
// Fallback to bool when type is not specified
@@ -78,14 +78,20 @@ macro_rules! init_flags_struct {
$($flag : type_expand!($($type)?),)*
}
- impl Default for $name {
- fn default() -> Self {
+ impl $name {
+ pub const fn new() -> Self {
Self {
$($flag : default_value!($($type)? $(= $default)?),)*
}
}
}
+ impl Default for $name {
+ fn default() -> Self {
+ Self::new()
+ }
+ }
+
impl FlagHolder for $name {
fn get_defaults_for_test() -> Self {
Self {
@@ -181,14 +187,12 @@ init_flags!(
}
);
-lazy_static! {
- /// Store some flag values
- static ref FLAGS: Mutex<InitFlags> = Mutex::new(InitFlags::default());
- /// Store the uid of bluetooth
- pub static ref AID_BLUETOOTH: Mutex<u32> = Mutex::new(1002);
- /// Store the prefix for file system
- pub static ref MISC: Mutex<String> = Mutex::new("/data/misc/".to_string());
-}
+/// Store some flag values
+static FLAGS: Mutex<InitFlags> = Mutex::new(InitFlags::new());
+/// Store the uid of bluetooth
+pub static AID_BLUETOOTH: Mutex<u32> = Mutex::new(1002);
+/// Store the prefix for file system
+pub static MISC: LazyLock<Mutex<String>> = LazyLock::new(|| Mutex::new("/data/misc/".to_string()));
/// Loads the flag values from the passed-in vector of string values
pub fn load(raw_flags: Vec<String>) {
@@ -207,11 +211,10 @@ pub fn dump() -> BTreeMap<&'static str, String> {
#[cfg(test)]
mod tests {
use super::*;
- lazy_static! {
- /// do not run concurrent tests as they all use the same global init_flag struct and
- /// accessor
- pub(super) static ref ASYNC_LOCK: Mutex<bool> = Mutex::new(false);
- }
+
+ /// do not run concurrent tests as they all use the same global init_flag struct and
+ /// accessor
+ pub(super) static ASYNC_LOCK: Mutex<bool> = Mutex::new(false);
pub(super) fn test_load(raw_flags: Vec<&str>) {
let raw_flags = raw_flags.into_iter().map(|x| x.to_string()).collect();
diff --git a/system/gd/rust/linux/service/Cargo.toml b/system/gd/rust/linux/service/Cargo.toml
index 78a2051ab2..69049107b9 100644
--- a/system/gd/rust/linux/service/Cargo.toml
+++ b/system/gd/rust/linux/service/Cargo.toml
@@ -16,7 +16,6 @@ dbus = "0.9.2"
dbus-crossroads = "0.4.0"
dbus-tokio = "0.7.6"
futures = "0.3.13"
-lazy_static = "1.4"
log = "0.4.14"
nix = "0.23"
num-traits = "0.2"
diff --git a/system/gd/rust/linux/service/src/main.rs b/system/gd/rust/linux/service/src/main.rs
index ad4f8be9ca..3c15b6536d 100644
--- a/system/gd/rust/linux/service/src/main.rs
+++ b/system/gd/rust/linux/service/src/main.rs
@@ -2,7 +2,6 @@ use clap::{App, AppSettings, Arg};
use dbus_projection::DisconnectWatcher;
use dbus_tokio::connection;
use futures::future;
-use lazy_static::lazy_static;
use nix::sys::signal;
use std::error::Error;
use std::sync::{Arc, Condvar, Mutex};
@@ -276,10 +275,8 @@ fn main() -> Result<(), Box<dyn Error>> {
})
}
-lazy_static! {
- /// Data needed for signal handling.
- static ref SIG_DATA: Mutex<Option<(Sender<Message>, Arc<SigData>)>> = Mutex::new(None);
-}
+/// Data needed for signal handling.
+static SIG_DATA: Mutex<Option<(Sender<Message>, Arc<SigData>)>> = Mutex::new(None);
extern "C" fn handle_sigterm(_signum: i32) {
let guard = SIG_DATA.lock().unwrap();
diff --git a/system/gd/rust/linux/stack/Cargo.toml b/system/gd/rust/linux/stack/Cargo.toml
index 9ad6b63ace..2562617b3d 100644
--- a/system/gd/rust/linux/stack/Cargo.toml
+++ b/system/gd/rust/linux/stack/Cargo.toml
@@ -14,7 +14,6 @@ btif_macros = { path = "btif_macros" }
dbus = "0.9.2"
env_logger = "0.8.3"
itertools = "0.10.5"
-lazy_static = "1.4"
log = "0.4.14"
log-panics = "2.1.0"
nix = "0.23"
diff --git a/system/gd/rust/linux/stack/src/uuid.rs b/system/gd/rust/linux/stack/src/uuid.rs
index ba9ccc40d6..b7401a7e76 100644
--- a/system/gd/rust/linux/stack/src/uuid.rs
+++ b/system/gd/rust/linux/stack/src/uuid.rs
@@ -1,9 +1,9 @@
//! Collection of Profile UUIDs and helpers to use them.
-use lazy_static::lazy_static;
use num_derive::{FromPrimitive, ToPrimitive};
use std::collections::{HashMap, HashSet};
use std::fmt::{Debug, Display, Formatter};
+use std::sync::LazyLock;
use bt_topshim::btif::Uuid;
@@ -85,11 +85,11 @@ impl Display for Profile {
pub struct UuidHelper {}
-lazy_static! {
- // AVRCP fights with A2DP when initializing, so let's initiate profiles in a known good order.
- // Specifically, A2DP must be initialized before AVRCP.
- // TODO (b/286991526): remove after issue is resolved
- static ref ORDERED_SUPPORTED_PROFILES: Vec<Profile> = vec![
+// AVRCP fights with A2DP when initializing, so let's initiate profiles in a known good order.
+// Specifically, A2DP must be initialized before AVRCP.
+// TODO (b/286991526): remove after issue is resolved
+static ORDERED_SUPPORTED_PROFILES: LazyLock<Vec<Profile>> = LazyLock::new(|| {
+ vec![
Profile::A2dpSink,
Profile::A2dpSource,
Profile::AvrcpController,
@@ -106,16 +106,14 @@ lazy_static! {
Profile::HearingAid,
Profile::VolumeControl,
Profile::CoordinatedSet,
- ];
-}
+ ]
+});
-lazy_static! {
- static ref SUPPORTED_PROFILES: HashSet<Profile> =
- ORDERED_SUPPORTED_PROFILES.iter().cloned().collect();
-}
+static SUPPORTED_PROFILES: LazyLock<HashSet<Profile>> =
+ LazyLock::new(|| ORDERED_SUPPORTED_PROFILES.iter().cloned().collect());
-lazy_static! {
- static ref PROFILES: HashMap<Uuid, Profile> = [
+static PROFILES: LazyLock<HashMap<Uuid, Profile>> = LazyLock::new(|| {
+ [
(Uuid::from_string(A2DP_SINK).unwrap(), Profile::A2dpSink),
(Uuid::from_string(A2DP_SOURCE).unwrap(), Profile::A2dpSource),
(Uuid::from_string(ADV_AUDIO_DIST).unwrap(), Profile::AdvAudioDist),
@@ -149,13 +147,11 @@ lazy_static! {
]
.iter()
.cloned()
- .collect();
-}
+ .collect()
+});
-lazy_static! {
- static ref PROFILES_UUIDS: HashMap<Profile, Uuid> =
- PROFILES.iter().map(|(k, v)| (*v, *k)).collect();
-}
+static PROFILES_UUIDS: LazyLock<HashMap<Profile, Uuid>> =
+ LazyLock::new(|| PROFILES.iter().map(|(k, v)| (*v, *k)).collect());
impl UuidHelper {
/// Checks whether a UUID corresponds to a currently enabled profile.
diff --git a/system/gd/rust/topshim/Android.bp b/system/gd/rust/topshim/Android.bp
index 84a41e9e99..391017ac96 100644
--- a/system/gd/rust/topshim/Android.bp
+++ b/system/gd/rust/topshim/Android.bp
@@ -22,7 +22,6 @@ rust_library_host_rlib {
"libcxx",
"libfutures",
"libgrpcio",
- "liblazy_static",
"liblog_rust",
"libnix",
"libnum_traits",
diff --git a/system/gd/rust/topshim/Cargo.toml b/system/gd/rust/topshim/Cargo.toml
index bc54e63a70..22829587b8 100644
--- a/system/gd/rust/topshim/Cargo.toml
+++ b/system/gd/rust/topshim/Cargo.toml
@@ -26,7 +26,6 @@ topshim_macros = { path = "macros" }
cxx = "1.0"
grpcio = { version = "0.13.0", default-features = false, features = ["protobufv3-codec", "openssl"] }
-lazy_static = "1.4"
log = "0.4"
num-derive = "0.3"
num-traits = "0.2"
diff --git a/system/gd/rust/topshim/facade/Android.bp b/system/gd/rust/topshim/facade/Android.bp
index 5de585bf76..2b40d3f2df 100644
--- a/system/gd/rust/topshim/facade/Android.bp
+++ b/system/gd/rust/topshim/facade/Android.bp
@@ -27,7 +27,6 @@ rust_defaults {
"libclap",
"libfutures",
"libgrpcio",
- "liblazy_static",
"liblog_rust",
"libnix",
"libnum_traits",
diff --git a/system/gd/rust/topshim/facade/src/main.rs b/system/gd/rust/topshim/facade/src/main.rs
index b55d41f122..65ac4c72a1 100644
--- a/system/gd/rust/topshim/facade/src/main.rs
+++ b/system/gd/rust/topshim/facade/src/main.rs
@@ -7,7 +7,6 @@ use futures::channel::mpsc;
use futures::executor::block_on;
use futures::stream::StreamExt;
use grpcio::*;
-use lazy_static::lazy_static;
use log::debug;
use nix::sys::signal;
use std::sync::{Arc, Mutex};
@@ -149,9 +148,7 @@ unsafe fn install_sigint() -> mpsc::UnboundedReceiver<()> {
rx
}
-lazy_static! {
- static ref SIGINT_TX: Mutex<Option<mpsc::UnboundedSender<()>>> = Mutex::new(None);
-}
+static SIGINT_TX: Mutex<Option<mpsc::UnboundedSender<()>>> = Mutex::new(None);
extern "C" fn handle_sigint(_: i32) {
let mut sigint_tx = SIGINT_TX.lock().unwrap();
diff --git a/system/gd/rust/topshim/src/topstack.rs b/system/gd/rust/topshim/src/topstack.rs
index ddb4f5e2dc..9688a6ced9 100644
--- a/system/gd/rust/topshim/src/topstack.rs
+++ b/system/gd/rust/topshim/src/topstack.rs
@@ -2,33 +2,30 @@
//!
//! Helpers for dealing with the stack on top of the Bluetooth interface.
-use lazy_static::lazy_static;
use std::any::{Any, TypeId};
use std::collections::HashMap;
-use std::sync::{Arc, Mutex};
+use std::sync::{Arc, LazyLock, Mutex};
use tokio::runtime::{Builder, Runtime};
-lazy_static! {
- // Shared runtime for topshim handlers. All async tasks will get run by this
- // runtime and this will properly serialize all spawned tasks.
- pub static ref RUNTIME: Arc<Runtime> = Arc::new(
+// Shared runtime for topshim handlers. All async tasks will get run by this
+// runtime and this will properly serialize all spawned tasks.
+pub static RUNTIME: LazyLock<Arc<Runtime>> = LazyLock::new(|| {
+ Arc::new(
Builder::new_multi_thread()
.worker_threads(1)
.max_blocking_threads(1)
.enable_all()
.build()
- .unwrap()
- );
-}
+ .unwrap(),
+ )
+});
pub fn get_runtime() -> Arc<Runtime> {
RUNTIME.clone()
}
-lazy_static! {
- static ref CB_DISPATCHER: Arc<Mutex<DispatchContainer>> =
- Arc::new(Mutex::new(DispatchContainer { instances: HashMap::new() }));
-}
+static CB_DISPATCHER: LazyLock<Arc<Mutex<DispatchContainer>>> =
+ LazyLock::new(|| Arc::new(Mutex::new(DispatchContainer { instances: HashMap::new() })));
/// A Box-ed struct that implements a `dispatch` fn.
///