blob: 18aae97c1da11c222492538e9bbdf575f33e30c6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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) 2025 metamuffin <metamuffin.org>
*/
use crate::Object;
pub trait Value<'a>: Sized {
const ALIGNED: bool;
fn load_aligned(buf: &'a [u32]) -> Option<Self> {
let _ = buf;
None
}
fn load_unaligned(buf: &'a [u8]) -> Option<Self> {
let _ = buf;
None
}
fn store(&self, buf: &mut Vec<u8>);
fn size(&self) -> usize;
}
impl<'a> Value<'a> for &'a str {
const ALIGNED: bool = false;
fn load_unaligned(buf: &'a [u8]) -> Option<Self> {
str::from_utf8(buf).ok()
}
fn store(&self, buf: &mut Vec<u8>) {
buf.extend(self.as_bytes());
}
fn size(&self) -> usize {
self.len()
}
}
impl Value<'_> for u32 {
const ALIGNED: bool = false;
fn load_aligned(buf: &[u32]) -> Option<Self> {
buf.get(0).copied()
}
fn store(&self, buf: &mut Vec<u8>) {
buf.extend(self.to_ne_bytes());
}
fn size(&self) -> usize {
4
}
}
impl Value<'_> for u64 {
const ALIGNED: bool = false;
fn load_aligned(buf: &[u32]) -> Option<Self> {
let hi = *buf.get(0)? as u64;
let lo = *buf.get(1)? as u64;
Some(hi << 32 | lo)
}
fn store(&self, buf: &mut Vec<u8>) {
buf.extend(self.to_ne_bytes());
}
fn size(&self) -> usize {
8
}
}
impl<'a> Value<'a> for Object<'a> {
const ALIGNED: bool = true;
fn load_aligned(buf: &'a [u32]) -> Option<Self> {
Self::load(buf)
}
fn store(&self, buf: &mut Vec<u8>) {
buf.extend(self.tags.iter().copied().map(u32::to_ne_bytes).flatten());
buf.extend(self.offsets.iter().copied().map(u32::to_ne_bytes).flatten());
buf.extend(self.values.iter().copied().map(u32::to_ne_bytes).flatten());
}
fn size(&self) -> usize {
(self.tags.len() + self.offsets.len() + self.values.len()) * size_of::<u32>()
}
}
|