33//! Adapts the approach from https://github.com/trishume/syntect/pull/270 with feature flags for the
44//! different backends.
55
6- use lazycell :: AtomicLazyCell ;
6+ use once_cell :: sync :: OnceCell ;
77use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
88use std:: hash:: { Hash , Hasher } ;
99
@@ -12,14 +12,14 @@ pub use regex_impl::{CaptureMatches, Captures, Match, Matches};
1212#[ derive( Debug ) ]
1313pub struct Regex {
1414 regex_str : String ,
15- regex : AtomicLazyCell < regex_impl:: Regex > ,
15+ regex : OnceCell < regex_impl:: Regex > ,
1616}
1717
1818impl Clone for Regex {
1919 fn clone ( & self ) -> Self {
2020 Regex {
2121 regex_str : self . regex_str . clone ( ) ,
22- regex : AtomicLazyCell :: new ( ) ,
22+ regex : OnceCell :: new ( ) ,
2323 }
2424 }
2525}
@@ -57,7 +57,7 @@ impl Regex {
5757 pub fn new ( regex_str : String ) -> Self {
5858 Self {
5959 regex_str,
60- regex : AtomicLazyCell :: new ( ) ,
60+ regex : OnceCell :: new ( ) ,
6161 }
6262 }
6363
@@ -68,15 +68,10 @@ impl Regex {
6868 }
6969
7070 fn regex ( & self ) -> & regex_impl:: Regex {
71- if let Some ( regex) = self . regex . borrow ( ) {
72- regex
73- } else {
74- let regex = regex_impl:: Regex :: new ( & self . regex_str ) . unwrap_or_else ( |_| {
75- panic ! ( "regex string should be pre-tested: {}" , self . regex_str)
76- } ) ;
77- self . regex . fill ( regex) . ok ( ) ;
78- self . regex . borrow ( ) . unwrap ( )
79- }
71+ self . regex . get_or_init ( || {
72+ regex_impl:: Regex :: new ( & self . regex_str )
73+ . unwrap_or_else ( |_| panic ! ( "regex string should be pre-tested: {}" , self . regex_str) )
74+ } )
8075 }
8176
8277 pub fn is_match ( & self , text : & str ) -> bool {
0 commit comments