-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib(rust): List all possible expanded macros
MaybeUninit has smallest wasm size but not thread-safe OnceLock has biggest wasm size while being thread-safe I'm doubt thread-safe is important since the current architecture may prevent data race.
- Loading branch information
Showing
1 changed file
with
94 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,103 @@ | ||
use libnusa::{number, Accessor}; | ||
use libnusa::{number, Accessor, Null}; | ||
|
||
#[derive(Default)] | ||
struct This { | ||
struct Counter { | ||
count: number::u8, | ||
tick: Null<number::f32>, | ||
} | ||
impl Counter { | ||
fn increment(&self) { | ||
self.count.set(self.count.get() + 1); | ||
self.tick.set(None); | ||
} | ||
} | ||
|
||
///////////////// using unsafe MaybeUninit ////////////////// | ||
use core::mem::MaybeUninit; | ||
static mut THIS: MaybeUninit<Counter> = MaybeUninit::uninit(); | ||
|
||
thread_local! { | ||
static THIS: This = This { | ||
#[export_name = "ns.init"] | ||
unsafe fn main() { | ||
THIS.write(Counter { | ||
count: number::u8::prop_name("count"), | ||
}; | ||
tick: Null::prop_name("tick"), | ||
}); | ||
} | ||
|
||
#[no_mangle] | ||
fn increment() { | ||
THIS.with(|this| { | ||
this.count.set(this.count.get() + 1); | ||
}) | ||
unsafe fn increment() { | ||
THIS.assume_init_ref().increment() | ||
} | ||
///////////////////////////////////////////////////////////// | ||
|
||
////////////////// using unsafe mut Option ////////////////// | ||
// static mut THIS: Option<Counter> = None; | ||
|
||
// #[export_name = "ns.init"] | ||
// unsafe fn main() { | ||
// THIS = Some(Counter { | ||
// count: number::u8::prop_name("count"), | ||
// tick: Null::prop_name("tick"), | ||
// }); | ||
// } | ||
|
||
// #[no_mangle] | ||
// unsafe fn increment() { | ||
// if let Some(this) = &THIS { | ||
// this.increment() | ||
// } | ||
// } | ||
/////////////////////////////////////////////////////////////// | ||
|
||
////////////// using TLS (Thread Local Storage) ////////////// | ||
// thread_local! { | ||
// static THIS: Counter = Counter { | ||
// count: number::u8::prop_name("count"), | ||
// tick: Null::prop_name("tick"), | ||
// }; | ||
// } | ||
|
||
// #[no_mangle] | ||
// fn increment() { | ||
// THIS.with(|this| this.increment()) | ||
// } | ||
///////////////////////////////////////////////////////////// | ||
|
||
///////////////////// using TLS in Cell ///////////////////// | ||
// use core::cell::Cell; | ||
// thread_local! { | ||
// static THIS: Cell<Counter> = panic!(); | ||
// } | ||
|
||
// #[export_name = "ns.init"] | ||
// fn main() { | ||
// THIS.set(Counter { | ||
// count: number::u8::prop_name("count"), | ||
// tick: Null::prop_name("tick"), | ||
// }); | ||
// } | ||
|
||
// #[no_mangle] | ||
// fn increment() { | ||
// THIS.get().increment(); | ||
// } | ||
///////////////////////////////////////////////////////////// | ||
|
||
/////////////////// using OnceLock //////////////////////////// | ||
// use std::sync::OnceLock; | ||
// static THIS: OnceLock<Counter> = OnceLock::new(); | ||
|
||
// #[export_name = "ns.init"] | ||
// fn main() { | ||
// let _ = THIS.set(Counter { | ||
// count: number::u8::prop_name("count"), | ||
// tick: Null::prop_name("tick"), | ||
// }); | ||
// } | ||
|
||
// #[no_mangle] | ||
// fn increment() { | ||
// if let Some(this) = THIS.get() { | ||
// this.increment() | ||
// } | ||
// } | ||
/////////////////////////////////////////////////////////////// |