From 0a05701441c84773849e74cec168cb8852d58218 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 30 Mar 2025 22:42:04 +0200 Subject: refactor: make remove & swap_remove return new FieldOwned struct --- src/lib.rs | 88 ++++++++++++++++++++++++++++++------------------------------- src/util.rs | 18 ------------- 2 files changed, 44 insertions(+), 62 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 3bbeb19..dbce335 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ use std::cmp::max; use std::mem::MaybeUninit; use std::ptr::NonNull; -use crate::util::{AnonUnique, Either, MaybeUninitByteSlice}; +use crate::util::{AnonUnique, MaybeUninitByteSlice}; mod util; @@ -197,7 +197,7 @@ impl MultiVec self.length = 1; } - pub fn remove(&mut self, item_index: usize) -> Option>> + pub fn remove(&mut self, item_index: usize) -> Option>> { if item_index >= self.length { return None; @@ -207,7 +207,7 @@ impl MultiVec self.length -= 1; - let mut item_fields = Vec::>::with_capacity(self.field_cnt()); + let mut item_fields = Vec::>::with_capacity(self.field_cnt()); for field_index in 0..self.field_cnt() { let field_metadata = &self.get_field_metadata()?.get(field_index)?; @@ -236,13 +236,11 @@ impl MultiVec field_slice .get_item(item_index) .expect("Not possible") - .bytes - .as_a() - .expect("Not possible"), + .bytes, ); - let item_field = Field { - bytes: Either::B(item_field_bytes), + let item_field = FieldOwned { + bytes: item_field_bytes, field_metadata, }; @@ -261,7 +259,7 @@ impl MultiVec Some(item_fields) } - pub fn swap_remove(&mut self, item_index: usize) -> Option>> + pub fn swap_remove(&mut self, item_index: usize) -> Option>> { if item_index >= self.length { return None; @@ -271,7 +269,7 @@ impl MultiVec self.length -= 1; - let mut item_fields = Vec::>::with_capacity(self.field_cnt()); + let mut item_fields = Vec::>::with_capacity(self.field_cnt()); for field_index in 0..self.field_cnt() { let field_metadata = &self.get_field_metadata()?.get(field_index)?; @@ -300,13 +298,11 @@ impl MultiVec field_slice .get_item(item_index) .expect("Not possible") - .bytes - .as_a() - .expect("Not possible"), + .bytes, ); - let item_field = Field { - bytes: Either::B(item_field_bytes), + let item_field = FieldOwned { + bytes: item_field_bytes, field_metadata, }; @@ -757,7 +753,7 @@ pub struct FieldSliceIter<'mv> impl<'mv> Iterator for FieldSliceIter<'mv> { - type Item = Field<'mv, 'mv>; + type Item = Field<'mv>; fn next(&mut self) -> Option { @@ -774,44 +770,48 @@ impl<'mv> Iterator for FieldSliceIter<'mv> self.index += 1; Some(Field { - bytes: Either::A(field_bytes), + bytes: field_bytes, field_metadata: self.field_metadata, }) } } #[derive(Debug)] -pub struct Field<'bytes, 'mv> +pub struct Field<'mv> { - bytes: Either<&'bytes [MaybeUninit], AnonUnique>, + bytes: &'mv [MaybeUninit], field_metadata: &'mv FieldMetadata, } -impl<'bytes, 'mv> Field<'bytes, 'mv> +impl<'mv> Field<'mv> { pub fn cast(&self) -> &T { assert_eq!(TypeId::of::(), self.field_metadata.type_id); - // println!("Piss: {:?}", unsafe { self.bytes.as_ref().cast::() }); + let ptr: *const MaybeUninit = self.bytes.as_ptr(); - let bytes = match &self.bytes { - Either::A(bytes) => bytes, - Either::B(bytes) => bytes.as_slice(), - }; + unsafe { &*ptr.cast::() } + } +} - let ptr: *const MaybeUninit = bytes.as_ptr(); +#[derive(Debug)] +pub struct FieldOwned<'mv> +{ + bytes: AnonUnique, + field_metadata: &'mv FieldMetadata, +} + +impl FieldOwned<'_> +{ + pub fn as_ref(&self) -> &T + { + assert_eq!(TypeId::of::(), self.field_metadata.type_id); + + let ptr: *const MaybeUninit = self.bytes.as_slice().as_ptr(); unsafe { &*ptr.cast::() } } - - // pub unsafe fn into_with_owned_bytes(self) -> Field<'static, 'mv> - // { - // Field { - // bytes: self.bytes.into_owned().into(), - // field_metadata: self.field_metadata, - // } - // } } #[derive(Debug)] @@ -824,7 +824,7 @@ pub struct FieldSliceMut<'mv> impl<'mv> FieldSliceMut<'mv> { - pub fn get_item<'this>(&'this self, item_index: usize) -> Option> + pub fn get_item<'this>(&'this self, item_index: usize) -> Option> where 'this: 'mv, { @@ -839,7 +839,7 @@ impl<'mv> FieldSliceMut<'mv> .get(start_off..start_off + self.field_metadata.size)?; Some(Field { - bytes: Either::A(field_bytes), + bytes: field_bytes, field_metadata: self.field_metadata, }) } @@ -1501,8 +1501,8 @@ mod tests assert_eq!(removed.len(), 2); - assert_eq!(*removed[0].cast::(), 901); - assert_eq!(*removed[1].cast::(), 7120); + assert_eq!(*removed[0].as_ref::(), 901); + assert_eq!(*removed[1].as_ref::(), 7120); } #[test] @@ -1525,8 +1525,8 @@ mod tests assert_eq!(removed.len(), 2); - assert_eq!(*removed[0].cast::(), 123123); - assert_eq!(*removed[1].cast::(), 9009); + assert_eq!(*removed[0].as_ref::(), 123123); + assert_eq!(*removed[1].as_ref::(), 9009); } #[test] @@ -1549,8 +1549,8 @@ mod tests assert_eq!(removed.len(), 2); - assert_eq!(*removed[0].cast::(), 901); - assert_eq!(*removed[1].cast::(), 7120); + assert_eq!(*removed[0].as_ref::(), 901); + assert_eq!(*removed[1].as_ref::(), 7120); } #[test] @@ -1573,8 +1573,8 @@ mod tests assert_eq!(removed.len(), 2); - assert_eq!(*removed[0].cast::(), 123123); - assert_eq!(*removed[1].cast::(), 9009); + assert_eq!(*removed[0].as_ref::(), 123123); + assert_eq!(*removed[1].as_ref::(), 9009); } #[test] diff --git a/src/util.rs b/src/util.rs index 8ff79c7..242df23 100644 --- a/src/util.rs +++ b/src/util.rs @@ -78,21 +78,3 @@ impl Drop for AnonUnique } } } - -#[derive(Debug)] -pub enum Either -{ - A(A), - B(B), -} - -impl Either -{ - pub fn as_a(&self) -> Option<&A> - { - match self { - Self::A(a) => Some(a), - Self::B(_) => None, - } - } -} -- cgit v1.2.3-18-g5258