icydb-core 0.145.7

IcyDB — A schema-first typed query engine and persistence runtime for Internet Computer canisters
Documentation
//! Module: executor::util
//! Responsibility: tiny helpers shared by executor runtime and executor-local tests.
//! Does not own: execution semantics, routing, or plan validation.

/// Apply one offset/limit window to an already ordered in-memory row set.
///
/// This helper owns only the vector slicing mechanics. Callers remain
/// responsible for deciding whether paging, projection, or delete semantics
/// allow this window to run at their phase boundary.
pub(in crate::db::executor) fn apply_offset_limit_window<T>(
    rows: &mut Vec<T>,
    offset: u32,
    limit: Option<u32>,
) {
    let offset = usize::min(rows.len(), usize::try_from(offset).unwrap_or(usize::MAX));
    if offset > 0 {
        rows.drain(..offset);
    }

    if let Some(limit) = limit {
        let limit = usize::min(rows.len(), usize::try_from(limit).unwrap_or(usize::MAX));
        rows.truncate(limit);
    }
}

/// Convert one row-count length into `u32` using saturating semantics.
#[must_use]
pub(in crate::db::executor) fn saturating_u32_len(row_len: usize) -> u32 {
    u32::try_from(row_len).unwrap_or(u32::MAX)
}

/// Convert one byte-length value into `u64` using saturating semantics.
///
/// This helper exists to keep numeric-clamp behavior consistent between runtime
/// terminal folds and executor-owned expected-value helpers in tests.
#[must_use]
#[expect(clippy::cast_possible_truncation)]
pub(in crate::db::executor) const fn saturating_row_len(row_len: usize) -> u64 {
    if row_len > u64::MAX as usize {
        u64::MAX
    } else {
        row_len as u64
    }
}

///
/// TESTS
///

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn saturating_row_len_returns_exact_value_within_u64_range() {
        assert_eq!(saturating_row_len(42), 42);
    }

    #[test]
    fn saturating_row_len_saturates_at_u64_max() {
        assert_eq!(saturating_row_len(usize::MAX), u64::MAX);
    }
}