Skip to content

Commit c25ee82

Browse files
refactor: update regex test to use OnceLock, refresh binary
1 parent 52ac170 commit c25ee82

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

tests/regex-test/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Source code used to create `/wizer/tests/regex_test.wasm`.
22

33
Rebuild 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
```

tests/regex-test/src/lib.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
use 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"]
78
pub 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]
1413
pub 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
}

tests/regex_test.wasm

-1.27 MB
Binary file not shown.

0 commit comments

Comments
 (0)