[pbs-devel] [PATCH proxmox 08/12] network-api: remove lazy_static dependency
Maximiliano Sandoval
m.sandoval at proxmox.com
Tue Aug 13 12:57:05 CEST 2024
Signed-off-by: Maximiliano Sandoval <m.sandoval at proxmox.com>
---
proxmox-network-api/Cargo.toml | 2 +-
proxmox-network-api/debian/control | 2 -
proxmox-network-api/src/api_types.rs | 12 ++---
proxmox-network-api/src/config/helper.rs | 35 ++++++-------
proxmox-network-api/src/config/lexer.rs | 67 +++++++++++-------------
proxmox-network-api/src/config/mod.rs | 17 +++---
proxmox-network-api/src/config/parser.rs | 7 ++-
7 files changed, 65 insertions(+), 77 deletions(-)
diff --git a/proxmox-network-api/Cargo.toml b/proxmox-network-api/Cargo.toml
index 8f81b3bd..11fd9cb8 100644
--- a/proxmox-network-api/Cargo.toml
+++ b/proxmox-network-api/Cargo.toml
@@ -7,11 +7,11 @@ license.workspace = true
repository.workspace = true
exclude.workspace = true
description = "Network Management API implementation"
+rust-version.workspace = true
[dependencies]
anyhow.workspace = true
const_format.workspace = true
-lazy_static.workspace = true
regex.workspace = true
serde = { workspace = true, features = ["derive"] }
diff --git a/proxmox-network-api/debian/control b/proxmox-network-api/debian/control
index e02f51fb..fb6571aa 100644
--- a/proxmox-network-api/debian/control
+++ b/proxmox-network-api/debian/control
@@ -8,7 +8,6 @@ Build-Depends: debhelper (>= 12),
libstd-rust-dev <!nocheck>,
librust-anyhow-1+default-dev <!nocheck>,
librust-const-format-0.2+default-dev <!nocheck>,
- librust-lazy-static-1+default-dev (>= 1.4-~~) <!nocheck>,
librust-proxmox-schema-3+api-macro-dev (>= 3.1.1-~~) <!nocheck>,
librust-proxmox-schema-3+api-types-dev (>= 3.1.1-~~) <!nocheck>,
librust-proxmox-schema-3+default-dev (>= 3.1.1-~~) <!nocheck>,
@@ -29,7 +28,6 @@ Depends:
${misc:Depends},
librust-anyhow-1+default-dev,
librust-const-format-0.2+default-dev,
- librust-lazy-static-1+default-dev (>= 1.4-~~),
librust-proxmox-schema-3+api-macro-dev (>= 3.1.1-~~),
librust-proxmox-schema-3+api-types-dev (>= 3.1.1-~~),
librust-proxmox-schema-3+default-dev (>= 3.1.1-~~),
diff --git a/proxmox-network-api/src/api_types.rs b/proxmox-network-api/src/api_types.rs
index ba1615e0..9f12b029 100644
--- a/proxmox-network-api/src/api_types.rs
+++ b/proxmox-network-api/src/api_types.rs
@@ -1,9 +1,9 @@
use std::fmt;
+use std::sync::LazyLock;
use anyhow::{bail, Error};
use serde::{Deserialize, Serialize};
-use lazy_static::lazy_static;
use regex::Regex;
use proxmox_schema::api;
@@ -15,11 +15,11 @@ use proxmox_schema::ArraySchema;
use proxmox_schema::Schema;
use proxmox_schema::StringSchema;
-lazy_static! {
- pub static ref PHYSICAL_NIC_REGEX: Regex = Regex::new(r"^(?:eth\d+|en[^:.]+|ib\d+)$").unwrap();
- pub static ref VLAN_INTERFACE_REGEX: Regex =
- Regex::new(r"^(?P<vlan_raw_device>\S+)\.(?P<vlan_id>\d+)|vlan(?P<vlan_id2>\d+)$").unwrap();
-}
+pub static PHYSICAL_NIC_REGEX: LazyLock<Regex> =
+ LazyLock::new(|| Regex::new(r"^(?:eth\d+|en[^:.]+|ib\d+)$").unwrap());
+pub static VLAN_INTERFACE_REGEX: LazyLock<Regex> = LazyLock::new(|| {
+ Regex::new(r"^(?P<vlan_raw_device>\S+)\.(?P<vlan_id>\d+)|vlan(?P<vlan_id2>\d+)$").unwrap()
+});
pub const NETWORK_INTERFACE_FORMAT: ApiStringFormat = ApiStringFormat::Pattern(&SAFE_ID_REGEX);
diff --git a/proxmox-network-api/src/config/helper.rs b/proxmox-network-api/src/config/helper.rs
index 00464384..9cc23eef 100644
--- a/proxmox-network-api/src/config/helper.rs
+++ b/proxmox-network-api/src/config/helper.rs
@@ -2,10 +2,10 @@ use std::collections::HashMap;
use std::os::unix::io::{AsRawFd, FromRawFd, OwnedFd};
use std::path::Path;
use std::process::Command;
+use std::sync::LazyLock;
use anyhow::{bail, format_err, Error};
use const_format::concatcp;
-use lazy_static::lazy_static;
use nix::ioctl_read_bad;
use nix::sys::socket::{socket, AddressFamily, SockFlag, SockType};
use regex::Regex;
@@ -49,16 +49,14 @@ pub static IPV4_REVERSE_MASK: &[&str] = &[
"255.255.255.255",
];
-lazy_static! {
- pub static ref IPV4_MASK_HASH_LOCALNET: HashMap<&'static str, u8> = {
- let mut map = HashMap::new();
- #[allow(clippy::needless_range_loop)]
- for i in 0..IPV4_REVERSE_MASK.len() {
- map.insert(IPV4_REVERSE_MASK[i], i as u8);
- }
- map
- };
-}
+pub static IPV4_MASK_HASH_LOCALNET: LazyLock<HashMap<&'static str, u8>> = LazyLock::new(|| {
+ let mut map = HashMap::new();
+ #[allow(clippy::needless_range_loop)]
+ for i in 0..IPV4_REVERSE_MASK.len() {
+ map.insert(IPV4_REVERSE_MASK[i], i as u8);
+ }
+ map
+});
pub fn parse_cidr(cidr: &str) -> Result<(String, u8, bool), Error> {
let (address, mask, is_v6) = parse_address_or_cidr(cidr)?;
@@ -93,12 +91,10 @@ pub(crate) fn check_netmask(mask: u8, is_v6: bool) -> Result<(), Error> {
pub(crate) fn parse_address_or_cidr(cidr: &str) -> Result<(String, Option<u8>, bool), Error> {
// NOTE: This is NOT the same regex as in proxmox-schema as this one has capture groups for
// the addresses vs cidr portions!
- lazy_static! {
- pub static ref CIDR_V4_REGEX: Regex =
- Regex::new(concatcp!(r"^(", IPV4RE_STR, r")(?:/(\d{1,2}))?$")).unwrap();
- pub static ref CIDR_V6_REGEX: Regex =
- Regex::new(concatcp!(r"^(", IPV6RE_STR, r")(?:/(\d{1,3}))?$")).unwrap();
- }
+ pub static CIDR_V4_REGEX: LazyLock<Regex> =
+ LazyLock::new(|| Regex::new(concatcp!(r"^(", IPV4RE_STR, r")(?:/(\d{1,2}))?$")).unwrap());
+ pub static CIDR_V6_REGEX: LazyLock<Regex> =
+ LazyLock::new(|| Regex::new(concatcp!(r"^(", IPV6RE_STR, r")(?:/(\d{1,3}))?$")).unwrap());
if let Some(caps) = CIDR_V4_REGEX.captures(cidr) {
let address = &caps[1];
@@ -134,9 +130,8 @@ pub(crate) fn get_network_interfaces() -> Result<HashMap<String, bool>, Error> {
ioctl_read_bad!(get_interface_flags, libc::SIOCGIFFLAGS, ifreq);
- lazy_static! {
- static ref IFACE_LINE_REGEX: Regex = Regex::new(r"^\s*([^:\s]+):").unwrap();
- }
+ static IFACE_LINE_REGEX: LazyLock<Regex> =
+ LazyLock::new(|| Regex::new(r"^\s*([^:\s]+):").unwrap());
let raw = std::fs::read_to_string(PROC_NET_DEV)
.map_err(|err| format_err!("unable to read {} - {}", PROC_NET_DEV, err))?;
diff --git a/proxmox-network-api/src/config/lexer.rs b/proxmox-network-api/src/config/lexer.rs
index d0b7d8cd..6a20f009 100644
--- a/proxmox-network-api/src/config/lexer.rs
+++ b/proxmox-network-api/src/config/lexer.rs
@@ -1,8 +1,7 @@
use std::collections::{HashMap, VecDeque};
use std::io::BufRead;
use std::iter::Iterator;
-
-use lazy_static::lazy_static;
+use std::sync::LazyLock;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Token {
@@ -33,39 +32,37 @@ pub enum Token {
EOF,
}
-lazy_static! {
- static ref KEYWORDS: HashMap<&'static str, Token> = {
- let mut map = HashMap::new();
- map.insert("address", Token::Address);
- map.insert("auto", Token::Auto);
- map.insert("dhcp", Token::DHCP);
- map.insert("gateway", Token::Gateway);
- map.insert("inet", Token::Inet);
- map.insert("inet6", Token::Inet6);
- map.insert("iface", Token::Iface);
- map.insert("loopback", Token::Loopback);
- map.insert("manual", Token::Manual);
- map.insert("netmask", Token::Netmask);
- map.insert("static", Token::Static);
- map.insert("mtu", Token::MTU);
- map.insert("bridge-ports", Token::BridgePorts);
- map.insert("bridge_ports", Token::BridgePorts);
- map.insert("bridge-vlan-aware", Token::BridgeVlanAware);
- map.insert("bridge_vlan_aware", Token::BridgeVlanAware);
- map.insert("vlan-id", Token::VlanId);
- map.insert("vlan_id", Token::VlanId);
- map.insert("vlan-raw-device", Token::VlanRawDevice);
- map.insert("vlan_raw_device", Token::VlanRawDevice);
- map.insert("bond-slaves", Token::BondSlaves);
- map.insert("bond_slaves", Token::BondSlaves);
- map.insert("bond-mode", Token::BondMode);
- map.insert("bond-primary", Token::BondPrimary);
- map.insert("bond_primary", Token::BondPrimary);
- map.insert("bond_xmit_hash_policy", Token::BondXmitHashPolicy);
- map.insert("bond-xmit-hash-policy", Token::BondXmitHashPolicy);
- map
- };
-}
+static KEYWORDS: LazyLock<HashMap<&'static str, Token>> = LazyLock::new(|| {
+ let mut map = HashMap::new();
+ map.insert("address", Token::Address);
+ map.insert("auto", Token::Auto);
+ map.insert("dhcp", Token::DHCP);
+ map.insert("gateway", Token::Gateway);
+ map.insert("inet", Token::Inet);
+ map.insert("inet6", Token::Inet6);
+ map.insert("iface", Token::Iface);
+ map.insert("loopback", Token::Loopback);
+ map.insert("manual", Token::Manual);
+ map.insert("netmask", Token::Netmask);
+ map.insert("static", Token::Static);
+ map.insert("mtu", Token::MTU);
+ map.insert("bridge-ports", Token::BridgePorts);
+ map.insert("bridge_ports", Token::BridgePorts);
+ map.insert("bridge-vlan-aware", Token::BridgeVlanAware);
+ map.insert("bridge_vlan_aware", Token::BridgeVlanAware);
+ map.insert("vlan-id", Token::VlanId);
+ map.insert("vlan_id", Token::VlanId);
+ map.insert("vlan-raw-device", Token::VlanRawDevice);
+ map.insert("vlan_raw_device", Token::VlanRawDevice);
+ map.insert("bond-slaves", Token::BondSlaves);
+ map.insert("bond_slaves", Token::BondSlaves);
+ map.insert("bond-mode", Token::BondMode);
+ map.insert("bond-primary", Token::BondPrimary);
+ map.insert("bond_primary", Token::BondPrimary);
+ map.insert("bond_xmit_hash_policy", Token::BondXmitHashPolicy);
+ map.insert("bond-xmit-hash-policy", Token::BondXmitHashPolicy);
+ map
+});
pub struct Lexer<R> {
input: R,
diff --git a/proxmox-network-api/src/config/mod.rs b/proxmox-network-api/src/config/mod.rs
index d9efeff9..054f53c8 100644
--- a/proxmox-network-api/src/config/mod.rs
+++ b/proxmox-network-api/src/config/mod.rs
@@ -6,9 +6,9 @@ pub use helper::{assert_ifupdown2_installed, network_reload, parse_cidr};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::io::Write;
+use std::sync::LazyLock;
use anyhow::{bail, format_err, Error};
-use lazy_static::lazy_static;
use regex::Regex;
use serde::de::{value, Deserialize, IntoDeserializer};
@@ -23,11 +23,11 @@ use parser::NetworkParser;
use proxmox_config_digest::ConfigDigest;
use proxmox_product_config::{open_api_lockfile, replace_system_config, ApiLockGuard};
-lazy_static! {
- static ref PHYSICAL_NIC_REGEX: Regex = Regex::new(r"^(?:eth\d+|en[^:.]+|ib\d+)$").unwrap();
- static ref VLAN_INTERFACE_REGEX: Regex =
- Regex::new(r"^(?P<vlan_raw_device>\S+)\.(?P<vlan_id>\d+)|vlan(?P<vlan_id2>\d+)$").unwrap();
-}
+static PHYSICAL_NIC_REGEX: LazyLock<Regex> =
+ LazyLock::new(|| Regex::new(r"^(?:eth\d+|en[^:.]+|ib\d+)$").unwrap());
+static VLAN_INTERFACE_REGEX: LazyLock<Regex> = LazyLock::new(|| {
+ Regex::new(r"^(?P<vlan_raw_device>\S+)\.(?P<vlan_id>\d+)|vlan(?P<vlan_id2>\d+)$").unwrap()
+});
pub fn is_physical_nic(iface: &str) -> bool {
PHYSICAL_NIC_REGEX.is_match(iface)
@@ -409,9 +409,8 @@ impl NetworkConfig {
/// Check if bridge ports exists
fn check_bridge_ports(&self) -> Result<(), Error> {
- lazy_static! {
- static ref VLAN_INTERFACE_REGEX: Regex = Regex::new(r"^(\S+)\.(\d+)$").unwrap();
- }
+ static VLAN_INTERFACE_REGEX: LazyLock<Regex> =
+ LazyLock::new(|| Regex::new(r"^(\S+)\.(\d+)$").unwrap());
for (iface, interface) in self.interfaces.iter() {
if let Some(ports) = &interface.bridge_ports {
diff --git a/proxmox-network-api/src/config/parser.rs b/proxmox-network-api/src/config/parser.rs
index 2d20b9e4..d05a67b0 100644
--- a/proxmox-network-api/src/config/parser.rs
+++ b/proxmox-network-api/src/config/parser.rs
@@ -3,9 +3,9 @@ use crate::VLAN_INTERFACE_REGEX;
use std::collections::{HashMap, HashSet};
use std::io::BufRead;
use std::iter::{Iterator, Peekable};
+use std::sync::LazyLock;
use anyhow::{bail, format_err, Error};
-use lazy_static::lazy_static;
use regex::Regex;
use serde::de::{value, Deserialize, IntoDeserializer};
@@ -551,9 +551,8 @@ impl<R: BufRead> NetworkParser<R> {
}
}
- lazy_static! {
- static ref INTERFACE_ALIAS_REGEX: Regex = Regex::new(r"^\S+:\d+$").unwrap();
- }
+ static INTERFACE_ALIAS_REGEX: LazyLock<Regex> =
+ LazyLock::new(|| Regex::new(r"^\S+:\d+$").unwrap());
if let Some(existing_interfaces) = existing_interfaces {
for (iface, active) in existing_interfaces.iter() {
--
2.39.2
More information about the pbs-devel
mailing list