Skip to content

Commit

Permalink
Replace lazy_static with once_cell
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored and djc committed May 24, 2023
1 parent 1d9d50c commit a10b0b1
Show file tree
Hide file tree
Showing 16 changed files with 504 additions and 334 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ idna = "0.3.0"
ipconfig = "0.3.0"
ipnet = "2.3.0"
js-sys = "0.3.44"
lazy_static = "1.2.0"
once_cell = "1.17.1"
libfuzzer-sys = "0.4"
lru-cache = "0.1.2"
pin-utils = "0.1.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ cfg-if.workspace = true
data-encoding.workspace = true
futures-channel = { workspace = true, default-features = false, features = ["std"] }
futures-util = { workspace = true, default-features = false, features = ["std"] }
lazy_static.workspace = true
once_cell.workspace = true
openssl = { workspace = true, features = ["v102", "v110"], optional = true }
radix_trie.workspace = true
rand.workspace = true
Expand Down
266 changes: 200 additions & 66 deletions crates/client/src/rr/zone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub use crate::proto::rr::domain::usage::*;
use crate::proto::rr::domain::{Label, Name};
use crate::proto::serialize::binary::BinEncodable;

use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use radix_trie::{Trie, TrieKey};

// Reserved reverse IPs
Expand All @@ -25,48 +25,178 @@ use radix_trie::{Trie, TrieKey};
// 19.172.in-addr.arpa. 24.172.in-addr.arpa. 31.172.in-addr.arpa.
// 20.172.in-addr.arpa. 25.172.in-addr.arpa. 168.192.in-addr.arpa.
// ```
lazy_static! {
/// 10.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_10: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("10").unwrap().append_domain(&IN_ADDR_ARPA).unwrap());

static ref IN_ADDR_ARPA_172: Name = Name::from_ascii("172").unwrap().append_domain(&IN_ADDR_ARPA).unwrap();

/// 16.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_16: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("16").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 17.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_17: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("17").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 18.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_18: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("18").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 19.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_19: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("19").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 20.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_20: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("20").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 21.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_21: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("21").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 22.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_22: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("22").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 23.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_23: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("23").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 24.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_24: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("24").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 25.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_25: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("25").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 26.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_26: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("26").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 27.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_27: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("27").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 28.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_28: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("28").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 29.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_29: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("29").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 30.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_30: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("30").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());
/// 31.172.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_172_31: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("31").unwrap().append_domain(&IN_ADDR_ARPA_172).unwrap());

/// 168.192.in-addr.arpa. usage
pub static ref IN_ADDR_ARPA_192_168: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("168.192").unwrap().append_domain(&IN_ADDR_ARPA).unwrap());
}

/// 10.in-addr.arpa. usage
pub static IN_ADDR_ARPA_10: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("10")
.unwrap()
.append_domain(&IN_ADDR_ARPA)
.unwrap(),
)
});

static IN_ADDR_ARPA_172: Lazy<Name> = Lazy::new(|| {
Name::from_ascii("172")
.unwrap()
.append_domain(&IN_ADDR_ARPA)
.unwrap()
});

/// 16.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_16: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("16")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 17.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_17: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("17")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 18.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_18: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("18")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 19.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_19: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("19")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 20.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_20: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("20")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 21.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_21: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("21")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 22.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_22: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("22")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 23.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_23: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("23")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 24.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_24: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("24")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 25.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_25: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("25")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 26.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_26: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("26")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 27.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_27: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("27")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 28.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_28: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("28")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 29.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_29: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("29")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 30.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_30: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("30")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});
/// 31.172.in-addr.arpa. usage
pub static IN_ADDR_ARPA_172_31: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("31")
.unwrap()
.append_domain(&IN_ADDR_ARPA_172)
.unwrap(),
)
});

/// 168.192.in-addr.arpa. usage
pub static IN_ADDR_ARPA_192_168: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::reverse(
Name::from_ascii("168.192")
.unwrap()
.append_domain(&IN_ADDR_ARPA)
.unwrap(),
)
});

