Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 75e9106

Browse files
committed
Remove lazy_static dependency.
1 parent 4ce4828 commit 75e9106

File tree

27 files changed

+204
-224
lines changed

27 files changed

+204
-224
lines changed

Cargo.lock

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

chirpstack-integration/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,5 @@
2424
async-trait = "0.1"
2525
serde = { version = "1.0", features = ["derive"] }
2626
tokio = { version = "1.44", features = ["macros", "rt-multi-thread"] }
27-
lazy_static = "1.5"
2827
serde_json = "1.0"
2928
toml = "0.8"

chirpstack-integration/src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
#[macro_use]
2-
extern crate lazy_static;
3-
41
use std::io::Cursor;
52
use std::str::FromStr;
3+
use std::sync::LazyLock;
64

75
use anyhow::Result;
86
use async_trait::async_trait;
@@ -13,10 +11,8 @@ use tracing_subscriber::{filter, prelude::*};
1311

1412
use chirpstack_api::{integration as integration_pb, prost::Message};
1513

16-
lazy_static! {
17-
static ref INTEGRATION: RwLock<Option<Box<dyn IntegrationTrait + Sync + Send>>> =
18-
RwLock::new(None);
19-
}
14+
static INTEGRATION: LazyLock<RwLock<Option<Box<dyn IntegrationTrait + Sync + Send>>>> =
15+
LazyLock::new(|| RwLock::new(None));
2016

2117
#[derive(Default, Deserialize, Clone)]
2218
#[serde(default)]

chirpstack/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@
137137
] }
138138

139139
# Misc
140-
lazy_static = "1.5"
141140
uuid = { version = "1.16", features = ["v4", "serde"] }
142141
chrono = "0.4"
143142
async-trait = "0.1"

chirpstack/src/adr/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::collections::HashMap;
2+
use std::sync::LazyLock;
23

34
use anyhow::Result;
45
use async_trait::async_trait;
@@ -14,10 +15,8 @@ pub mod lora_lr_fhss;
1415
pub mod lr_fhss;
1516
pub mod plugin;
1617

17-
lazy_static! {
18-
static ref ADR_ALGORITHMS: RwLock<HashMap<String, Box<dyn Handler + Sync + Send>>> =
19-
RwLock::new(HashMap::new());
20-
}
18+
static ADR_ALGORITHMS: LazyLock<RwLock<HashMap<String, Box<dyn Handler + Sync + Send>>>> =
19+
LazyLock::new(|| RwLock::new(HashMap::new()));
2120

