use crate::RIType;
use tetcore_std::cell::Cell;
pub trait FromFFIValue: Sized + RIType {
fn from_ffi_value(arg: Self::FFIType) -> Self;
}
pub trait IntoFFIValue: RIType {
type Owned;
fn into_ffi_value(&self) -> WrappedFFIValue<Self::FFIType, Self::Owned>;
}
pub enum WrappedFFIValue<T, O = ()> {
Wrapped(T),
WrappedAndOwned(T, O),
}
impl<T: Copy, O> WrappedFFIValue<T, O> {
pub fn get(&self) -> T {
match self {
Self::Wrapped(data) | Self::WrappedAndOwned(data, _) => *data,
}
}
}
impl<T, O> From<T> for WrappedFFIValue<T, O> {
fn from(val: T) -> Self {
WrappedFFIValue::Wrapped(val)
}
}
impl<T, O> From<(T, O)> for WrappedFFIValue<T, O> {
fn from(val: (T, O)) -> Self {
WrappedFFIValue::WrappedAndOwned(val.0, val.1)
}
}
#[derive(Clone, Copy)]
enum ExchangeableFunctionState {
Original,
Replaced,
}
pub struct ExchangeableFunction<T>(Cell<(T, ExchangeableFunctionState)>);
impl<T> ExchangeableFunction<T> {
pub const fn new(impl_: T) -> Self {
Self(Cell::new((impl_, ExchangeableFunctionState::Original)))
}
}
impl<T: Copy> ExchangeableFunction<T> {
pub fn replace_implementation(&'static self, new_impl: T) -> RestoreImplementation<T> {
if let ExchangeableFunctionState::Replaced = self.0.get().1 {
panic!("Trying to replace an already replaced implementation!")
}
let old = self.0.replace((new_impl, ExchangeableFunctionState::Replaced));
RestoreImplementation(self, Some(old.0))
}
fn restore_orig_implementation(&self, orig: T) {
self.0.set((orig, ExchangeableFunctionState::Original));
}
pub fn get(&self) -> T {
self.0.get().0
}
}
unsafe impl<T> Sync for ExchangeableFunction<T> {}
pub struct RestoreImplementation<T: 'static + Copy>(&'static ExchangeableFunction<T>, Option<T>);
impl<T: Copy> Drop for RestoreImplementation<T> {
fn drop(&mut self) {
self.0.restore_orig_implementation(self.1.take().expect("Value is only taken on drop; qed"));
}
}