From ffa6b5c4ae2cdd3e07426ed0330f3f66e90ee57b Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 6 Jan 2026 19:10:37 +0100 Subject: tag registry --- common/object/src/buffer.rs | 12 ++++++++++++ common/object/src/lib.rs | 5 ++++- common/object/src/registry.rs | 27 +++++++++++++++++++++++++++ common/object/src/tests.rs | 12 ++++++++++++ common/object/src/value.rs | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 common/object/src/registry.rs (limited to 'common/object/src') diff --git a/common/object/src/buffer.rs b/common/object/src/buffer.rs index 56b8caf..dbde833 100644 --- a/common/object/src/buffer.rs +++ b/common/object/src/buffer.rs @@ -5,6 +5,7 @@ */ use crate::{Object, Tag, ValueStore}; +use bytemuck::try_cast_vec; pub struct ObjectBuffer(pub Vec); @@ -48,3 +49,14 @@ impl ObjectBuffer { ) } } + +impl From> for ObjectBuffer { + fn from(value: Vec) -> Self { + ObjectBuffer(try_cast_vec(value).unwrap_or_else(|(_, v)| { + v.into_iter() + .array_chunks() + .map(u32::from_ne_bytes) + .collect() + })) + } +} diff --git a/common/object/src/lib.rs b/common/object/src/lib.rs index 831dee7..522b6c1 100644 --- a/common/object/src/lib.rs +++ b/common/object/src/lib.rs @@ -3,12 +3,15 @@ which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2026 metamuffin */ +#![feature(iter_array_chunks)] mod buffer; +mod registry; #[cfg(test)] mod tests; mod value; pub use buffer::*; +pub use registry::*; pub use value::*; use std::marker::PhantomData; @@ -18,7 +21,7 @@ use std::marker::PhantomData; pub struct Tag(pub u32); pub struct TypedTag(pub Tag, pub PhantomData); -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] pub struct Object<'a> { tags: &'a [u32], offsets: &'a [u32], diff --git a/common/object/src/registry.rs b/common/object/src/registry.rs new file mode 100644 index 0000000..7148efd --- /dev/null +++ b/common/object/src/registry.rs @@ -0,0 +1,27 @@ +/* + This file is part of jellything (https://codeberg.org/metamuffin/jellything) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2026 metamuffin +*/ + +use crate::Tag; +use log::error; +use std::{any::TypeId, collections::BTreeMap}; + +#[derive(Default)] +pub struct Registry { + tags: BTreeMap, +} +impl Registry { + pub fn add(&mut self, tag: Tag, info: TagInfo) { + if let Some(other) = self.tags.get(&tag) { + error!("Tag conflict: {:?} vs {:?}", info.name, other.name) + } + self.tags.insert(tag, info); + } +} + +pub struct TagInfo { + pub name: &'static str, + pub r#type: Option, +} diff --git a/common/object/src/tests.rs b/common/object/src/tests.rs index 35a29ba..143782c 100644 --- a/common/object/src/tests.rs +++ b/common/object/src/tests.rs @@ -37,3 +37,15 @@ fn read_multi_field() { assert_eq!(friends.next(), Some("Charlie")); assert_eq!(friends.next(), None); } + +#[test] +fn vec_align_test() { + let mut c = 0; + for _ in 0..10_000 { + let x = Vec::::with_capacity(16); + if x.as_ptr().align_offset(4) == 0 { + c += 1; + } + } + assert_eq!(c, 10_000, "correctly aligned vecs by system allocator") +} diff --git a/common/object/src/value.rs b/common/object/src/value.rs index d77d53a..1b24e79 100644 --- a/common/object/src/value.rs +++ b/common/object/src/value.rs @@ -107,3 +107,37 @@ impl ValueStore for ObjectBuffer { self.0.len() * 4 } } +impl<'a> Value<'a> for &'a [u8] { + const ALIGNED: bool = false; + fn load_unaligned(buf: &'a [u8]) -> Option { + Some(buf) + } +} +impl ValueStore for &[u8] { + fn is_aligned(&self) -> bool { + false + } + fn store_unaligned(&self, buf: &mut Vec) { + buf.extend(*self); + } + fn size(&self) -> usize { + self.len() + } +} +impl<'a> Value<'a> for &'a [u32] { + const ALIGNED: bool = true; + fn load_aligned(buf: &'a [u32]) -> Option { + Some(buf) + } +} +impl ValueStore for &[u32] { + fn is_aligned(&self) -> bool { + true + } + fn store_aligned(&self, buf: &mut Vec) { + buf.extend(*self); + } + fn size(&self) -> usize { + self.len() * 4 + } +} -- cgit v1.3