2221
pub async fn setup() -> Result<()> {
2322
info!("Setting up adr algorithms");

chirpstack/src/api/mod.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::sync::LazyLock;
12
use std::time::{Duration, Instant};
23
use std::{
34
future::Future,
@@ -67,33 +68,31 @@ pub mod relay;
6768
pub mod tenant;
6869
pub mod user;
6970

70-
lazy_static! {
71-
static ref GRPC_COUNTER: Family<GrpcLabels, Counter> = {
72-
let counter = Family::<GrpcLabels, Counter>::default();
73-
prometheus::register(
74-
"api_requests_handled",
75-
"Number of API requests handled by service, method and status code",
76-
counter.clone(),
77-
);
78-
counter
79-
};
80-
static ref GRPC_HISTOGRAM: Family<GrpcLabels, Histogram> = {
81-
let histogram = Family::<GrpcLabels, Histogram>::new_with_constructor(|| {
82-
Histogram::new(
83-
[
84-
0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,
85-
]
86-
.into_iter(),
87-
)
88-
});
89-
prometheus::register(
90-
"api_requests_handled_seconds",
91-
"Duration of API requests handled by service, method and status code",
92-
histogram.clone(),
93-
);
94-
histogram
95-
};
96-
}
71+
static GRPC_COUNTER: LazyLock<Family<GrpcLabels, Counter>> = LazyLock::new(|| {
72+
let counter = Family::<GrpcLabels, Counter>::default();
73+
prometheus::register(
74+
"api_requests_handled",
75+
"Number of API requests handled by service, method and status code",
76+
counter.clone(),
77+
);
78+
counter
79+
});
80+
static GRPC_HISTOGRAM: LazyLock<Family<GrpcLabels, Histogram>> = LazyLock::new(|| {
81+
let histogram = Family::<GrpcLabels, Histogram>::new_with_constructor(|| {
82+
Histogram::new(
83+
[
84+
0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,
85+
]
86+
.into_iter(),
87+
)
88+
});
89+
prometheus::register(
90+
"api_requests_handled_seconds",
91+
"Duration of API requests handled by service, method and status code",
92+
histogram.clone(),
93+
);
94+
histogram
95+
});
9796

9897
#[derive(RustEmbed)]
9998
#[folder = "../ui/build"]

chirpstack/src/backend/joinserver.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::Arc;
1+
use std::sync::{Arc, LazyLock};
22

33
use anyhow::Result;
44
use tokio::sync::RwLock;
@@ -8,9 +8,8 @@ use crate::{config, stream};
88
use backend::{Client, ClientConfig};
99
use lrwn::{EUI64Prefix, EUI64};
1010

11-
lazy_static! {
12-
static ref CLIENTS: RwLock<Vec<(EUI64Prefix, Arc<Client>)>> = RwLock::new(vec![]);
13-
}
11+
static CLIENTS: LazyLock<RwLock<Vec<(EUI64Prefix, Arc<Client>)>>> =
12+
LazyLock::new(|| RwLock::new(vec![]));
1413

1514
pub async fn setup() -> Result<()> {
1615
info!("Setting up Join Server clients");

chirpstack/src/backend/roaming.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22
use std::io::Cursor;
33
use std::str::FromStr;
4-
use std::sync::Arc;
4+
use std::sync::{Arc, LazyLock};
55

66
use anyhow::Result;
77
use chrono::{Duration, DurationRound};
@@ -15,9 +15,8 @@ use backend::{Client, ClientConfig, GWInfoElement, ULMetaData};
1515
use chirpstack_api::{common, gw};
1616
use lrwn::{region, DevAddr, NetID, EUI64};
1717

18-
lazy_static! {
19-
static ref CLIENTS: RwLock<HashMap<NetID, Arc<Client>>> = RwLock::new(HashMap::new());
20-
}
18+
static CLIENTS: LazyLock<RwLock<HashMap<NetID, Arc<Client>>>> =
19+
LazyLock::new(|| RwLock::new(HashMap::new()));
2120

2221
pub async fn setup() -> Result<()> {
2322
info!("Setting up roaming clients");

chirpstack/src/config.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::path::Path;
2-
use std::sync::{Arc, Mutex};
2+
use std::sync::{Arc, LazyLock, Mutex};
33
use std::time::Duration;
44
use std::{env, fs};
55

@@ -9,9 +9,8 @@ use serde::{Deserialize, Serialize};
99
use lrwn::region::CommonName;
1010
use lrwn::{AES128Key, DevAddrPrefix, EUI64Prefix, NetID};
1111

12-
lazy_static! {
13-
static ref CONFIG: Mutex<Arc<Configuration>> = Mutex::new(Arc::new(Default::default()));
14-
}
12+
static CONFIG: LazyLock<Mutex<Arc<Configuration>>> =
13+
LazyLock::new(|| Mutex::new(Arc::new(Default::default())));
1514

1615
#[derive(Default, Serialize, Deserialize, Clone)]
1716
#[serde(default)]

chirpstack/src/downlink/classb.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::LazyLock;
2+
13
use aes::cipher::generic_array::GenericArray;
24
use aes::cipher::{BlockEncrypt, KeyInit};
35
use aes::{Aes128, Block};
@@ -7,14 +9,11 @@ use tracing::debug;
79

810
use lrwn::DevAddr;
911

10-
lazy_static! {
11-
static ref BEACON_PERIOD: Duration = Duration::try_seconds(128).unwrap();
12-
static ref BEACON_RESERVED: Duration = Duration::try_milliseconds(2120).unwrap();
13-
static ref BEACON_GUARD: Duration = Duration::try_seconds(3).unwrap();
14-
static ref BEACON_WINDOW: Duration = Duration::try_milliseconds(122880).unwrap();
15-
static ref PING_PERIOD_BASE: usize = 1 << 12;
16-
static ref SLOT_LEN: Duration = Duration::try_milliseconds(30).unwrap();
17-
}
12+
static BEACON_PERIOD: LazyLock<Duration> = LazyLock::new(|| Duration::try_seconds(128).unwrap());
13+
static BEACON_RESERVED: LazyLock<Duration> =
14+
LazyLock::new(|| Duration::try_milliseconds(2120).unwrap());
15+
static PING_PERIOD_BASE: usize = 1 << 12;
16+
static SLOT_LEN: LazyLock<Duration> = LazyLock::new(|| Duration::try_milliseconds(30).unwrap());
1817

1918
pub fn get_beacon_start(ts: Duration) -> Duration {
2019
Duration::try_seconds(ts.num_seconds() - (ts.num_seconds() % BEACON_PERIOD.num_seconds()))
@@ -26,7 +25,7 @@ pub fn get_ping_offset(beacon_ts: Duration, dev_addr: &DevAddr, ping_nb: usize)
2625
return Err(anyhow!("ping_nb must be > 0"));
2726
}
2827

29-
let ping_period = *PING_PERIOD_BASE / ping_nb;
28+
let ping_period = PING_PERIOD_BASE / ping_nb;
3029
let beacon_time = (beacon_ts.num_seconds() % (1 << 32)) as u32;
3130

3231
let key_bytes: [u8; 16] = [0x00; 16];
@@ -54,7 +53,7 @@ pub fn get_next_ping_slot_after(
5453
}
5554

5655
let mut beacon_start_ts = get_beacon_start(after_gps_epoch_ts);
57-
let ping_period = *PING_PERIOD_BASE / ping_nb;
56+
let ping_period = PING_PERIOD_BASE / ping_nb;
5857

5958
loop {
6059
let ping_offset = get_ping_offset(beacon_start_ts, dev_addr, ping_nb)?;
@@ -122,7 +121,7 @@ pub mod test {
122121
for k in 0..8 {
123122
let mut beacon_ts = Duration::zero();
124123
let ping_nb: usize = 1 << k;
125-
let ping_period = *PING_PERIOD_BASE / ping_nb;
124+
let ping_period = PING_PERIOD_BASE / ping_nb;
126125
let dev_addr = DevAddr::from_be_bytes([0, 0, 0, 0]);
127126

128127
for _ in 0..100000 {

0 commit comments

Comments
 (0)