use crate::schema::attr::Value;
use crate::AttrId;
use smallvec::SmallVec;
#[derive(Clone, Debug, PartialEq)]
pub struct AttrSet<'a> {
pub entries: SmallVec<[(AttrId, Value<'a>); 8]>,
}
impl<'a> AttrSet<'a> {
#[must_use]
pub fn new() -> Self {
Self {
entries: SmallVec::new(),
}
}
pub fn push(&mut self, id: AttrId, v: Value<'a>) {
self.entries.push((id, v));
}
#[must_use]
pub fn len(&self) -> usize {
self.entries.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
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()); }
}