summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2025-03-30 22:42:04 +0200
committerHampusM <hampus@hampusmat.com>2025-03-30 22:42:04 +0200
commit0a05701441c84773849e74cec168cb8852d58218 (patch)
treefd99b78e58c7b9f89100b645ea727b2eff61c688
parent2a30a0456c2d1898cc65908bd43a7f7b15abc7db (diff)
refactor: make remove & swap_remove return new FieldOwned struct
-rw-r--r--src/lib.rs88
-rw-r--r--src/util.rs18
2 files changed, 44 insertions, 62 deletions
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<Vec<Field<'static, '_>>>
+ pub fn remove(&mut self, item_index: usize) -> Option<Vec<FieldOwned<'_>>>
{
if item_index >= self.length {
return None;
@@ -207,7 +207,7 @@ impl MultiVec
self.length -= 1;
- let mut item_fields = Vec::<Field<'static, '_>>::with_capacity(self.field_cnt());
+ let mut item_fields = Vec::<FieldOwned<'_>>::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<Vec<Field<'static, '_>>>
+ pub fn swap_remove(&mut self, item_index: usize) -> Option<Vec<FieldOwned<'_>>>
{
if item_index >= self.length {
return None;
@@ -271,7 +269,7 @@ impl MultiVec
self.length -= 1;
- let mut item_fields = Vec::<Field<'static, '_>>::with_capacity(self.field_cnt());
+ let mut item_fields = Vec::<FieldOwned<'_>>::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<Self::Item>
{
@@ -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<u8>], AnonUnique>,
+ bytes: &'mv [MaybeUninit<u8>],
field_metadata: &'mv FieldMetadata,
}
-impl<'bytes, 'mv> Field<'bytes, 'mv>
+impl<'mv> Field<'mv>
{
pub fn cast<T: 'static>(&self) -> &T
{
assert_eq!(TypeId::of::<T>(), self.field_metadata.type_id);
- // println!("Piss: {:?}", unsafe { self.bytes.as_ref().cast::<u8>() });
+ let ptr: *const MaybeUninit<u8> = self.bytes.as_ptr();
- let bytes = match &self.bytes {
- Either::A(bytes) => bytes,
- Either::B(bytes) => bytes.as_slice(),
- };
+ unsafe { &*ptr.cast::<T>() }
+ }
+}
- let ptr: *const MaybeUninit<u8> = bytes.as_ptr();
+#[derive(Debug)]
+pub struct FieldOwned<'mv>
+{
+ bytes: AnonUnique,
+ field_metadata: &'mv FieldMetadata,
+}
+
+impl FieldOwned<'_>
+{
+ pub fn as_ref<T: 'static>(&self) -> &T
+ {
+ assert_eq!(TypeId::of::<T>(), self.field_metadata.type_id);
+
+ let ptr: *const MaybeUninit<u8> = self.bytes.as_slice().as_ptr();
unsafe { &*ptr.cast::<T>() }
}
-
- // 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<Field<'mv, 'mv>>
+ pub fn get_item<'this>(&'this self, item_index: usize) -> Option<Field<'mv>>
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::<u32>(), 901);
- assert_eq!(*removed[1].cast::<u16>(), 7120);
+ assert_eq!(*removed[0].as_ref::<u32>(), 901);
+ assert_eq!(*removed[1].as_ref::<u16>(), 7120);
}
#[test]
@@ -1525,8 +1525,8 @@ mod tests
assert_eq!(removed.len(), 2);
- assert_eq!(*removed[0].cast::<u32>(), 123123);
- assert_eq!(*removed[1].cast::<u16>(), 9009);
+ assert_eq!(*removed[0].as_ref::<u32>(), 123123);
+ assert_eq!(*removed[1].as_ref::<u16>(), 9009);
}
#[test]
@@ -1549,8 +1549,8 @@ mod tests
assert_eq!(removed.len(), 2);
- assert_eq!(*removed[0].cast::<u32>(), 901);
- assert_eq!(*removed[1].cast::<u16>(), 7120);
+ assert_eq!(*removed[0].as_ref::<u32>(), 901);
+ assert_eq!(*removed[1].as_ref::<u16>(), 7120);
}
#[test]
@@ -1573,8 +1573,8 @@ mod tests
assert_eq!(removed.len(), 2);
- assert_eq!(*removed[0].cast::<u32>(), 123123);
- assert_eq!(*removed[1].cast::<u16>(), 9009);
+ assert_eq!(*removed[0].as_ref::<u32>(), 123123);
+ assert_eq!(*removed[1].as_ref::<u16>(), 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, B>
-{
- A(A),
- B(B),
-}
-
-impl<A, B> Either<A, B>
-{
- pub fn as_a(&self) -> Option<&A>
- {
- match self {
- Self::A(a) => Some(a),
- Self::B(_) => None,
- }
- }
-}