3
3
//! Adapts the approach from https://github.com/trishume/syntect/pull/270 with feature flags for the
4
4
//! different backends.
5
5
6
- use lazycell :: AtomicLazyCell ;
6
+ use once_cell :: sync :: OnceCell ;
7
7
use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
8
8
use std:: hash:: { Hash , Hasher } ;
9
9
@@ -12,14 +12,14 @@ pub use regex_impl::{CaptureMatches, Captures, Match, Matches};
12
12
#[ derive( Debug ) ]
13
13
pub struct Regex {
14
14
regex_str : String ,
15
- regex : AtomicLazyCell < regex_impl:: Regex > ,
15
+ regex : OnceCell < regex_impl:: Regex > ,
16
16
}
17
17
18
18
impl Clone for Regex {
19
19
fn clone ( & self ) -> Self {
20
20
Regex {
21
21
regex_str : self . regex_str . clone ( ) ,
22
- regex : AtomicLazyCell :: new ( ) ,
22
+ regex : OnceCell :: new ( ) ,
23
23
}
24
24
}
25
25
}
@@ -57,7 +57,7 @@ impl Regex {
57
57
pub fn new ( regex_str : String ) -> Self {
58
58
Self {
59
59
regex_str,
60
- regex : AtomicLazyCell :: new ( ) ,
60
+ regex : OnceCell :: new ( ) ,
61
61
}
62
62
}
63
63
@@ -68,15 +68,10 @@ impl Regex {
68
68
}
69
69
70
70
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
+ } )
80
75
}
81
76
82
77
pub fn is_match ( & self , text : & str ) -> bool {
0 commit comments