Skip to content

Commit c1b4f67

Browse files
Pauanalexcrichton
authored andcommitted
Adding in unintern function (#1828)
* Adding in unintern function * Adding in some basic unit tests for interning and uninterning
1 parent 777a12d commit c1b4f67

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/cache/intern.rs

+21
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ cfg_if! {
4040
}
4141
})
4242
}
43+
44+
fn unintern_str(key: &str) {
45+
CACHE.with(|cache| {
46+
let mut cache = cache.entries.borrow_mut();
47+
48+
cache.remove(key);
49+
})
50+
}
4351
}
4452
}
4553

@@ -80,3 +88,16 @@ pub fn intern(s: &str) -> &str {
8088

8189
s
8290
}
91+
92+
93+
/// Removes a Rust string from the intern cache.
94+
///
95+
/// This does the opposite of the [`intern`](fn.intern.html) function.
96+
///
97+
/// If the [`intern`](fn.intern.html) function is called again then it will re-intern the string.
98+
#[allow(unused_variables)]
99+
#[inline]
100+
pub fn unintern(s: &str) {
101+
#[cfg(feature = "enable-interning")]
102+
unintern_str(s);
103+
}

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ if_std! {
7070
mod anyref;
7171

7272
mod cache;
73-
pub use cache::intern::intern;
73+
pub use cache::intern::{intern, unintern};
7474
}
7575

7676
/// Representation of an object owned by JS.

tests/wasm/simple.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use wasm_bindgen::prelude::*;
2-
use wasm_bindgen::JsCast;
2+
use wasm_bindgen::{JsCast, intern, unintern};
33
use wasm_bindgen_test::*;
44

55
#[wasm_bindgen(module = "tests/wasm/simple.js")]
@@ -157,6 +157,9 @@ fn binding_to_unimplemented_apis_doesnt_break_everything() {
157157
fn optional_slices() {
158158
optional_str_none(None);
159159
optional_str_some(Some("x"));
160+
optional_str_some(Some(intern("x")));
161+
unintern("x");
162+
optional_str_some(Some("x"));
160163
optional_slice_none(None);
161164
optional_slice_some(Some(&[1, 2, 3]));
162165
optional_string_none(None);

0 commit comments

Comments
 (0)