kitt_score 0.1.0

Decision engine at the core of Project KITT — in-memory stateful matching with pluggable scoring backends.
Documentation
//! An event's attribute bundle.
//!
//! Implemented as a `SmallVec<[(AttrId, Value); 8]>` — most events carry
//! fewer than 8 attributes, so no heap allocation occurs on the hot path.

use crate::schema::attr::Value;
use crate::AttrId;
use smallvec::SmallVec;

/// Borrowed attribute bundle carried by an event.
#[derive(Clone, Debug, PartialEq)]
pub struct AttrSet<'a> {
    /// Public to support zero-cost construction via a literal in benchmarks.
    pub entries: SmallVec<[(AttrId, Value<'a>); 8]>,
}

impl<'a> AttrSet<'a> {
    /// Creates an empty `AttrSet`.
    #[must_use]
    pub fn new() -> Self {
        Self {
            entries: SmallVec::new(),
        }
    }

    /// Appends an attribute entry.
    pub fn push(&mut self, id: AttrId, v: Value<'a>) {
        self.entries.push((id, v));
    }

    /// Returns the number of attribute entries.
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Returns `true` if there are no attribute entries.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Iterates over attribute entries.
    pub fn iter(&self) -> impl Iterator<Item = &(AttrId, Value<'a>)> {
        self.entries.iter()
    }
}

impl<'a> Default for AttrSet<'a> {
    fn default() -> Self {
        Self::new()
    }
}

impl<'a> FromIterator<(AttrId, Value<'a>)> for AttrSet<'a> {
    fn from_iter<I: IntoIterator<Item = (AttrId, Value<'a>)>>(iter: I) -> Self {
        Self {
            entries: iter.into_iter().collect(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::schema::attr::Value;

    #[test]
    fn attrset_holds_up_to_8_without_allocating() {
        let mut s = AttrSet::new();
        for i in 0..8_u16 {
            s.push(AttrId(i), Value::Int(i64::from(i)));
        }
        assert!(!s.entries.spilled()); // SmallVec API — confirms stack storage
    }
}