From: Maximiliano Sandoval <m.sandoval@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox v2 08/12] network-api: remove lazy_static dependency
Date: Wed, 14 Aug 2024 09:19:57 +0200 [thread overview]
Message-ID: <20240814072001.53422-8-m.sandoval@proxmox.com> (raw)
In-Reply-To: <20240814072001.53422-1-m.sandoval@proxmox.com>
Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
---
proxmox-network-api/Cargo.toml | 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 ++-
6 files changed, 65 insertions(+), 75 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/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
_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
next prev parent reply other threads:[~2024-08-14 7:20 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-14 7:19 [pbs-devel] [PATCH proxmox v2 01/12] cargo: set msrv to 1.80 Maximiliano Sandoval
2024-08-14 7:19 ` [pbs-devel] [PATCH proxmox v2 02/12] dns-api: remove lazy-static dependency Maximiliano Sandoval
2024-08-14 7:19 ` [pbs-devel] [PATCH proxmox v2 03/12] async: remove lazy_static dependency Maximiliano Sandoval
2024-08-14 7:19 ` [pbs-devel] [PATCH proxmox v2 04/12] subscription: " Maximiliano Sandoval
2024-08-14 7:19 ` [pbs-devel] [PATCH proxmox v2 05/12] rest-server: " Maximiliano Sandoval
2024-08-14 7:19 ` [pbs-devel] [PATCH proxmox v2 06/12] time: " Maximiliano Sandoval
2024-08-14 7:19 ` [pbs-devel] [PATCH proxmox v2 07/12] sys: " Maximiliano Sandoval
2024-08-14 7:19 ` Maximiliano Sandoval [this message]
2024-08-14 7:19 ` [pbs-devel] [PATCH proxmox v2 09/12] auth-api: " Maximiliano Sandoval
2024-08-14 7:19 ` [pbs-devel] [PATCH proxmox v2 10/12] acme-api: " Maximiliano Sandoval
2024-08-14 7:20 ` [pbs-devel] [PATCH proxmox v2 11/12] schema: " Maximiliano Sandoval
2024-08-14 7:20 ` [pbs-devel] [PATCH proxmox v2 12/12] cargo: remove lazy_static dependency on workspace Maximiliano Sandoval
2024-08-14 8:51 ` [pbs-devel] [PATCH proxmox v2 01/12] cargo: set msrv to 1.80 Wolfgang Bumiller
2024-08-14 10:04 ` [pbs-devel] applied-series: " Wolfgang Bumiller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240814072001.53422-8-m.sandoval@proxmox.com \
--to=m.sandoval@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.