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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#![no_std]
extern crate self as cppvtbl;
use core::{
ffi::c_void,
marker::PhantomPinned,
mem,
ops::{Deref, DerefMut},
pin::Pin,
};
#[cfg(feature = "macros")]
pub use cppvtbl_macros::{impl_vtables, vtable};
#[repr(C)]
pub struct WithVtables<T: HasVtables> {
vtables: T::Tables,
value: T,
}
impl<T: HasVtables> WithVtables<T> {
pub fn new(value: T) -> Self {
Self {
vtables: T::TABLES,
value,
}
}
pub fn vtables(&self) -> &T::Tables {
&self.vtables
}
pub fn vtables_mut(&mut self) -> *mut T::Tables {
&mut self.vtables
}
}
impl<T: HasVtables> From<T> for WithVtables<T> {
fn from(value: T) -> Self {
Self::new(value)
}
}
impl<T: HasVtables> Deref for WithVtables<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl<T: HasVtables> DerefMut for WithVtables<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.value
}
}
pub unsafe trait HasVtables {
type Tables;
const TABLES: Self::Tables;
}
#[repr(transparent)]
pub struct VtableRef<V: 'static>(&'static V, PhantomPinned);
impl<V: 'static> VtableRef<V> {
pub const unsafe fn new(vtable: &'static V) -> Self {
Self(vtable, PhantomPinned)
}
pub fn table(&self) -> &'static V {
self.0
}
pub fn into_raw(v: &Self) -> *const c_void {
v as *const _ as *const c_void
}
pub fn into_raw_mut(v: &mut Self) -> *mut c_void {
v as *mut _ as *mut c_void
}
pub unsafe fn from_raw<'r>(raw: *const c_void) -> &'r Self {
mem::transmute(raw as *const _ as *const Self)
}
pub unsafe fn from_raw_mut<'r>(raw: *mut c_void) -> &'r mut Self {
mem::transmute(raw as *mut _ as *mut Self)
}
}
pub trait HasVtable<V>: Sized + HasVtables {
fn get(from: &WithVtables<Self>) -> &VtableRef<V>;
fn get_mut(from: &mut WithVtables<Self>) -> Pin<&mut VtableRef<V>>;
}