use super::*;
impl SboStorage {
#[must_use]
#[inline(always)]
pub(crate) const fn uninit() -> Self {
Self {
words: [MaybeUninit::uninit(); 3],
}
}
#[must_use]
#[inline(always)]
pub(crate) const fn as_ptr(&self) -> *const u8 {
let Self { words } = self;
words.as_ptr().cast::<u8>()
}
#[must_use]
#[inline(always)]
pub(crate) const fn as_mut_ptr(&mut self) -> *mut u8 {
let Self { words } = self;
words.as_mut_ptr().cast::<u8>()
}
}
impl SignalVTable {
#[must_use]
#[inline(always)]
pub(crate) const fn of<T>() -> &'static Self
where
T: 'static,
{
const {
&Self {
type_id: sbo_type_id::<T>,
drop_fn: sbo_drop_value::<T>,
is_inline: is_sbo_inline::<T>(),
}
}
}
}
impl Default for SignalSlot {
#[inline(always)]
fn default() -> Self {
Self::empty()
}
}
impl Drop for SignalSlot {
#[inline]
fn drop(&mut self) {
self.clear();
}
}
impl SignalSlot {
#[must_use]
#[inline(always)]
pub(crate) const fn empty() -> Self {
Self {
storage: SboStorage::uninit(),
vtable: None,
marker: PhantomData,
}
}
#[must_use]
#[inline]
pub(crate) fn new<T>(value: T) -> Self
where
T: 'static,
{
let mut storage: SboStorage = SboStorage::uninit();
let vtable: &'static SignalVTable = SignalVTable::of::<T>();
if is_sbo_inline::<T>() {
let dst: *mut T = storage.as_mut_ptr().cast::<T>();
unsafe {
dst.write(value);
}
} else {
let boxed_ptr: *mut T = Box::into_raw(Box::new(value));
let dst: *mut *mut T = storage.as_mut_ptr().cast::<*mut T>();
unsafe {
dst.write(boxed_ptr);
}
}
Self {
storage,
vtable: Some(vtable),
marker: PhantomData,
}
}
#[must_use]
#[inline(always)]
pub(crate) fn is_some(&self) -> bool {
let Self { vtable, .. } = self;
vtable.is_some()
}
#[must_use]
#[inline(always)]
pub(crate) fn is_empty(&self) -> bool {
let Self { vtable, .. } = self;
vtable.is_none()
}
#[must_use]
#[inline(always)]
pub(crate) fn is_inline(&self) -> bool {
let Self { vtable, .. } = self;
vtable.is_some_and(|vt: &'static SignalVTable| vt.is_inline)
}
#[must_use]
#[inline(always)]
pub(crate) fn is<T>(&self) -> bool
where
T: 'static,
{
let Self { vtable, .. } = self;
vtable.is_some_and(|vt: &'static SignalVTable| (vt.type_id)() == TypeId::of::<T>())
}
#[must_use]
#[inline(always)]
pub(crate) fn downcast_ref<T>(&self) -> Option<&T>
where
T: 'static,
{
let Self {
storage, vtable, ..
} = self;
let vt: &'static SignalVTable = (*vtable)?;
if (vt.type_id)() != TypeId::of::<T>() {
return None;
}
if is_sbo_inline::<T>() {
let src: *const T = storage.as_ptr().cast::<T>();
Some(unsafe { &*src })
} else {
unsafe {
let boxed_ptr: *mut T = storage.as_ptr().cast::<*mut T>().read();
Some(&*boxed_ptr)
}
}
}
#[must_use]
#[inline(always)]
pub(crate) fn downcast_mut<T>(&mut self) -> Option<&mut T>
where
T: 'static,
{
let Self {
storage, vtable, ..
} = self;
let vt: &'static SignalVTable = (*vtable)?;
if (vt.type_id)() != TypeId::of::<T>() {
return None;
}
if is_sbo_inline::<T>() {
let src: *mut T = storage.as_mut_ptr().cast::<T>();
Some(unsafe { &mut *src })
} else {
unsafe {
let boxed_ptr: *mut T = storage.as_mut_ptr().cast::<*mut T>().read();
Some(&mut *boxed_ptr)
}
}
}
#[inline]
pub(crate) fn replace<T>(&mut self, value: T)
where
T: 'static,
{
if let Some(existing) = Self::downcast_mut::<T>(self) {
*existing = value;
} else {
*self = Self::new(value);
}
}
#[must_use]
#[inline(always)]
pub(crate) fn take(&mut self) -> Self {
take(self)
}
#[inline]
pub(crate) fn clear(&mut self) {
let Self {
storage, vtable, ..
} = self;
if let Some(vt) = vtable.take() {
unsafe {
(vt.drop_fn)(storage);
}
}
}
}