diff options
Diffstat (limited to 'common/object/src/value.rs')
| -rw-r--r-- | common/object/src/value.rs | 139 |
1 files changed, 57 insertions, 82 deletions
diff --git a/common/object/src/value.rs b/common/object/src/value.rs index 9b9a438..0a1ceb9 100644 --- a/common/object/src/value.rs +++ b/common/object/src/value.rs @@ -4,19 +4,58 @@ Copyright (C) 2026 metamuffin <metamuffin.org> */ -use crate::{Object, ObjectBuffer, Tag}; +use crate::{Object, Tag, slice_to_ob}; +use core::{marker::Sized, unimplemented}; use std::{borrow::Cow, fmt::Display, str::FromStr}; -pub trait ValueLoad<'a>: ValueStore + Sized { +pub trait ValueMarker<'a> { + type Inner: ValueSer<'a>; +} + +impl<'a> ValueMarker<'a> for str { + type Inner = &'a str; +} +impl<'a> ValueMarker<'a> for Object { + type Inner = &'a Object; +} +impl<'a> ValueMarker<'a> for [u8] { + type Inner = &'a [u8]; +} + +impl<'a> ValueMarker<'a> for Tag { + type Inner = Tag; +} +impl<'a> ValueMarker<'a> for u32 { + type Inner = u32; +} +impl<'a> ValueMarker<'a> for i64 { + type Inner = i64; +} +impl<'a> ValueMarker<'a> for u64 { + type Inner = u64; +} +impl<'a> ValueMarker<'a> for () { + type Inner = (); +} +impl<'a> ValueMarker<'a> for f64 { + type Inner = f64; +} + +pub trait ValueSer<'a>: Sized { const TYPE: ValueType; - fn load_aligned(buf: &'a [u32]) -> Option<Self> { - let _ = buf; + fn load_aligned(_buf: &'a [u32]) -> Option<Self> { unimplemented!() } - fn load_unaligned(buf: &'a [u8]) -> Option<Self> { - let _ = buf; + fn load_unaligned(_buf: &'a [u8]) -> Option<Self> { + unimplemented!() + } + fn store_aligned(&self, _buf: &mut Vec<u32>) { unimplemented!() } + fn store_unaligned(&self, _buf: &mut Vec<u8>) { + unimplemented!() + } + fn size(&self) -> usize; } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -126,22 +165,11 @@ impl FromStr for Value<'static> { } } -pub trait ValueStore { - 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; -} -impl<'a> ValueLoad<'a> for &'a str { +impl<'a> ValueSer<'a> for &'a str { const TYPE: ValueType = ValueType::String; fn load_unaligned(buf: &'a [u8]) -> Option<Self> { str::from_utf8(buf).ok() } -} -impl ValueStore for &str { - fn get_type(&self) -> ValueType { - ValueType::String - } fn store_unaligned(&self, buf: &mut Vec<u8>) { buf.extend(self.as_bytes()); } @@ -149,16 +177,11 @@ impl ValueStore for &str { self.len() } } -impl ValueLoad<'_> for u32 { +impl ValueSer<'_> for u32 { const TYPE: ValueType = ValueType::UInt; fn load_aligned(buf: &[u32]) -> Option<Self> { buf.get(0).copied().map(u32::from_be) } -} -impl ValueStore for u32 { - fn get_type(&self) -> ValueType { - ValueType::UInt - } fn store_aligned(&self, buf: &mut Vec<u32>) { buf.push(self.to_be()); } @@ -166,16 +189,11 @@ impl ValueStore for u32 { 4 } } -impl ValueLoad<'_> for Tag { +impl ValueSer<'_> for Tag { const TYPE: ValueType = ValueType::Tag; fn load_aligned(buf: &[u32]) -> Option<Self> { buf.get(0).copied().map(u32::from_be).map(Tag) } -} -impl ValueStore for Tag { - fn get_type(&self) -> ValueType { - ValueType::Tag - } fn store_aligned(&self, buf: &mut Vec<u32>) { buf.push(self.0.to_be()); } @@ -183,18 +201,13 @@ impl ValueStore for Tag { 4 } } -impl ValueLoad<'_> for u64 { +impl ValueSer<'_> for u64 { const TYPE: ValueType = ValueType::UInt; fn load_aligned(buf: &[u32]) -> Option<Self> { let hi = u32::from_be(*buf.get(0)?) as u64; let lo = u32::from_be(*buf.get(1)?) as u64; Some(hi << 32 | lo) } -} -impl ValueStore for u64 { - fn get_type(&self) -> ValueType { - ValueType::UInt - } fn store_aligned(&self, buf: &mut Vec<u32>) { buf.push(((self >> 32) as u32).to_be()); buf.push((*self as u32).to_be()); @@ -203,7 +216,7 @@ impl ValueStore for u64 { 8 } } -impl ValueLoad<'_> for f64 { +impl ValueSer<'_> for f64 { const TYPE: ValueType = ValueType::Float; fn load_aligned(buf: &[u32]) -> Option<Self> { if buf.len() < 2 { @@ -215,11 +228,6 @@ impl ValueLoad<'_> for f64 { a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3], ])) } -} -impl ValueStore for f64 { - fn get_type(&self) -> ValueType { - ValueType::Float - } fn store_aligned(&self, buf: &mut Vec<u32>) { let b = self.to_be_bytes(); buf.push(u32::from_be_bytes([b[0], b[1], b[2], b[3]])); @@ -229,16 +237,11 @@ impl ValueStore for f64 { 8 } } -impl ValueLoad<'_> for i64 { +impl ValueSer<'_> for i64 { const TYPE: ValueType = ValueType::Int; fn load_aligned(buf: &[u32]) -> Option<Self> { u64::load_aligned(buf).map(|x| x as i64) } -} -impl ValueStore for i64 { - fn get_type(&self) -> ValueType { - ValueType::Int - } fn store_aligned(&self, buf: &mut Vec<u32>) { (*self as u64).store_aligned(buf); } @@ -246,47 +249,24 @@ impl ValueStore for i64 { 8 } } -impl<'a> ValueLoad<'a> for Object<'a> { +impl<'a> ValueSer<'a> for &'a Object { const TYPE: ValueType = ValueType::Object; fn load_aligned(buf: &'a [u32]) -> Option<Self> { - Self::load(buf) - } -} -impl ValueStore for Object<'_> { - fn get_type(&self) -> ValueType { - ValueType::Object - } - fn store_aligned(&self, buf: &mut Vec<u32>) { - buf.push(self.tags.len() as u32); - buf.extend(self.tags); - buf.extend(self.offsets); - buf.extend(self.values); - } - fn size(&self) -> usize { - (self.tags.len() + self.offsets.len() + self.values.len()) * size_of::<u32>() - } -} -impl ValueStore for ObjectBuffer { - fn get_type(&self) -> ValueType { - ValueType::Object + // TODO validate + Some(slice_to_ob(buf)) } fn store_aligned(&self, buf: &mut Vec<u32>) { buf.extend(&self.0); } fn size(&self) -> usize { - self.0.len() * 4 + self.0.len() * size_of::<u32>() } } -impl<'a> ValueLoad<'a> for &'a [u8] { +impl<'a> ValueSer<'a> for &'a [u8] { const TYPE: ValueType = ValueType::Binary; fn load_unaligned(buf: &'a [u8]) -> Option<Self> { Some(buf) } -} -impl ValueStore for &[u8] { - fn get_type(&self) -> ValueType { - ValueType::Binary - } fn store_unaligned(&self, buf: &mut Vec<u8>) { buf.extend(*self); } @@ -294,16 +274,11 @@ impl ValueStore for &[u8] { self.len() } } -impl<'a> ValueLoad<'a> for () { +impl<'a> ValueSer<'a> for () { const TYPE: ValueType = ValueType::Int; fn load_aligned(_buf: &'a [u32]) -> Option<Self> { Some(()) } -} -impl ValueStore for () { - fn get_type(&self) -> ValueType { - ValueType::Binary - } fn store_aligned(&self, _buf: &mut Vec<u32>) {} fn size(&self) -> usize { 0 |