Skip to content

Commit

Permalink
Merge pull request #163 from atezet/use-lazylock
Browse files Browse the repository at this point in the history
Use LazyLock over lazy_static
  • Loading branch information
greyblake authored Jul 29, 2024
2 parents da83cbf + 514de47 commit c4d1878
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ A regular expression can be defined right in place:
pub struct PhoneNumber(String);
```

or it can be defined with `std::sync::LazyLock`:

```rs
use regex::Regex;

static PHONE_NUMBER_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new("^[0-9]{3}-[0-9]{3}$").unwrap());

#[nutype(validate(regex = PHONE_NUMBER_REGEX))]
pub struct PhoneNumber(String);
```

or it can be defined with `lazy_static`:

```rs
Expand Down
1 change: 0 additions & 1 deletion examples/string_regex_email/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ authors = ["Serhii Potapov <blake131313@gmail.com>"]
[dependencies]
nutype = { path = "../../nutype", features = ["regex"] }
regex = "1.10.2"
lazy_static = "1.4.0"
11 changes: 5 additions & 6 deletions examples/string_regex_email/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use lazy_static::lazy_static;
use std::sync::LazyLock;

use nutype::nutype;
use regex::Regex;

lazy_static! {
// Note: this regex is very simplified.
// In reality you'd like to use a more sophisticated regex for email validation.
static ref EMAIL_REGEX: Regex = Regex::new("^\\w+@\\w+\\.\\w+$").unwrap();
}
// Note: this regex is very simplified.
// In reality you'd like to use a more sophisticated regex for email validation.
static EMAIL_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new("^\\w+@\\w+\\.\\w+$").unwrap());

// Note: technically the local part of email address is case-sensitive, but in practice all the
// popular email services (e.g. gmail) make it case-insensitive, so applying `lowercase` is OK.
Expand Down
17 changes: 17 additions & 0 deletions nutype/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,23 @@
//! # }
//! ```
//!
//! or it can be defined with `std::sync::LazyLock`:
//!
//! ```
//! # mod wrapper_module {
//!
//! use nutype::nutype;
//! use std::sync::LazyLock;
//! use regex::Regex;
//!
//! static PHONE_NUMBER_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new("^[0-9]{3}-[0-9]{3}$").unwrap());
//!
//! #[nutype(validate(regex = PHONE_NUMBER_REGEX))]
//! pub struct PhoneNumber(String);
//!
//! # }
//! ```
//!
//! or it can be defined with `lazy_static`:
//!
//! ```
Expand Down
22 changes: 22 additions & 0 deletions test_suite/tests/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,16 @@ mod derive_schemars_json_schema {
#[cfg(feature = "regex")]
mod validation_with_regex {
use super::*;
use std::sync::LazyLock;

use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use regex::Regex;


static PHONE_REGEX_LAZY_LOCK: LazyLock<Regex> =
LazyLock::new(|| Regex::new("^[0-9]{3}-[0-9]{3}$").unwrap());

lazy_static! {
static ref PHONE_REGEX_LAZY_STATIC: Regex = Regex::new("^[0-9]{3}-[0-9]{3}$").unwrap();
}
Expand All @@ -622,6 +628,22 @@ mod validation_with_regex {
assert_eq!(inner, "123-456".to_string());
}

#[test]
fn test_regex_with_lazy_lock() {
#[nutype(validate(regex = PHONE_REGEX_LAZY_LOCK), derive(Debug, PartialEq))]
pub struct PhoneNumber(String);

// PredicateViolated
assert_eq!(
PhoneNumber::try_new("123456"),
Err(PhoneNumberError::RegexViolated)
);

// Valid
let inner = PhoneNumber::try_new("123-456").unwrap().into_inner();
assert_eq!(inner, "123-456".to_string());
}

#[test]
fn test_regex_with_lazy_static() {
#[nutype(validate(regex = PHONE_REGEX_LAZY_STATIC), derive(Debug, PartialEq))]
Expand Down

0 comments on commit c4d1878

Please sign in to comment.