Skip to content

Commit

Permalink
lib(rust): List all possible expanded macros
Browse files Browse the repository at this point in the history
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
DrSensor committed Oct 17, 2023
1 parent cdd4d38 commit e68de9a
Showing 1 changed file with 94 additions and 10 deletions.
104 changes: 94 additions & 10 deletions examples/rust/cargo/no-macro/src/lib.rs
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()
// }
// }
///////////////////////////////////////////////////////////////

0 comments on commit e68de9a

Please sign in to comment.