JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Before

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨e432306

⁨giterated-abi/src/vtable/mod.rs⁩ - ⁨854⁩ bytes
Raw
1 use std::{marker::PhantomData, mem::transmute, ops::Deref};
2
3 mod object;
4 pub mod operation;
5 mod setting;
6 mod value;
7
8 pub use object::*;
9 pub use setting::*;
10 pub use value::*;
11
12 pub trait ObjectABI {
13 type VTable;
14 }
15
16 #[repr(transparent)]
17 pub struct VTable<T: ObjectABI> {
18 _marker: PhantomData<T>,
19 }
20
21 impl<T: ObjectABI> VTable<T> {
22 /// Creates a new `VTable<T>` reference to a static vtable in memory.
23 ///
24 /// Might be unsafe? It seems like it should be safe...
25 pub const fn new<V>(vtable: &'static V) -> &'static Self {
26 // We're going to transmute the reference to the typed vtable to us
27 // which will probably be fine
28 unsafe { transmute(vtable) }
29 }
30 }
31
32 impl<T: ObjectABI> Deref for VTable<T> {
33 type Target = T::VTable;
34
35 fn deref(&self) -> &Self::Target {
36 unsafe { transmute(self) }
37 }
38 }
39