nanvm_lib/mem/
object.rs

1use core::{
2    mem::{align_of, size_of},
3    ptr::drop_in_place,
4};
5
6/// Object properties
7pub trait Object: Sized {
8    const OBJECT_ALIGN: usize = align_of::<Self>();
9    #[inline(always)]
10    fn object_size(&self) -> usize {
11        size_of::<Self>()
12    }
13    #[inline(always)]
14    unsafe fn object_drop(&mut self) {
15        drop_in_place(self)
16    }
17}
18
19#[cfg(test)]
20mod test {
21    use wasm_bindgen_test::wasm_bindgen_test;
22
23    use super::Object;
24
25    #[repr(transparent)]
26    struct A(i32);
27
28    impl Object for A {}
29
30    impl Drop for A {
31        fn drop(&mut self) {
32            self.0 += 1;
33        }
34    }
35
36    #[test]
37    #[wasm_bindgen_test]
38    fn test() {
39        assert_eq!(A(5).object_size(), 4);
40        let mut a = A(5);
41        unsafe { a.object_drop() };
42        assert_eq!(a.0, 6);
43    }
44}