vtable_rs/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#![no_std]

use core::ops::{Deref, DerefMut};

pub use vtable_rs_proc_macros::vtable;

pub trait VmtInstance<T: 'static>: 'static + VmtLayout {
    const VTABLE: &'static Self::Layout<T>;
}
pub unsafe trait VmtLayout: 'static {
    type Layout<T: 'static>: 'static + Copy;
}

#[repr(C)]
pub struct VPtr<V: VmtLayout + ?Sized, T: 'static>(&'static <V as VmtLayout>::Layout<T>);

impl<V: VmtLayout + ?Sized, T: 'static> Deref for VPtr<V, T> {
    type Target = &'static <V as VmtLayout>::Layout<T>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<V: VmtLayout + ?Sized, T: 'static> DerefMut for VPtr<V, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<V: VmtLayout + ?Sized, T: 'static> core::fmt::Debug for VPtr<V, T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct(core::any::type_name::<Self>())
            .field(
                "vtable",
                &format_args!("{:X}", core::ptr::addr_of!(*self.0) as usize),
            )
            .finish()
    }
}

impl<V: VmtLayout + ?Sized, T: 'static> Clone for VPtr<V, T> {
    fn clone(&self) -> Self {
        Self(self.0)
    }
}

impl<V: VmtLayout + ?Sized, T: 'static> Copy for VPtr<V, T> {}

impl<T, V: VmtInstance<T> + ?Sized> Default for VPtr<V, T> {
    fn default() -> Self {
        VPtr(vmt_instance::<V, T>())
    }
}

pub const fn vmt_instance<V: VmtInstance<T> + ?Sized, T>() -> &'static V::Layout<T> {
    V::VTABLE
}