tpit_rt/
lib.rs

1#![no_std]
2
3use core::{
4    marker::PhantomData,
5    mem::forget,
6    num::NonZeroU32,
7    sync::{
8        atomic::{AtomicUsize, Ordering},
9        // Arc,
10    },
11};
12#[repr(transparent)]
13pub struct Tpit<D> {
14    ptr: Option<NonZeroU32>,
15    phantom: PhantomData<D>,
16}
17
18impl<D> Drop for Tpit<D> {
19    fn drop(&mut self) {
20        #[link(wasm_import_module = "tpit")]
21        extern "C" {
22            fn drop(a: u32);
23            fn void(a: u32);
24        }
25
26        if let Some(p) = self.ptr {
27            unsafe {
28                drop(p.into());
29            }
30        }
31    }
32}
33impl<D> Tpit<D> {
34    pub unsafe fn new(ptr: u32) -> Self {
35        Self {
36            ptr: NonZeroU32::new(ptr),
37            phantom: PhantomData,
38        }
39    }
40    pub fn ptr(&self) -> u32 {
41        match self.ptr {
42            Some(a) => {
43                let a: u32 = a.into();
44                a
45            }
46            None => 0,
47        }
48    }
49    pub unsafe fn cast<U>(self) -> Tpit<U> {
50        unsafe { Tpit::<U>::new(self.forget_to_ptr()) }
51    }
52    pub fn forget_to_ptr(self) -> u32 {
53        let x = self.ptr();
54        forget(self);
55        return x;
56    }
57    pub unsafe fn summon(a: &mut u32) -> &mut Self {
58        unsafe { core::mem::transmute(a) }
59    }
60}