-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.rs
49 lines (39 loc) · 1.11 KB
/
util.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use std::mem;
use wasm_bindgen::JsCast;
use wasm_bindgen::JsValue;
// This is a hack until wasm_bindgen's API settles around `anyref`, see
// https://github.com/rustwasm/wasm-bindgen/issues/999
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct JsId {
// TODO: figure out if we could use NonZero here
id: u32,
}
impl JsId {
pub(crate) fn from_value(value: JsValue) -> JsId {
unsafe {
JsId {
id: mem::transmute(value),
}
}
}
/// Only safe to call in the same thread that originally created the `id`.
pub(crate) unsafe fn into_value(id: JsId) -> JsValue {
mem::transmute(id.id)
}
pub(crate) unsafe fn with_value_unchecked<F, T, R>(&self, f: F) -> R
where
F: FnOnce(&T) -> R,
T: JsCast,
{
let value = JsId::into_value(self.clone()).unchecked_into();
let result = f(&value);
mem::forget(value);
result
}
}
pub(crate) fn identical<T>(a: Option<&T>, b: Option<&T>) -> bool
where
T: AsRef<JsValue>,
{
a.map(|t| t.as_ref()) == b.map(|t| t.as_ref())
}