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 <liuwe@microsoft.com>
This commit is contained in:
Wei Liu 2022-06-20 14:10:55 +00:00 committed by Liu Wei
parent 32b855df3a
commit f1dc7f442a
4 changed files with 11 additions and 17 deletions

2
Cargo.lock generated
View File

@ -554,10 +554,10 @@ version = "0.1.0"
dependencies = [
"epoll",
"getrandom",
"lazy_static",
"libc",
"log",
"net_gen",
"once_cell",
"pnet",
"pnet_datalink",
"rate_limiter",

View File

@ -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"

View File

@ -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;

View File

@ -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<Mutex<&'static str>> = 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).