aboutsummaryrefslogtreecommitdiff
path: root/common/object/src/tag.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2026-02-27 20:56:20 +0100
committermetamuffin <metamuffin@disroot.org>2026-02-27 20:56:20 +0100
commit7930d543a2aa68d4ad2958605827d7eb1baa91f8 (patch)
treefe59d1f549e303a96b78d3e925d75abb70b73af0 /common/object/src/tag.rs
parentc05bfcc2775f0e11db6e856bfcf06d0419c35d54 (diff)
downloadjellything-7930d543a2aa68d4ad2958605827d7eb1baa91f8.tar
jellything-7930d543a2aa68d4ad2958605827d7eb1baa91f8.tar.bz2
jellything-7930d543a2aa68d4ad2958605827d7eb1baa91f8.tar.zst
reimplement Object as slice type
Diffstat (limited to 'common/object/src/tag.rs')
-rw-r--r--common/object/src/tag.rs73
1 files changed, 73 insertions, 0 deletions
diff --git a/common/object/src/tag.rs b/common/object/src/tag.rs
new file mode 100644
index 0000000..c09af27
--- /dev/null
+++ b/common/object/src/tag.rs
@@ -0,0 +1,73 @@
+/*
+ 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 <metamuffin.org>
+*/
+
+use core::marker::{PhantomData, Sized};
+use std::fmt::Display;
+
+#[repr(transparent)]
+#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
+pub struct Tag(pub u32);
+
+impl Default for Tag {
+ fn default() -> Self {
+ Self::new(b"1111")
+ }
+}
+impl Tag {
+ pub const fn new(fourcc: &[u8; 4]) -> Self {
+ Self(u32::from_le_bytes(*fourcc))
+ }
+}
+impl Display for Tag {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.write_str(str::from_utf8(&self.0.to_le_bytes()).unwrap())
+ }
+}
+
+#[derive(PartialEq, Eq, PartialOrd, Ord)]
+pub struct TypedTag<T: ?Sized>(pub Tag, pub PhantomData<T>);
+impl<T: ?Sized> Display for TypedTag<T> {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ self.0.fmt(f)
+ }
+}
+impl<T: ?Sized> Clone for TypedTag<T> {
+ fn clone(&self) -> Self {
+ Self(self.0, PhantomData)
+ }
+}
+impl<T: ?Sized> Copy for TypedTag<T> {}
+impl<T: ?Sized> TypedTag<T> {
+ pub const fn new(tag: Tag) -> Self {
+ Self(tag, PhantomData)
+ }
+}
+
+pub mod types {
+ use crate::Object;
+ use std::any::TypeId;
+
+ pub const OBJECT: TypeId = TypeId::of::<Object>();
+ pub const STR: TypeId = TypeId::of::<&str>();
+ pub const BINARY: TypeId = TypeId::of::<&[u8]>();
+ pub const U32: TypeId = TypeId::of::<u32>();
+ pub const U64: TypeId = TypeId::of::<u64>();
+ pub const I64: TypeId = TypeId::of::<i64>();
+ pub const F64: TypeId = TypeId::of::<f64>();
+}
+
+#[macro_export]
+macro_rules! fields {
+ ($($id:ident: $type:ty = $tag:literal;)*) => {
+ $(pub const $id: $crate::TypedTag<$type> = $crate::TypedTag::new($crate::Tag::new($tag));)*
+ };
+}
+#[macro_export]
+macro_rules! enums {
+ ($($id:ident = $tag:literal;)*) => {
+ $(pub const $id: $crate::Tag = $crate::Tag::new($tag);)*
+ };
+}