euv-core 0.48.1

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Raw pointer helpers for 24-byte aligned `SboStorage`.
impl SboStorage {
    /// Creates uninitialized 24-byte aligned storage without touching memory.
    ///
    /// # Returns
    ///
    /// - `Self` - Uninitialized aligned storage buffer.
    #[must_use]
    #[inline(always)]
    pub(crate) const fn uninit() -> Self {
        Self {
            words: [MaybeUninit::uninit(); 3],
        }
    }

    /// Returns a raw const pointer to the start of the aligned buffer.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Storage buffer reference.
    ///
    /// # Returns
    ///
    /// - `*const u8` - 8-byte-aligned const byte pointer.
    #[must_use]
    #[inline(always)]
    pub(crate) const fn as_ptr(&self) -> *const u8 {
        let Self { words } = self;
        words.as_ptr().cast::<u8>()
    }

    /// Returns a raw mutable pointer to the start of the aligned buffer.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable storage buffer reference.
    ///
    /// # Returns
    ///
    /// - `*mut u8` - 8-byte-aligned mutable byte pointer.
    #[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>()
    }
}

/// Static vtable lookup for `SignalVTable`.
impl SignalVTable {
    /// Returns a `'static` vtable reference for concrete type `T`.
    ///
    /// # Returns
    ///
    /// - `&'static Self` - Static type-erased vtable for `T`.
    #[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>(),
            }
        }
    }
}

/// Default empty initialization for `SignalSlot`.
impl Default for SignalSlot {
    /// Creates an empty `SignalSlot` holding no value.
    ///
    /// # Returns
    ///
    /// - `Self` - Empty slot.
    #[inline(always)]
    fn default() -> Self {
        Self::empty()
    }
}

/// Panic-safe drop implementation releasing any inline or heap-spilled payload stored in `SignalSlot`.
impl Drop for SignalSlot {
    /// Drops the active value if present, ensuring no double-drop even on unwind.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable slot being dropped.
    #[inline]
    fn drop(&mut self) {
        self.clear();
    }
}

/// Core SBO storage, typed access, and mutation operations for `SignalSlot`.
impl SignalSlot {
    /// Creates an empty `SignalSlot` without allocating or zero-initializing storage.
    ///
    /// # Returns
    ///
    /// - `Self` - Empty slot with `vtable == None`.
    #[must_use]
    #[inline(always)]
    pub(crate) const fn empty() -> Self {
        Self {
            storage: SboStorage::uninit(),
            vtable: None,
            marker: PhantomData,
        }
    }

    /// Creates a `SignalSlot` storing `value` inline when `size_of::<T>() <= 24 && align_of::<T>() <= 8`, or spilled to `Box` otherwise.
    ///
    /// # Arguments
    ///
    /// - `T` - Value to store.
    ///
    /// # Returns
    ///
    /// - `Self` - Populated SBO slot owning `value`.
    #[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>();
            // SAFETY: `is_sbo_inline::<T>()` guarantees `size_of::<T>() <= 24` and `align_of::<T>() <= 8`.
            // `storage` is 24 bytes aligned to 8 and uninitialized, so writing `value` is sound.
            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>();
            // SAFETY: `*mut T` has size `<= 8 <= 24` and alignment `<= 8`. `storage` is 24 bytes aligned to 8.
            unsafe {
                dst.write(boxed_ptr);
            }
        }
        Self {
            storage,
            vtable: Some(vtable),
            marker: PhantomData,
        }
    }

    /// Returns `true` when this slot currently holds a live value.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Slot reference.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when populated.
    #[must_use]
    #[inline(always)]
    pub(crate) fn is_some(&self) -> bool {
        let Self { vtable, .. } = self;
        vtable.is_some()
    }

    /// Returns `true` when this slot holds no value.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Slot reference.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when empty.
    #[must_use]
    #[inline(always)]
    pub(crate) fn is_empty(&self) -> bool {
        let Self { vtable, .. } = self;
        vtable.is_none()
    }

    /// Returns `true` when this slot holds a live value stored inline without heap allocation.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Slot reference.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when populated and stored inline.
    #[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)
    }

    /// Returns `true` when this slot holds a live value of concrete type `T`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Slot reference.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when the active value has `TypeId::of::<T>()`.
    #[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>())
    }

    /// Returns a shared reference `&T` if this slot holds a live value of concrete type `T`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Slot reference.
    ///
    /// # Returns
    ///
    /// - `Option<&T>` - Borrowed value reference when live and matching `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>();
            // SAFETY: `vt.type_id` matched `T` and `is_sbo_inline::<T>()` guarantees `storage` holds an initialized inline `T`.
            Some(unsafe { &*src })
        } else {
            // SAFETY: `vt.type_id` matched `T` and `!is_sbo_inline::<T>()` guarantees `storage` holds a valid `Box::into_raw` `*mut T`.
            unsafe {
                let boxed_ptr: *mut T = storage.as_ptr().cast::<*mut T>().read();
                Some(&*boxed_ptr)
            }
        }
    }

    /// Returns a mutable reference `&mut T` if this slot holds a live value of concrete type `T`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable slot reference.
    ///
    /// # Returns
    ///
    /// - `Option<&mut T>` - Exclusive mutable reference when live and matching `T`.
    #[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>();
            // SAFETY: `vt.type_id` matched `T`, `&mut self` grants exclusive access, and `storage` holds an initialized inline `T`.
            Some(unsafe { &mut *src })
        } else {
            // SAFETY: `vt.type_id` matched `T`, `&mut self` grants exclusive access, and `storage` holds a valid `Box::into_raw` `*mut T`.
            unsafe {
                let boxed_ptr: *mut T = storage.as_mut_ptr().cast::<*mut T>().read();
                Some(&mut *boxed_ptr)
            }
        }
    }

    /// Writes `value` into this slot, reusing existing inline or heap-spilled storage in place when types match.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable slot reference.
    /// - `T` - Replacement value.
    #[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);
        }
    }

    /// Moves the active payload slot out, leaving `Self::empty()` behind with zero heap allocations.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable slot reference.
    ///
    /// # Returns
    ///
    /// - `Self` - Previously stored slot contents.
    #[must_use]
    #[inline(always)]
    pub(crate) fn take(&mut self) -> Self {
        take(self)
    }

    /// Drops any stored value and resets this slot to empty safely even if `T::drop` panics.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable slot reference.
    #[inline]
    pub(crate) fn clear(&mut self) {
        let Self {
            storage, vtable, ..
        } = self;
        if let Some(vt) = vtable.take() {
            // SAFETY: `vtable` was cleared to `None` BEFORE invoking `drop_fn`, guaranteeing
            // that `Drop` cannot double-drop `storage` even if `T::drop` unwinds.
            unsafe {
                (vt.drop_fn)(storage);
            }
        }
    }
}