Skip to content

Commit 49e14b7

Browse files
authored
Remove lazy_static in favor of OnceLock (#2063)
* [esp-metadata] Remove lazy_static in favor of OnceLock * [esp-wifishark] Remove lazy_static in favor of normal initialisation * [ieee802154-sniffer] Shorten SelectorConfig initialisation * [ieee802154-sniffer] Remove lazy_static in favor of normal initialisation
1 parent 0cf7abf commit 49e14b7

File tree

7 files changed

+113
-161
lines changed

7 files changed

+113
-161
lines changed

esp-metadata/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "esp-metadata"
33
version = "0.3.0"
44
edition = "2021"
5-
rust-version = "1.60.0"
5+
rust-version = "1.70.0"
66
description = "Metadata for Espressif devices"
77
repository = "https://github.com/esp-rs/esp-hal"
88
license = "MIT OR Apache-2.0"
@@ -11,6 +11,5 @@ license = "MIT OR Apache-2.0"
1111
anyhow = "1.0.86"
1212
clap = { version = "4.5.16", features = ["derive"], optional = true }
1313
basic-toml = "0.1.9"
14-
lazy_static = "1.5.0"
1514
serde = { version = "1.0.209", features = ["derive"] }
1615
strum = { version = "0.26.3", features = ["derive"] }

esp-metadata/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Crates.io](https://img.shields.io/crates/v/esp-metadata?labelColor=1C2C2E&color=C96329&logo=Rust&style=flat-square)](https://crates.io/crates/esp-metadata)
44
[![docs.rs](https://img.shields.io/docsrs/esp-metadata?labelColor=1C2C2E&color=C96329&logo=rust&style=flat-square)](https://docs.rs/esp-metadata)
5-
![MSRV](https://img.shields.io/badge/MSRV-1.60-blue?labelColor=1C2C2E&style=flat-square)
5+
![MSRV](https://img.shields.io/badge/MSRV-1.70-blue?labelColor=1C2C2E&style=flat-square)
66
![Crates.io](https://img.shields.io/crates/l/esp-metadata?labelColor=1C2C2E&style=flat-square)
77
[![Matrix](https://img.shields.io/matrix/esp-rs:matrix.org?label=join%20matrix&labelColor=1C2C2E&color=BEC5C9&logo=matrix&style=flat-square)](https://matrix.to/#/#esp-rs:matrix.org)
88

@@ -14,7 +14,7 @@ Metadata for Espressif devices, intended for use in [build scripts].
1414

1515
## Minimum Supported Rust Version (MSRV)
1616

17-
This crate is guaranteed to compile on stable Rust 1.60 and up. It _might_
17+
This crate is guaranteed to compile on stable Rust 1.70 and up. It _might_
1818
compile with older versions but that may change in any new patch release.
1919

2020
## License

esp-metadata/src/lib.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
//! Metadata for Espressif devices, primarily intended for use in build scripts.
22
3+
use std::sync::OnceLock;
4+
35
use anyhow::{bail, Result};
46
use strum::IntoEnumIterator;
57

6-
const ESP32_TOML: &str = include_str!("../devices/esp32.toml");
7-
const ESP32C2_TOML: &str = include_str!("../devices/esp32c2.toml");
8-
const ESP32C3_TOML: &str = include_str!("../devices/esp32c3.toml");
9-
const ESP32C6_TOML: &str = include_str!("../devices/esp32c6.toml");
10-
const ESP32H2_TOML: &str = include_str!("../devices/esp32h2.toml");
11-
const ESP32S2_TOML: &str = include_str!("../devices/esp32s2.toml");
12-
const ESP32S3_TOML: &str = include_str!("../devices/esp32s3.toml");
13-
14-
lazy_static::lazy_static! {
15-
static ref ESP32_CFG: Config = basic_toml::from_str(ESP32_TOML).unwrap();
16-
static ref ESP32C2_CFG: Config = basic_toml::from_str(ESP32C2_TOML).unwrap();
17-
static ref ESP32C3_CFG: Config = basic_toml::from_str(ESP32C3_TOML).unwrap();
18-
static ref ESP32C6_CFG: Config = basic_toml::from_str(ESP32C6_TOML).unwrap();
19-
static ref ESP32H2_CFG: Config = basic_toml::from_str(ESP32H2_TOML).unwrap();
20-
static ref ESP32S2_CFG: Config = basic_toml::from_str(ESP32S2_TOML).unwrap();
21-
static ref ESP32S3_CFG: Config = basic_toml::from_str(ESP32S3_TOML).unwrap();
8+
macro_rules! include_toml {
9+
($type:ty, $file:expr) => {{
10+
static LOADED_TOML: OnceLock<$type> = OnceLock::new();
11+
LOADED_TOML.get_or_init(|| basic_toml::from_str(include_str!($file)).unwrap())
12+
}};
2213
}
2314

2415
/// Supported device architectures.
@@ -178,13 +169,13 @@ impl Config {
178169
/// The configuration for the specified chip.
179170
pub fn for_chip(chip: &Chip) -> &Self {
180171
match chip {
181-
Chip::Esp32 => &ESP32_CFG,
182-
Chip::Esp32c2 => &ESP32C2_CFG,
183-
Chip::Esp32c3 => &ESP32C3_CFG,
184-
Chip::Esp32c6 => &ESP32C6_CFG,
185-
Chip::Esp32h2 => &ESP32H2_CFG,
186-
Chip::Esp32s2 => &ESP32S2_CFG,
187-
Chip::Esp32s3 => &ESP32S3_CFG,
172+
Chip::Esp32 => include_toml!(Config, "../devices/esp32.toml"),
173+
Chip::Esp32c2 => include_toml!(Config, "../devices/esp32c2.toml"),
174+
Chip::Esp32c3 => include_toml!(Config, "../devices/esp32c3.toml"),
175+
Chip::Esp32c6 => include_toml!(Config, "../devices/esp32c6.toml"),
176+
Chip::Esp32h2 => include_toml!(Config, "../devices/esp32h2.toml"),
177+
Chip::Esp32s2 => include_toml!(Config, "../devices/esp32s2.toml"),
178+
Chip::Esp32s3 => include_toml!(Config, "../devices/esp32s3.toml"),
188179
}
189180
}
190181

extras/esp-wifishark/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ r-extcap = "0.2.4"
88
pcap-file = "2.0.0"
99
serialport = "4.2.1"
1010
clap = { version = "4.3.5", features = ["derive"] }
11-
lazy_static = "1.4.0"

extras/esp-wifishark/src/main.rs

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::{
44
};
55

66
use clap::Parser;
7-
use lazy_static::lazy_static;
87
use pcap_file::{
98
pcap::{PcapHeader, PcapPacket, PcapWriter},
109
DataLink,
@@ -25,13 +24,16 @@ pub struct AppArgs {
2524
serialport: String,
2625
}
2726

28-
lazy_static! {
29-
static ref METADATA: Metadata = Metadata {
30-
help_url: "http://github.com/esp-rs/esp-wifi".into(),
31-
display_description: "esp-wifi".into(),
32-
..r_extcap::cargo_metadata!()
33-
};
34-
static ref WIFI_CAPTURE_INTERFACE: Interface = Interface {
27+
fn main() {
28+
let args = AppArgs::parse();
29+
30+
if !args.extcap.capture {
31+
if let Some(_filter) = args.extcap.extcap_capture_filter {
32+
std::process::exit(0);
33+
}
34+
}
35+
36+
let wifi_capture_interface = Interface {
3537
value: "wifi".into(),
3638
display: "esp-wifi Ethernet capture".into(),
3739
dlt: Dlt {
@@ -40,7 +42,8 @@ lazy_static! {
4042
display: "Ethernet".into(),
4143
},
4244
};
43-
static ref BT_CAPTURE_INTERFACE: Interface = Interface {
45+
46+
let bt_capture_interface = Interface {
4447
value: "bt".into(),
4548
display: "esp-wifi HCI capture".into(),
4649
dlt: Dlt {
@@ -49,44 +52,43 @@ lazy_static! {
4952
display: "HCI".into(),
5053
},
5154
};
52-
static ref CONFIG_SERIALPORT: StringConfig = StringConfig::builder()
53-
.config_number(1)
54-
.call("serialport")
55-
.display("Serialport")
56-
.tooltip("Serialport to connect to")
57-
.required(false)
58-
.placeholder("")
59-
.build();
60-
}
61-
62-
fn main() {
63-
let args = AppArgs::parse();
64-
65-
if !args.extcap.capture {
66-
if let Some(_filter) = args.extcap.extcap_capture_filter {
67-
std::process::exit(0);
68-
}
69-
}
7055

7156
match args.extcap.run().unwrap() {
7257
ExtcapStep::Interfaces(interfaces_step) => {
58+
let metadata = Metadata {
59+
help_url: "http://github.com/esp-rs/esp-wifi".into(),
60+
display_description: "esp-wifi".into(),
61+
..r_extcap::cargo_metadata!()
62+
};
63+
7364
interfaces_step.list_interfaces(
74-
&METADATA,
75-
&[&*WIFI_CAPTURE_INTERFACE, &*BT_CAPTURE_INTERFACE],
65+
&metadata,
66+
&[&wifi_capture_interface, &bt_capture_interface],
7667
&[],
7768
);
7869
}
7970
ExtcapStep::Dlts(dlts_step) => {
8071
dlts_step
81-
.print_from_interfaces(&[&*WIFI_CAPTURE_INTERFACE, &*BT_CAPTURE_INTERFACE])
72+
.print_from_interfaces(&[&wifi_capture_interface, &bt_capture_interface])
8273
.unwrap();
8374
}
84-
ExtcapStep::Config(config_step) => config_step.list_configs(&[&*CONFIG_SERIALPORT]),
75+
ExtcapStep::Config(config_step) => {
76+
let config_serialport = StringConfig::builder()
77+
.config_number(1)
78+
.call("serialport")
79+
.display("Serialport")
80+
.tooltip("Serialport to connect to")
81+
.required(false)
82+
.placeholder("")
83+
.build();
84+
85+
config_step.list_configs(&[&config_serialport])
86+
}
8587
ExtcapStep::ReloadConfig(_reload_config_step) => {
8688
panic!("Unsupported operation");
8789
}
8890
ExtcapStep::Capture(capture_step) => {
89-
let (data_link, prefix) = if capture_step.interface == WIFI_CAPTURE_INTERFACE.value {
91+
let (data_link, prefix) = if capture_step.interface == wifi_capture_interface.value {
9092
(DataLink::ETHERNET, "@WIFIFRAME [")
9193
} else {
9294
(DataLink::BLUETOOTH_HCI_H4, "@HCIFRAME [")

extras/ieee802154-sniffer/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ r-extcap = "0.2.4"
88
pcap-file = "2.0.0"
99
serialport = "4.2.0"
1010
clap = { version = "4.1.7", features = ["derive"] }
11-
lazy_static = "1.4.0"

extras/ieee802154-sniffer/src/main.rs

Lines changed: 62 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::{
44
};
55

66
use clap::Parser;
7-
use lazy_static::lazy_static;
87
use pcap_file::{
98
pcap::{PcapHeader, PcapPacket, PcapWriter},
109
DataLink,
@@ -28,102 +27,11 @@ pub struct AppArgs {
2827
channel: String,
2928
}
3029

31-
lazy_static! {
32-
static ref METADATA: Metadata = Metadata {
33-
help_url: "http://github.com/esp-rs".into(),
34-
display_description: "esp-ieee802154".into(),
35-
..r_extcap::cargo_metadata!()
36-
};
37-
static ref WIFI_CAPTURE_INTERFACE: Interface = Interface {
38-
value: "802.15.4".into(),
39-
display: "esp-ieee802154 Sniffer".into(),
40-
dlt: Dlt {
41-
data_link_type: DataLink::USER0,
42-
name: "USER0".into(),
43-
display: "IEEE802.15.4".into(),
44-
},
45-
};
46-
static ref CONFIG_SERIALPORT: StringConfig = StringConfig::builder()
47-
.config_number(1)
48-
.call("serialport")
49-
.display("Serialport")
50-
.tooltip("Serialport to connect to")
51-
.required(false)
52-
.placeholder("")
53-
.build();
54-
static ref CONFIG_CHANNEL: SelectorConfig = SelectorConfig::builder()
55-
.config_number(3)
56-
.call("channel")
57-
.display("Channel")
58-
.tooltip("Channel Selector")
59-
.default_options([
60-
ConfigOptionValue::builder()
61-
.value("11")
62-
.display("11")
63-
.default(true)
64-
.build(),
65-
ConfigOptionValue::builder()
66-
.value("12")
67-
.display("12")
68-
.build(),
69-
ConfigOptionValue::builder()
70-
.value("13")
71-
.display("13")
72-
.build(),
73-
ConfigOptionValue::builder()
74-
.value("14")
75-
.display("14")
76-
.build(),
77-
ConfigOptionValue::builder()
78-
.value("15")
79-
.display("15")
80-
.build(),
81-
ConfigOptionValue::builder()
82-
.value("16")
83-
.display("16")
84-
.build(),
85-
ConfigOptionValue::builder()
86-
.value("17")
87-
.display("17")
88-
.build(),
89-
ConfigOptionValue::builder()
90-
.value("18")
91-
.display("18")
92-
.build(),
93-
ConfigOptionValue::builder()
94-
.value("19")
95-
.display("19")
96-
.build(),
97-
ConfigOptionValue::builder()
98-
.value("20")
99-
.display("20")
100-
.build(),
101-
ConfigOptionValue::builder()
102-
.value("21")
103-
.display("21")
104-
.build(),
105-
ConfigOptionValue::builder()
106-
.value("22")
107-
.display("22")
108-
.build(),
109-
ConfigOptionValue::builder()
110-
.value("23")
111-
.display("23")
112-
.build(),
113-
ConfigOptionValue::builder()
114-
.value("24")
115-
.display("24")
116-
.build(),
117-
ConfigOptionValue::builder()
118-
.value("25")
119-
.display("25")
120-
.build(),
121-
ConfigOptionValue::builder()
122-
.value("26")
123-
.display("26")
124-
.build(),
125-
])
126-
.build();
30+
fn config_option_value(value: &'static str) -> ConfigOptionValue {
31+
ConfigOptionValue::builder()
32+
.value(value)
33+
.display(value)
34+
.build()
12735
}
12836

12937
fn main() {
@@ -135,17 +43,71 @@ fn main() {
13543
}
13644
}
13745

46+
let wifi_capture_interface = Interface {
47+
value: "802.15.4".into(),
48+
display: "esp-ieee802154 Sniffer".into(),
49+
dlt: Dlt {
50+
data_link_type: DataLink::USER0,
51+
name: "USER0".into(),
52+
display: "IEEE802.15.4".into(),
53+
},
54+
};
55+
13856
match args.extcap.run().unwrap() {
13957
ExtcapStep::Interfaces(interfaces_step) => {
140-
interfaces_step.list_interfaces(&METADATA, &[&*WIFI_CAPTURE_INTERFACE], &[]);
58+
let metadata = Metadata {
59+
help_url: "http://github.com/esp-rs".into(),
60+
display_description: "esp-ieee802154".into(),
61+
..r_extcap::cargo_metadata!()
62+
};
63+
64+
interfaces_step.list_interfaces(&metadata, &[&wifi_capture_interface], &[]);
14165
}
14266
ExtcapStep::Dlts(dlts_step) => {
14367
dlts_step
144-
.print_from_interfaces(&[&*WIFI_CAPTURE_INTERFACE])
68+
.print_from_interfaces(&[&wifi_capture_interface])
14569
.unwrap();
14670
}
14771
ExtcapStep::Config(config_step) => {
148-
config_step.list_configs(&[&*CONFIG_SERIALPORT, &*CONFIG_CHANNEL])
72+
let config_serialport = StringConfig::builder()
73+
.config_number(1)
74+
.call("serialport")
75+
.display("Serialport")
76+
.tooltip("Serialport to connect to")
77+
.required(false)
78+
.placeholder("")
79+
.build();
80+
81+
let config_channel = SelectorConfig::builder()
82+
.config_number(3)
83+
.call("channel")
84+
.display("Channel")
85+
.tooltip("Channel Selector")
86+
.default_options([
87+
ConfigOptionValue::builder()
88+
.value("11")
89+
.display("11")
90+
.default(true)
91+
.build(),
92+
config_option_value("12"),
93+
config_option_value("13"),
94+
config_option_value("14"),
95+
config_option_value("15"),
96+
config_option_value("16"),
97+
config_option_value("17"),
98+
config_option_value("18"),
99+
config_option_value("19"),
100+
config_option_value("20"),
101+
config_option_value("21"),
102+
config_option_value("22"),
103+
config_option_value("23"),
104+
config_option_value("24"),
105+
config_option_value("25"),
106+
config_option_value("26"),
107+
])
108+
.build();
109+
110+
config_step.list_configs(&[&config_serialport, &config_channel])
149111
}
150112
ExtcapStep::ReloadConfig(_reload_config_step) => {
151113
panic!("Unsupported operation");

0 commit comments

Comments
 (0)