Skip to content

Commit

Permalink
Replace lazy_static as a dependency with LazyLock when using regex
Browse files Browse the repository at this point in the history
  • Loading branch information
atezet authored and greyblake committed Aug 10, 2024
1 parent ae0699a commit 70bc7cc
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 12 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ At the moment the string inner type supports only `String` (owned) type.

Requirements:
* `regex` feature of `nutype` is enabled.
* You have to explicitly include `regex` and `lazy_static` as dependencies.
* You have to explicitly include `regex` as a dependency.

There are a number of ways you can use regex.

Expand Down Expand Up @@ -354,7 +354,7 @@ assert_eq!(name.into_inner(), " boo ");

* `arbitrary` - enables derive of [`arbitrary::Arbitrary`](https://docs.rs/arbitrary/latest/arbitrary/trait.Arbitrary.html).
* `new_unchecked` - enables generation of unsafe `::new_unchecked()` function.
* `regex` - allows to use `regex = ` validation on string-based types. Note: your crate also has to explicitly have `regex` and `lazy_static` within dependencies.
* `regex` - allows to use `regex = ` validation on string-based types. Note: your crate also has to explicitly have `regex` within its dependencies.
* `serde` - integrations with [`serde`](https://crates.io/crates/serde) crate. Allows to derive `Serialize` and `Deserialize` traits.
* `schemars08` - allows to derive [`JsonSchema`](https://docs.rs/schemars/0.8.12/schemars/trait.JsonSchema.html) trait of [schemars](https://crates.io/crates/schemars) crate. Note that at the moment validation rules are not respected.
* `std` - enabled by default. Use `default-features = false` to disable.
Expand Down
1 change: 0 additions & 1 deletion dummy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ serde_json = "*"
schemars = "*"
regex = "*"
once_cell = "*"
lazy_static = "*"
ron = "0.8.1"
arbitrary = "1.3.2"
num = "0.4.3"
4 changes: 2 additions & 2 deletions nutype/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
//!
//! Requirements:
//! * `regex` feature of `nutype` is enabled.
//! * You crate have to explicitly include `regex` and `lazy_static` dependencies.
//! * You crate have to explicitly include `regex` as a dependency.
//!
//! There are a number of ways you can use regex.
//!
Expand Down Expand Up @@ -406,7 +406,7 @@
//!
//! * `arbitrary` - enables derive of [`arbitrary::Arbitrary`](https://docs.rs/arbitrary/latest/arbitrary/trait.Arbitrary.html).
//! * `new_unchecked` - enables generation of unsafe `::new_unchecked()` function.
//! * `regex` - allows to use `regex = ` validation on string-based types. Note: your crate also has to explicitly have `regex` and `lazy_static` within dependencies.
//! * `regex` - allows to use `regex = ` validation on string-based types. Note: your crate also has to explicitly have `regex` within its dependencies.
//! * `serde` - integrations with [`serde`](https://crates.io/crates/serde) crate. Allows to derive `Serialize` and `Deserialize` traits.
//! * `schemars08` - allows to derive [`JsonSchema`](https://docs.rs/schemars/0.8.12/schemars/trait.JsonSchema.html) trait of [schemars](https://crates.io/crates/schemars) crate. Note that at the moment validation rules are not respected.
//! * `std` - enabled by default. Use `default-features = false` to disable.
Expand Down
8 changes: 3 additions & 5 deletions nutype_macros/src/string/gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,9 @@ impl GenerateNewtype for StringNewtype {
match regex_def {
RegexDef::StringLiteral(regex_str_lit) => {
quote!(
lazy_static::lazy_static! {
// Make up a sufficiently unique regex name to ensure that it does
// not clashes with anything import with `use super::*`.
static ref __NUTYPE_REGEX__: ::regex::Regex = ::regex::Regex::new(#regex_str_lit).expect("Nutype failed to a build a regex");
}
// Make up a sufficiently unique regex name to ensure that it does
// not clashes with anything import with `use super::*`.
static __NUTYPE_REGEX__: ::std::sync::LazyLock<::regex::Regex> = ::std::sync::LazyLock::new(|| ::regex::Regex::new(#regex_str_lit).expect("Nutype failed to a build a regex"));
if !__NUTYPE_REGEX__.is_match(&val) {
return Err(#error_type_name::RegexViolated);
}
Expand Down
2 changes: 1 addition & 1 deletion nutype_macros/src/string/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl Parse for SpannedStringValidator {
} else {
let msg = concat!(
"To validate string types with regex, the feature `regex` of the crate `nutype` must be enabled.\n",
"IMPORTANT: Make sure that your crate EXPLICITLY depends on `regex` and `lazy_static` crates.\n",
"IMPORTANT: Make sure that your crate EXPLICITLY depends on the `regex` crate.\n",
"And... don't forget to take care of yourself and your beloved ones. That is even more important.",
);
Err(syn::Error::new(ident.span(), msg))
Expand Down

0 comments on commit 70bc7cc

Please sign in to comment.