diff options
author | HampusM <hampus@hampusmat.com> | 2025-03-28 14:29:32 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2025-03-28 14:29:32 +0100 |
commit | c1de2d417389c1478bab4798b7cde6cbb08bba87 (patch) | |
tree | 8cb1b89212d0f1dfc0c6956a579bf2e8ae94a960 | |
parent | e66b34a0cd2de0c5918ee4bf5890feaaca2342fe (diff) |
feat: add immutable field slice iter
-rw-r--r-- | src/lib.rs | 66 |
1 files changed, 65 insertions, 1 deletions
@@ -245,7 +245,11 @@ impl MultiVec ) }; - FieldSlice { bytes, field_metadata } + FieldSlice { + bytes, + len: self.len(), + field_metadata, + } } /// Returns a slice containing the specified field of all items. @@ -485,6 +489,7 @@ impl Drop for MultiVec pub struct FieldSlice<'mv> { bytes: &'mv [MaybeUninit<u8>], + len: usize, field_metadata: &'mv FieldMetadata, } @@ -496,6 +501,65 @@ impl FieldSlice<'_> unsafe { self.bytes.cast::<Item>() } } + + pub fn iter(&self) -> FieldSliceIter<'_> + { + FieldSliceIter { + bytes: self.bytes, + index: 0, + len: self.len, + field_metadata: self.field_metadata, + } + } +} + +pub struct FieldSliceIter<'mv> +{ + bytes: &'mv [MaybeUninit<u8>], + index: usize, + len: usize, + field_metadata: &'mv FieldMetadata, +} + +impl<'mv> Iterator for FieldSliceIter<'mv> +{ + type Item = Field<'mv>; + + fn next(&mut self) -> Option<Self::Item> + { + let start_off = self.index * self.field_metadata.size; + + if self.index >= self.len { + return None; + } + + let field_bytes = self + .bytes + .get(start_off..start_off + self.field_metadata.size)?; + + self.index += 1; + + Some(Field { + bytes: field_bytes, + field_metadata: self.field_metadata, + }) + } +} + +pub struct Field<'mv> +{ + bytes: &'mv [MaybeUninit<u8>], + field_metadata: &'mv FieldMetadata, +} + +impl Field<'_> +{ + pub fn cast<T: 'static>(&mut self) -> &T + { + assert_eq!(TypeId::of::<T>(), self.field_metadata.type_id); + + unsafe { &*self.bytes.as_ptr().cast::<T>() } + } } pub struct FieldSliceMut<'mv> |