use crate::RIType;
use core::cell::Cell;
pub trait FromFFIValue: Sized + RIType {
fn from_ffi_value(arg: Self::FFIType) -> Self::Inner;
}
pub trait IntoFFIValue: RIType {
type Destructor;
fn into_ffi_value(value: &mut Self::Inner) -> (Self::FFIType, Self::Destructor);
}
#[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"));
}
}