euv-core 0.57.0

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
#[cfg(feature = "devtools")]
use super::*;

/// Zero-initialized empty breadcrumb slot helpers.
#[cfg(feature = "devtools")]
impl InlineBreadcrumbSlot {
    /// Returns a zeroed empty breadcrumb slot.
    ///
    /// # Returns
    ///
    /// - `Self` - Empty inline breadcrumb slot.
    #[must_use]
    pub(crate) const fn empty() -> Self {
        Self {
            seq: 0,
            frame_seq: 0,
            kind_len: 0,
            kind_buf: [0; PANIC_MAX_KIND_BYTES],
            detail_len: 0,
            detail_buf: [0; PANIC_MAX_DETAIL_BYTES],
        }
    }

    /// Writes `kind` and `detail` into this slot in-place without any heap allocation.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable breadcrumb slot.
    /// - `u64` - Monotonic breadcrumb sequence number.
    /// - `u64` - Active frame index.
    /// - `&str` - Event kind label.
    /// - `&str` - Event detail label.
    pub(crate) fn write_in_place(&mut self, seq: u64, frame_seq: u64, kind: &str, detail: &str) {
        let Self {
            seq: slot_seq,
            frame_seq: slot_frame,
            kind_len,
            kind_buf,
            detail_len,
            detail_buf,
        } = self;
        *slot_seq = seq;
        *slot_frame = frame_seq;
        *kind_len = copy_utf8_truncated(kind, kind_buf) as u8;
        *detail_len = copy_utf8_truncated(detail, detail_buf) as u8;
    }

    /// Materializes an owned `RuntimeEventBreadcrumb` from this inline slot.
    ///
    /// # Arguments
    ///
    /// - `Self` - Inline breadcrumb slot copied by value.
    ///
    /// # Returns
    ///
    /// - `RuntimeEventBreadcrumb` - Owned serializable breadcrumb entry.
    #[must_use]
    pub(crate) fn to_owned_breadcrumb(self) -> RuntimeEventBreadcrumb {
        let Self {
            seq,
            frame_seq,
            kind_len,
            kind_buf,
            detail_len,
            detail_buf,
        } = self;
        let kind_str: String =
            from_utf8(&kind_buf[..(kind_len as usize).min(PANIC_MAX_KIND_BYTES)])
                .unwrap_or("")
                .to_string();
        let detail_str: String =
            from_utf8(&detail_buf[..(detail_len as usize).min(PANIC_MAX_DETAIL_BYTES)])
                .unwrap_or("")
                .to_string();
        RuntimeEventBreadcrumb::new(seq, kind_str, detail_str, frame_seq)
    }
}

/// Zero-allocation runtime context and ring buffer operations.
#[cfg(feature = "devtools")]
impl InlineRuntimeContextState {
    /// Creates an initialized runtime context slot with canonical default route and theme labels.
    ///
    /// # Returns
    ///
    /// - `Self` - Fresh inline runtime context state.
    #[must_use]
    pub(crate) fn new_default() -> Self {
        let mut route_buf: [u8; PANIC_MAX_ROUTE_BYTES] = [0; PANIC_MAX_ROUTE_BYTES];
        let route_len: usize = copy_utf8_truncated(PANIC_DEFAULT_ROUTE, &mut route_buf);
        let mut theme_buf: [u8; PANIC_MAX_THEME_BYTES] = [0; PANIC_MAX_THEME_BYTES];
        let theme_len: usize = copy_utf8_truncated(PANIC_DEFAULT_THEME, &mut theme_buf);
        Self {
            route_len,
            route_buf,
            theme_len,
            theme_buf,
            overlays_len: 0,
            overlays_buf: [0; PANIC_MAX_OVERLAYS_BYTES],
            viewport_width: 0.0,
            viewport_height: 0.0,
            scale_factor: 1.0,
            frame_seq: 0,
            is_idle: false,
            next_seq: 1,
            write_cursor: 0,
            count: 0,
            breadcrumbs: [InlineBreadcrumbSlot::empty(); PANIC_BREADCRUMB_RING_CAPACITY],
        }
    }

