From 17b58e8edbe9d15be36c3481aaa953ac55327f6b Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Thu, 13 Nov 2025 20:21:07 -0800 Subject: [PATCH] add utility impls for SimValue> --- crates/fayalite/src/sim/value.rs | 72 +++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/crates/fayalite/src/sim/value.rs b/crates/fayalite/src/sim/value.rs index 89eb4e6..ff6660c 100644 --- a/crates/fayalite/src/sim/value.rs +++ b/crates/fayalite/src/sim/value.rs @@ -23,10 +23,10 @@ use bitvec::{slice::BitSlice, vec::BitVec}; use hashbrown::hash_map::Entry; use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Error as _, ser::Error as _}; use std::{ - borrow::Cow, + borrow::{Borrow, BorrowMut, Cow}, fmt::{self, Write}, hash::{BuildHasher, Hash, Hasher, RandomState}, - ops::{Deref, DerefMut}, + ops::{Deref, DerefMut, Index, IndexMut}, sync::{Arc, Mutex}, }; @@ -364,6 +364,74 @@ impl ToExpr for SimValue { } } +impl Index for SimValue> +where + [SimValue]: Index, +{ + type Output = <[SimValue] as Index>::Output; + + fn index(&self, index: I) -> &Self::Output { + (**self).borrow().index(index) + } +} + +impl IndexMut for SimValue> +where + [SimValue]: IndexMut, +{ + fn index_mut(&mut self, index: I) -> &mut Self::Output { + (**self).borrow_mut().index_mut(index) + } +} + +impl SimValue> { + pub fn iter(&self) -> std::slice::Iter<'_, SimValue> { + (**self).borrow().iter() + } + pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, SimValue> { + (**self).borrow_mut().iter_mut() + } +} + +impl IntoIterator for SimValue> { + type Item = SimValue; + type IntoIter = std::vec::IntoIter>; + + fn into_iter(self) -> Self::IntoIter { + Vec::into_iter(Self::into_value(self).into()) + } +} + +impl<'a, T: Type, Len: Size> IntoIterator for &'a SimValue> { + type Item = &'a SimValue; + type IntoIter = std::slice::Iter<'a, SimValue>; + + fn into_iter(self) -> Self::IntoIter { + self.iter() + } +} + +impl<'a, T: Type, Len: Size> IntoIterator for &'a mut SimValue> { + type Item = &'a mut SimValue; + type IntoIter = std::slice::IterMut<'a, SimValue>; + + fn into_iter(self) -> Self::IntoIter { + self.iter_mut() + } +} + +impl AsRef<[SimValue]> for SimValue> { + fn as_ref(&self) -> &[SimValue] { + (**self).as_ref() + } +} + +impl AsMut<[SimValue]> for SimValue> { + fn as_mut(&mut self) -> &mut [SimValue] { + (**self).as_mut() + } +} + pub trait SimValuePartialEq: Type { #[track_caller] fn sim_value_eq(this: &SimValue, other: &SimValue) -> bool;