From f1dc7f442a17f12ddd4789c8809913e5961e8709 Mon Sep 17 00:00:00 2001 From: Wei Liu Date: Mon, 20 Jun 2022 14:10:55 +0000 Subject: [PATCH] net_util: switch from lazy_static to once_cell Once_cell does not require using macro and is slated to become part of Rust std at some point. Signed-off-by: Wei Liu --- Cargo.lock | 2 +- net_util/Cargo.toml | 2 +- net_util/src/lib.rs | 5 ----- net_util/src/tap.rs | 19 +++++++++---------- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 38828ce0a..8817401dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -554,10 +554,10 @@ version = "0.1.0" dependencies = [ "epoll", "getrandom", - "lazy_static", "libc", "log", "net_gen", + "once_cell", "pnet", "pnet_datalink", "rate_limiter", diff --git a/net_util/Cargo.toml b/net_util/Cargo.toml index c1f3c95fd..f1825f6e0 100644 --- a/net_util/Cargo.toml +++ b/net_util/Cargo.toml @@ -21,7 +21,7 @@ vm-virtio = { path = "../vm-virtio" } vmm-sys-util = "0.9.0" [dev-dependencies] -lazy_static = "1.4.0" +once_cell = "1.12.0" pnet = "0.31.0" pnet_datalink = "0.31.0" serde_json = "1.0.81" diff --git a/net_util/src/lib.rs b/net_util/src/lib.rs index 4607ce989..c62a0aaab 100644 --- a/net_util/src/lib.rs +++ b/net_util/src/lib.rs @@ -5,11 +5,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the THIRD-PARTY file. -// This is only used by the tests module from tap.rs, but we cannot use #[macro_use] unless the -// reference to lazy_static is declared at the root level of the importing crate. -#[cfg(test)] -#[macro_use] -extern crate lazy_static; #[macro_use] extern crate log; diff --git a/net_util/src/tap.rs b/net_util/src/tap.rs index 094e195c9..fac62986d 100644 --- a/net_util/src/tap.rs +++ b/net_util/src/tap.rs @@ -401,6 +401,8 @@ mod tests { use std::thread; use std::time::Duration; + use once_cell::sync::Lazy; + use pnet::packet::ethernet::{EtherTypes, EthernetPacket, MutableEthernetPacket}; use pnet::packet::ip::IpNextHeaderProtocols; use pnet::packet::ipv4::{Ipv4Packet, MutableIpv4Packet}; @@ -415,16 +417,13 @@ mod tests { static DATA_STRING: &str = "test for tap"; static SUBNET_MASK: &str = "255.255.255.0"; - // We needed to have a mutex as a global variable, so we used the crate that provides the - // lazy_static! macro for testing. The main potential problem, caused by tests being run in - // parallel by cargo, is creating different TAPs and trying to associate the same address, - // so we hide the IP address &str behind this mutex, more as a convention to remember to lock - // it at the very beginning of each function susceptible to this issue. Another variant is - // to use a different IP address per function, but we must remember to pick an unique one - // each time. - lazy_static! { - static ref TAP_IP_LOCK: Mutex<&'static str> = Mutex::new("192.168.241.1"); - } + // We needed to have a mutex as a global variable, so we used once_cell for testing. The main + // potential problem, caused by tests being run in parallel by cargo, is creating different + // TAPs and trying to associate the same address, so we hide the IP address &str behind this + // mutex, more as a convention to remember to lock it at the very beginning of each function + // susceptible to this issue. Another variant is to use a different IP address per function, + // but we must remember to pick an unique one each time. + static TAP_IP_LOCK: Lazy> = Lazy::new(|| Mutex::new("192.168.241.1")); // Describes the outcomes we are currently interested in when parsing a packet (we use // an UDP packet for testing).