    /// Appends one breadcrumb into the fixed-capacity ring buffer with zero heap allocation.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable runtime context state.
    /// - `&str` - Event kind label.
    /// - `&str` - Event detail label.
    pub(crate) fn push_breadcrumb(&mut self, kind: &str, detail: &str) {
        let Self {
            frame_seq,
            next_seq,
            write_cursor,
            count,
            breadcrumbs,
            ..
        } = self;
        let seq: u64 = *next_seq;
        *next_seq = seq.saturating_add(1);
        let idx: usize = *write_cursor;
        breadcrumbs[idx].write_in_place(seq, *frame_seq, kind, detail);
        *write_cursor = (idx + 1) % PANIC_BREADCRUMB_RING_CAPACITY;
        if *count < PANIC_BREADCRUMB_RING_CAPACITY {
            *count += 1;
        }
    }

    /// Exports recent breadcrumbs in strict chronological order (oldest to newest).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Borrowed runtime context state.
    ///
    /// # Returns
    ///
    /// - `Vec<RuntimeEventBreadcrumb>` - Chronological owned breadcrumb entries.
    #[must_use]
    pub(crate) fn snapshot_breadcrumbs(&self) -> Vec<RuntimeEventBreadcrumb> {
        let Self {
            write_cursor,
            count,
            breadcrumbs,
            ..
        } = self;
        let total: usize = *count;
        let mut out: Vec<RuntimeEventBreadcrumb> = Vec::with_capacity(total);
        let start: usize = if total == PANIC_BREADCRUMB_RING_CAPACITY {
            *write_cursor
        } else {
            0
        };
        for offset in 0..total {
            let slot_idx: usize = (start + offset) % PANIC_BREADCRUMB_RING_CAPACITY;
            out.push(breadcrumbs[slot_idx].to_owned_breadcrumb());
        }
        out
    }

    /// Materializes a complete owned `PanicRuntimeContext` snapshot from this inline state.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Borrowed runtime context state.
    ///
    /// # Returns
    ///
    /// - `PanicRuntimeContext` - Complete serializable runtime context snapshot.
    #[must_use]
    pub(crate) fn to_owned_context(&self) -> PanicRuntimeContext {
        let Self {
            route_len,
            route_buf,
            theme_len,
            theme_buf,
            overlays_len,
            overlays_buf,
            viewport_width,
            viewport_height,
            scale_factor,
            frame_seq,
            is_idle,
            ..
        } = self;
        let active_route: String = from_utf8(&route_buf[..(*route_len).min(PANIC_MAX_ROUTE_BYTES)])
            .unwrap_or(PANIC_DEFAULT_ROUTE)
            .to_string();
        let theme: String = from_utf8(&theme_buf[..(*theme_len).min(PANIC_MAX_THEME_BYTES)])
            .unwrap_or(PANIC_DEFAULT_THEME)
            .to_string();
        let overlays_raw: &str =
            from_utf8(&overlays_buf[..(*overlays_len).min(PANIC_MAX_OVERLAYS_BYTES)]).unwrap_or("");
        let active_overlays: Vec<String> = overlays_raw
            .split(PANIC_OVERLAY_SEPARATOR)
            .map(str::trim)
            .filter(|item: &&str| !item.is_empty())
            .map(str::to_string)
            .collect();
        let recent_breadcrumbs: Vec<RuntimeEventBreadcrumb> = self.snapshot_breadcrumbs();
        PanicRuntimeContext {
            active_route,
            viewport_width: *viewport_width,
            viewport_height: *viewport_height,
            scale_factor: *scale_factor,
            theme,
            active_overlays,
            frame_seq: *frame_seq,
            is_idle: *is_idle,
            recent_breadcrumbs,
        }
    }
}