diff --git a/src/cache/intern.rs b/src/cache/intern.rs index c63c18d2..c8aa51b2 100644 --- a/src/cache/intern.rs +++ b/src/cache/intern.rs @@ -40,6 +40,14 @@ cfg_if! { } }) } + + fn unintern_str(key: &str) { + CACHE.with(|cache| { + let mut cache = cache.entries.borrow_mut(); + + cache.remove(key); + }) + } } } @@ -80,3 +88,16 @@ pub fn intern(s: &str) -> &str { s } + + +/// Removes a Rust string from the intern cache. +/// +/// This does the opposite of the [`intern`](fn.intern.html) function. +/// +/// If the [`intern`](fn.intern.html) function is called again then it will re-intern the string. +#[allow(unused_variables)] +#[inline] +pub fn unintern(s: &str) { + #[cfg(feature = "enable-interning")] + unintern_str(s); +} diff --git a/src/lib.rs b/src/lib.rs index 811fc145..52bf8880 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,7 +70,7 @@ if_std! { mod anyref; mod cache; - pub use cache::intern::intern; + pub use cache::intern::{intern, unintern}; } /// Representation of an object owned by JS. diff --git a/tests/wasm/simple.rs b/tests/wasm/simple.rs index 568f91de..1363c62e 100644 --- a/tests/wasm/simple.rs +++ b/tests/wasm/simple.rs @@ -1,5 +1,5 @@ use wasm_bindgen::prelude::*; -use wasm_bindgen::JsCast; +use wasm_bindgen::{JsCast, intern, unintern}; use wasm_bindgen_test::*; #[wasm_bindgen(module = "tests/wasm/simple.js")] @@ -157,6 +157,9 @@ fn binding_to_unimplemented_apis_doesnt_break_everything() { fn optional_slices() { optional_str_none(None); optional_str_some(Some("x")); + optional_str_some(Some(intern("x"))); + unintern("x"); + optional_str_some(Some("x")); optional_slice_none(None); optional_slice_some(Some(&[1, 2, 3])); optional_string_none(None);