From 4896bb78632ea9af58f2e451b2e1873736f40593 Mon Sep 17 00:00:00 2001 From: Folf Date: Fri, 15 Nov 2024 14:03:51 +0000 Subject: [PATCH] feat: Replace OnceCell with static OnceLock for regex compilation (#250) --- atrium-api/src/types/string.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/atrium-api/src/types/string.rs b/atrium-api/src/types/string.rs index 08ce104a..5f1873a0 100644 --- a/atrium-api/src/types/string.rs +++ b/atrium-api/src/types/string.rs @@ -7,7 +7,7 @@ use ipld_core::cid; use langtag::{LanguageTag, LanguageTagBuf}; use regex::Regex; use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer}; -use std::{cell::OnceCell, cmp, ops::Deref, str::FromStr}; +use std::{cmp, ops::Deref, str::FromStr, sync::OnceLock}; /// Common trait implementations for Lexicon string formats that are newtype wrappers /// around `String`. @@ -213,7 +213,7 @@ impl FromStr for Datetime { // datetimes to the subset that is also valid under ISO 8601. Apply a regex that // validates enough of the relevant ISO 8601 format that the RFC 3339 parser can // do the rest. - const RE_ISO_8601: OnceCell = OnceCell::new(); + static RE_ISO_8601: OnceLock = OnceLock::new(); if RE_ISO_8601 .get_or_init(|| Regex::new(r"^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?(Z|(\+[0-9]{2}|\-[0-9][1-9]):[0-9]{2})$").unwrap()) .is_match(s) @@ -267,7 +267,7 @@ impl Did { #[allow(clippy::borrow_interior_mutable_const, clippy::declare_interior_mutable_const)] /// Parses a `Did` from the given string. pub fn new(did: String) -> Result { - const RE_DID: OnceCell = OnceCell::new(); + static RE_DID: OnceLock = OnceLock::new(); // https://atproto.com/specs/did#at-protocol-did-identifier-syntax if did.len() > 2048 { @@ -305,7 +305,7 @@ impl Handle { #[allow(clippy::borrow_interior_mutable_const, clippy::declare_interior_mutable_const)] /// Parses a `Handle` from the given string. pub fn new(handle: String) -> Result { - const RE_HANDLE: OnceCell = OnceCell::new(); + static RE_HANDLE: OnceLock = OnceLock::new(); // https://atproto.com/specs/handle#handle-identifier-syntax if handle.len() > 253 { @@ -338,7 +338,7 @@ impl Nsid { #[allow(clippy::borrow_interior_mutable_const, clippy::declare_interior_mutable_const)] /// Parses an NSID from the given string. pub fn new(nsid: String) -> Result { - const RE_NSID: OnceCell = OnceCell::new(); + static RE_NSID: OnceLock = OnceLock::new(); // https://atproto.com/specs/handle#handle-identifier-syntax if nsid.len() > 317 { @@ -420,7 +420,7 @@ impl Tid { #[allow(clippy::borrow_interior_mutable_const, clippy::declare_interior_mutable_const)] /// Parses a `TID` from the given string. pub fn new(tid: String) -> Result { - const RE_TID: OnceCell = OnceCell::new(); + static RE_TID: OnceLock = OnceLock::new(); if tid.len() != 13 { Err("TID must be 13 characters") @@ -452,7 +452,7 @@ impl RecordKey { #[allow(clippy::borrow_interior_mutable_const, clippy::declare_interior_mutable_const)] /// Parses a `Record Key` from the given string. pub fn new(s: String) -> Result { - const RE_RKEY: OnceCell = OnceCell::new(); + static RE_RKEY: OnceLock = OnceLock::new(); if [".", ".."].contains(&s.as_str()) { Err("Disallowed rkey")