]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/wasm/thread_local.rs
Simplify SaveHandler trait
[rust.git] / src / libstd / sys / wasm / thread_local.rs
1 use crate::boxed::Box;
2 use crate::ptr;
3
4 pub type Key = usize;
5
6 struct Allocated {
7     value: *mut u8,
8     dtor: Option<unsafe extern fn(*mut u8)>,
9 }
10
11 #[inline]
12 pub unsafe fn create(dtor: Option<unsafe extern fn(*mut u8)>) -> Key {
13     Box::into_raw(Box::new(Allocated {
14         value: ptr::null_mut(),
15         dtor,
16     })) as usize
17 }
18
19 #[inline]
20 pub unsafe fn set(key: Key, value: *mut u8) {
21     (*(key as *mut Allocated)).value = value;
22 }
23
24 #[inline]
25 pub unsafe fn get(key: Key) -> *mut u8 {
26     (*(key as *mut Allocated)).value
27 }
28
29 #[inline]
30 pub unsafe fn destroy(key: Key) {
31     let key = Box::from_raw(key as *mut Allocated);
32     if let Some(f) = key.dtor {
33         f(key.value);
34     }
35 }
36
37 #[inline]
38 pub fn requires_synchronized_create() -> bool {
39     false
40 }