// example., example.com., example.net., and example.org.
//
Expand All @@ -79,21 +209,27 @@ lazy_static! {
// "example.org.", and any names falling within those domains, are
// special in the following ways:
// ```
lazy_static! {
static ref COM: Label = Label::from_ascii("com").unwrap();
static ref NET: Label = Label::from_ascii("net").unwrap();
static ref ORG: Label = Label::from_ascii("org").unwrap();
static ref EXAMPLE_L: Label = Label::from_ascii("example").unwrap();

/// example. usage
pub static ref EXAMPLE: ZoneUsage = ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone()]).unwrap());
/// example.com. usage
pub static ref EXAMPLE_COM: ZoneUsage = ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone(), COM.clone()]).unwrap());
/// example.com. usage
pub static ref EXAMPLE_NET: ZoneUsage = ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone(), NET.clone()]).unwrap());
/// example.com. usage
pub static ref EXAMPLE_ORG: ZoneUsage = ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone(), ORG.clone()]).unwrap());
}

static COM: Lazy<Label> = Lazy::new(|| Label::from_ascii("com").unwrap());
static NET: Lazy<Label> = Lazy::new(|| Label::from_ascii("net").unwrap());
static ORG: Lazy<Label> = Lazy::new(|| Label::from_ascii("org").unwrap());
static EXAMPLE_L: Lazy<Label> = Lazy::new(|| Label::from_ascii("example").unwrap());

/// example. usage
pub static EXAMPLE: Lazy<ZoneUsage> =
Lazy::new(|| ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone()]).unwrap()));
/// example.com. usage
pub static EXAMPLE_COM: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone(), COM.clone()]).unwrap())
});
/// example.com. usage
pub static EXAMPLE_NET: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone(), NET.clone()]).unwrap())
});
/// example.com. usage
pub static EXAMPLE_ORG: Lazy<ZoneUsage> = Lazy::new(|| {
ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone(), ORG.clone()]).unwrap())
});

// test.
//
Expand All @@ -105,10 +241,10 @@ lazy_static! {
// The domain "test.", and any names falling within ".test.", are
// special in the following ways:
// ```
lazy_static! {
/// test. usage
pub static ref TEST: ZoneUsage = ZoneUsage::test(Name::from_ascii("test.").unwrap());
}

/// test. usage
pub static TEST: Lazy<ZoneUsage> =
Lazy::new(|| ZoneUsage::test(Name::from_ascii("test.").unwrap()));

#[derive(Clone, Eq, PartialEq)]
struct TrieName(Name);
Expand Down Expand Up @@ -258,10 +394,8 @@ impl UsageTrie {
}
}

lazy_static! {
/// All default usage mappings
pub static ref USAGE: UsageTrie = UsageTrie::default();
}
/// All default usage mappings
pub static USAGE: Lazy<UsageTrie> = Lazy::new(UsageTrie::default);

#[cfg(test)]
mod tests {
Expand Down
2 changes: 1 addition & 1 deletion crates/proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ http = { workspace = true, optional = true }
idna.workspace = true
ipnet.workspace = true
js-sys = { workspace = true, optional = true }
lazy_static.workspace = true
native-tls = { workspace = true, optional = true }
once_cell.workspace = true
openssl = { workspace = true, features = ["v102", "v110"], optional = true }
quinn = { workspace = true, optional = true, features = ["log", "runtime-tokio", "tls-rustls"] }
rand.workspace = true
Expand Down
16 changes: 7 additions & 9 deletions crates/proto/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::{fmt, io, sync};
pub use backtrace::Backtrace as ExtBacktrace;
use enum_as_inner::EnumAsInner;
#[cfg(feature = "backtrace")]
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use thiserror::Error;

use crate::op::Header;
Expand All @@ -26,16 +26,14 @@ use crate::rr::dnssec::rdata::tsig::TsigAlgorithm;
use crate::rr::{Name, RecordType};
use crate::serialize::binary::DecodeError;

/// Boolean for checking if backtrace is enabled at runtime
#[cfg(feature = "backtrace")]
#[cfg_attr(docsrs, doc(cfg(feature = "backtrace")))]
lazy_static! {
/// Boolean for checking if backtrace is enabled at runtime
pub static ref ENABLE_BACKTRACE: bool = {
use std::env;
let bt = env::var("RUST_BACKTRACE");
matches!(bt.as_ref().map(|s| s as &str), Ok("full") | Ok("1"))
};
}
pub static ENABLE_BACKTRACE: Lazy<bool> = Lazy::new(|| {
use std::env;
let bt = env::var("RUST_BACKTRACE");
matches!(bt.as_ref().map(|s| s as &str), Ok("full") | Ok("1"))
});

/// Generate a backtrace
///
Expand Down
Loading

0 comments on commit a10b0b1

Please sign in to comment.