aboutsummaryrefslogtreecommitdiff
path: root/common/object/src/value.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2026-02-18 03:41:05 +0100
committermetamuffin <metamuffin@disroot.org>2026-02-18 03:41:05 +0100
commitb39e1a10c731fd0e61a566a0668abc33ae821b49 (patch)
tree6e2e48b5e56a2cf4b4f966b6f2e014c446a242a1 /common/object/src/value.rs
parent95606a9deed45ae285c2d4dee01de9d21a43b044 (diff)
downloadjellything-b39e1a10c731fd0e61a566a0668abc33ae821b49.tar
jellything-b39e1a10c731fd0e61a566a0668abc33ae821b49.tar.bz2
jellything-b39e1a10c731fd0e61a566a0668abc33ae821b49.tar.zst
use fourcc as object tags (bad idea); store type info within objects
Diffstat (limited to 'common/object/src/value.rs')
-rw-r--r--common/object/src/value.rs78
1 files changed, 57 insertions, 21 deletions
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<u32>) {}
fn store_unaligned(&self, _buf: &mut Vec<u8>) {}
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<u8>) {
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<u32>) {
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<u32>) {
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<u32>) {
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<u32>) {
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<u32>) {
(*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<u32>) {
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<u32>) {
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<u8>) {
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<u32>) {}
fn size(&self) -> usize {