Skip to content

Commit

Permalink
switch lazy_static to once_cell
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Whitehead <[email protected]>
  • Loading branch information
andrewwhitehead committed Aug 13, 2022
1 parent d9edd60 commit f75240e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dashmap = "3.11"
crossbeam = "0.7"
uuid = { version = "0.8", features = ["v4"] }
regex = "1"
lazy_static = "1"
once_cell = "1"
log = "0.4"
asynchronous-codec = "0.6"
async-std = { version = "1", features = ["attributes"], optional = true }
Expand Down
35 changes: 16 additions & 19 deletions src/endpoint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod transport;
pub use host::Host;
pub use transport::Transport;

use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use regex::Regex;
use std::fmt;
use std::net::SocketAddr;
Expand All @@ -16,6 +16,9 @@ pub use error::EndpointError;

pub type Port = u16;

static TRANSPORT_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^([[:lower:]]+):https://(.+)$").unwrap());
static HOST_PORT_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(.+):(\d+)$").unwrap());

/// Represents a ZMQ Endpoint.
///
/// # Examples
Expand Down Expand Up @@ -58,11 +61,6 @@ impl FromStr for Endpoint {
type Err = EndpointError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
lazy_static! {
static ref TRANSPORT_REGEX: Regex = Regex::new(r"^([[:lower:]]+):https://(.+)$").unwrap();
static ref HOST_PORT_REGEX: Regex = Regex::new(r"^(.+):(\d+)$").unwrap();
}

let caps = TRANSPORT_REGEX
.captures(s)
.ok_or(EndpointError::Syntax("Could not parse transport"))?;
Expand Down Expand Up @@ -149,21 +147,20 @@ mod private {
#[cfg(test)]
mod tests {
use super::*;
use lazy_static::lazy_static;

lazy_static! {
static ref PAIRS: Vec<(Endpoint, &'static str)> = vec![
static PAIRS: Lazy<Vec<(Endpoint, &'static str)>> = Lazy::new(|| {
vec![
(
Endpoint::Ipc(Some(PathBuf::from("/tmp/asdf"))),
"ipc:https:///tmp/asdf"
"ipc:https:///tmp/asdf",
),
(
Endpoint::Ipc(Some(PathBuf::from("my/dir_1/dir-2"))),
"ipc:https://my/dir_1/dir-2"
"ipc:https://my/dir_1/dir-2",
),
(
Endpoint::Ipc(Some(PathBuf::from("@abstract/namespace"))),
"ipc:https://@abstract/namespace"
"ipc:https://@abstract/namespace",
),
(
Endpoint::Tcp(Host::Domain("www.example.com".to_string()), 1234),
Expand All @@ -183,22 +180,22 @@ mod tests {
),
(
Endpoint::Tcp(Host::Domain("i❤.ws".to_string()), 80),
"tcp:https://i❤.ws:80"
"tcp:https://i❤.ws:80",
),
(
Endpoint::Tcp(Host::Domain("xn--i-7iq.ws".to_string()), 80),
"tcp:https://xn--i-7iq.ws:80"
"tcp:https://xn--i-7iq.ws:80",
),
(
Endpoint::Tcp(Host::Ipv4("127.0.0.1".parse().unwrap()), 65535),
"tcp:https://127.0.0.1:65535"
"tcp:https://127.0.0.1:65535",
),
(
Endpoint::Tcp(Host::Ipv4("127.0.0.1".parse().unwrap()), 0),
"tcp:https://127.0.0.1:0"
)
];
}
"tcp:https://127.0.0.1:0",
),
]
});

#[test]
fn test_endpoint_display() {
Expand Down

0 comments on commit f75240e

Please sign in to comment.