rrplug/interfaces/interface.rs
1//! definitions for memory layout of an interface and how to init it
2
3use std::{ffi::c_void, marker::PhantomData, ptr::NonNull};
4
5/// the memory layout for a exposed source interface
6#[repr(C)]
7pub struct Interface<T: Sync + Send> {
8 vtable: NonNull<*const c_void>,
9 pub(crate) data: T,
10 marker: PhantomData<*mut T>, // should make it not sync
11}
12
13impl<T: Sync + Send> Interface<T> {
14 /// Creates a new [`Interface<T>`] from an array of associated functions and the data of the struct/interface.
15 pub const fn new(vtable: NonNull<*const c_void>, interface_data: T) -> Self {
16 Self {
17 vtable,
18 data: interface_data,
19 marker: PhantomData,
20 }
21 }
22}
23
24// TODO: add examples here or in some other place
25
26/// used to created the interface layout before registering it
27pub trait AsInterface: Sized + Sync + Send {
28 /// used to created the interface layout before registering it
29 fn to_interface(self) -> Interface<Self>;
30}