File tree Expand file tree Collapse file tree 3 files changed +10
-13
lines changed
Expand file tree Collapse file tree 3 files changed +10
-13
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ Source code used to create `/wizer/tests/regex_test.wasm`.
22
33Rebuild with:
44
5- ```
6- $ cargo build --release --target wasm32-wasi -p regex-test
7- $ cp target/wasm32-wasi /release/regex_test.wasm tests/regex_test.wasm
5+ ``` console
6+ cargo build --release --target wasm32-wasip1 -p regex-test
7+ cp target/wasm32-wasip1 /release/regex_test.wasm tests/regex_test.wasm
88```
Original file line number Diff line number Diff line change 11use regex:: Regex ;
2+ use std:: sync:: OnceLock ;
23
34/// A regex that matches numbers that start with "1".
4- static mut REGEX : Option < Regex > = None ;
5+ static REGEX : OnceLock < Regex > = OnceLock :: new ( ) ;
56
67#[ export_name = "wizer.initialize" ]
78pub fn init ( ) {
8- unsafe {
9- REGEX = Some ( Regex :: new ( r"^1\d*$" ) . unwrap ( ) ) ;
10- }
9+ REGEX . get_or_init ( || Regex :: new ( r"^1\d*$" ) . expect ( "failed to compile regex" ) ) ;
1110}
1211
1312#[ no_mangle]
1413pub fn run ( n : i32 ) -> i32 {
1514 let s = format ! ( "{}" , n) ;
16- let regex = unsafe { REGEX . as_ref ( ) . unwrap ( ) } ;
17- if regex. is_match ( & s) {
18- 42
19- } else {
20- 0
21- }
15+ let regex = REGEX
16+ . get ( )
17+ . expect ( "regex should have been initialized at wizering" ) ;
18+ if regex. is_match ( & s) { 42 } else { 0 }
2219}
You can’t perform that action at this time.
0 commit comments