From b39e1a10c731fd0e61a566a0668abc33ae821b49 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Wed, 18 Feb 2026 03:41:05 +0100 Subject: use fourcc as object tags (bad idea); store type info within objects --- common/object/src/value.rs | 78 +++++++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 21 deletions(-) (limited to 'common/object/src/value.rs') diff --git a/common/object/src/value.rs b/common/object/src/value.rs index 733e3a0..eb9de7c 100644 --- a/common/object/src/value.rs +++ b/common/object/src/value.rs @@ -17,8 +17,44 @@ pub trait Value<'a>: ValueStore + Sized { unimplemented!() } } + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum ValueType { + Binary, + String, + Object, + UInt, + Int, + Tag, + Float, + Bool, +} + +impl ValueType { + #[inline] + pub fn is_aligned(self) -> bool { + match self { + ValueType::Binary | ValueType::String => false, + _ => true, + } + } + pub fn from_num(n: u32) -> Self { + match n { + 0 => Self::Binary, + 1 => Self::String, + 2 => Self::Object, + 3 => Self::UInt, + 4 => Self::Int, + 5 => Self::Tag, + 6 => Self::Float, + 7 => Self::Bool, + _ => unreachable!(), + } + } +} + pub trait ValueStore { - fn is_aligned(&self) -> bool; + fn get_type(&self) -> ValueType; fn store_aligned(&self, _buf: &mut Vec) {} fn store_unaligned(&self, _buf: &mut Vec) {} fn size(&self) -> usize; @@ -30,8 +66,8 @@ impl<'a> Value<'a> for &'a str { } } impl ValueStore for &str { - fn is_aligned(&self) -> bool { - false + fn get_type(&self) -> ValueType { + ValueType::String } fn store_unaligned(&self, buf: &mut Vec) { buf.extend(self.as_bytes()); @@ -47,8 +83,8 @@ impl Value<'_> for u32 { } } impl ValueStore for u32 { - fn is_aligned(&self) -> bool { - true + fn get_type(&self) -> ValueType { + ValueType::UInt } fn store_aligned(&self, buf: &mut Vec) { buf.push(self.to_be()); @@ -64,8 +100,8 @@ impl Value<'_> for Tag { } } impl ValueStore for Tag { - fn is_aligned(&self) -> bool { - true + fn get_type(&self) -> ValueType { + ValueType::String } fn store_aligned(&self, buf: &mut Vec) { buf.push(self.0.to_be()); @@ -83,8 +119,8 @@ impl Value<'_> for u64 { } } impl ValueStore for u64 { - fn is_aligned(&self) -> bool { - true + fn get_type(&self) -> ValueType { + ValueType::UInt } fn store_aligned(&self, buf: &mut Vec) { buf.push(((self >> 32) as u32).to_be()); @@ -108,8 +144,8 @@ impl Value<'_> for f64 { } } impl ValueStore for f64 { - fn is_aligned(&self) -> bool { - true + fn get_type(&self) -> ValueType { + ValueType::Float } fn store_aligned(&self, buf: &mut Vec) { let b = self.to_be_bytes(); @@ -127,8 +163,8 @@ impl Value<'_> for i64 { } } impl ValueStore for i64 { - fn is_aligned(&self) -> bool { - true + fn get_type(&self) -> ValueType { + ValueType::Int } fn store_aligned(&self, buf: &mut Vec) { (*self as u64).store_aligned(buf); @@ -144,8 +180,8 @@ impl<'a> Value<'a> for Object<'a> { } } impl ValueStore for Object<'_> { - fn is_aligned(&self) -> bool { - true + fn get_type(&self) -> ValueType { + ValueType::Object } fn store_aligned(&self, buf: &mut Vec) { buf.push(self.tags.len() as u32); @@ -158,8 +194,8 @@ impl ValueStore for Object<'_> { } } impl ValueStore for ObjectBuffer { - fn is_aligned(&self) -> bool { - true + fn get_type(&self) -> ValueType { + ValueType::Object } fn store_aligned(&self, buf: &mut Vec) { buf.extend(&self.0); @@ -175,8 +211,8 @@ impl<'a> Value<'a> for &'a [u8] { } } impl ValueStore for &[u8] { - fn is_aligned(&self) -> bool { - false + fn get_type(&self) -> ValueType { + ValueType::Binary } fn store_unaligned(&self, buf: &mut Vec) { buf.extend(*self); @@ -192,8 +228,8 @@ impl<'a> Value<'a> for () { } } impl ValueStore for () { - fn is_aligned(&self) -> bool { - true + fn get_type(&self) -> ValueType { + ValueType::Binary } fn store_aligned(&self, _buf: &mut Vec) {} fn size(&self) -> usize { -- cgit v1.3