mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2025-01-05 04:15:20 +00:00
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:
parent
32b855df3a
commit
f1dc7f442a
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -554,10 +554,10 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"epoll",
|
"epoll",
|
||||||
"getrandom",
|
"getrandom",
|
||||||
"lazy_static",
|
|
||||||
"libc",
|
"libc",
|
||||||
"log",
|
"log",
|
||||||
"net_gen",
|
"net_gen",
|
||||||
|
"once_cell",
|
||||||
"pnet",
|
"pnet",
|
||||||
"pnet_datalink",
|
"pnet_datalink",
|
||||||
"rate_limiter",
|
"rate_limiter",
|
||||||
|
@ -21,7 +21,7 @@ vm-virtio = { path = "../vm-virtio" }
|
|||||||
vmm-sys-util = "0.9.0"
|
vmm-sys-util = "0.9.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
lazy_static = "1.4.0"
|
once_cell = "1.12.0"
|
||||||
pnet = "0.31.0"
|
pnet = "0.31.0"
|
||||||
pnet_datalink = "0.31.0"
|
pnet_datalink = "0.31.0"
|
||||||
serde_json = "1.0.81"
|
serde_json = "1.0.81"
|
||||||
|
@ -5,11 +5,6 @@
|
|||||||
// Use of this source code is governed by a BSD-style license that can be
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
// found in the THIRD-PARTY file.
|
// 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]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
|
|
||||||
|
@ -401,6 +401,8 @@ mod tests {
|
|||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
use pnet::packet::ethernet::{EtherTypes, EthernetPacket, MutableEthernetPacket};
|
use pnet::packet::ethernet::{EtherTypes, EthernetPacket, MutableEthernetPacket};
|
||||||
use pnet::packet::ip::IpNextHeaderProtocols;
|
use pnet::packet::ip::IpNextHeaderProtocols;
|
||||||
use pnet::packet::ipv4::{Ipv4Packet, MutableIpv4Packet};
|
use pnet::packet::ipv4::{Ipv4Packet, MutableIpv4Packet};
|
||||||
@ -415,16 +417,13 @@ mod tests {
|
|||||||
static DATA_STRING: &str = "test for tap";
|
static DATA_STRING: &str = "test for tap";
|
||||||
static SUBNET_MASK: &str = "255.255.255.0";
|
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
|
// We needed to have a mutex as a global variable, so we used once_cell for testing. The main
|
||||||
// lazy_static! macro for testing. The main potential problem, caused by tests being run in
|
// potential problem, caused by tests being run in parallel by cargo, is creating different
|
||||||
// parallel by cargo, is creating different TAPs and trying to associate the same address,
|
// TAPs and trying to associate the same address, so we hide the IP address &str behind this
|
||||||
// so we hide the IP address &str behind this mutex, more as a convention to remember to lock
|
// mutex, more as a convention to remember to lock it at the very beginning of each function
|
||||||
// it at the very beginning of each function susceptible to this issue. Another variant is
|
// susceptible to this issue. Another variant is to use a different IP address per function,
|
||||||
// to use a different IP address per function, but we must remember to pick an unique one
|
// but we must remember to pick an unique one each time.
|
||||||
// each time.
|
static TAP_IP_LOCK: Lazy<Mutex<&'static str>> = Lazy::new(|| Mutex::new("192.168.241.1"));
|
||||||
lazy_static! {
|
|
||||||
static ref TAP_IP_LOCK: Mutex<&'static str> = Mutex::new("192.168.241.1");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Describes the outcomes we are currently interested in when parsing a packet (we use
|
// Describes the outcomes we are currently interested in when parsing a packet (we use
|
||||||
// an UDP packet for testing).
|
// an UDP packet for testing).
|
||||||
|
Loading…
Reference in New Issue
Block a user