use std::{cell::Cell, os::raw::c_void};
use crate::memory::context::stack::Stack;
pub struct LocalSlotRef<'target> {
slot: &'target Cell<*mut c_void>,
}
impl<'target> LocalSlotRef<'target> {
#[inline]
pub(crate) fn new(slot: &'target Cell<*mut c_void>) -> Self {
LocalSlotRef { slot }
}
}
pub struct StackSlotRef<'target> {
stack: &'target Stack,
offset: usize,
}
impl<'target> StackSlotRef<'target> {
#[inline]
pub(crate) fn new(stack: &'target Stack, offset: usize) -> Self {
StackSlotRef { stack, offset }
}
}
pub unsafe trait SlotRef: private::SlotRefPriv {}
unsafe impl<'target> SlotRef for LocalSlotRef<'target> {}
unsafe impl<'target> SlotRef for StackSlotRef<'target> {}
mod private {
use std::{os::raw::c_void, ptr::NonNull};
use super::{LocalSlotRef, StackSlotRef};
use crate::private::Private;
pub unsafe trait SlotRefPriv {
unsafe fn set(&self, data: NonNull<c_void>, _: Private);
}
unsafe impl<'target> SlotRefPriv for StackSlotRef<'target> {
#[inline]
unsafe fn set(&self, data: NonNull<c_void>, _: Private) {
unsafe {
self.stack.set_root(self.offset, data.cast());
}
}
}
unsafe impl<'target> SlotRefPriv for LocalSlotRef<'target> {
#[inline]
unsafe fn set(&self, data: NonNull<c_void>, _: Private) {
self.slot.set(data.as_ptr().cast());
}
}
}