aboutsummaryrefslogtreecommitdiff
path: root/common/object/src/value.rs
blob: aad6101f03aa3568c4ce1d69a51e9b490316e441 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
    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 crate::{Object, ObjectBuffer};

pub trait Value<'a>: ValueStore + 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
    }
}
pub trait ValueStore {
    fn is_aligned(&self) -> bool;
    fn store_aligned(&self, _buf: &mut Vec<u32>) {}
    fn store_unaligned(&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()
    }
}
impl ValueStore for &str {
    fn is_aligned(&self) -> bool {
        false
    }
    fn store_unaligned(&self, buf: &mut Vec<u8>) {
        buf.extend(self.as_bytes());
    }
    fn size(&self) -> usize {
        self.len()
    }
}
impl Value<'_> for u32 {
    const ALIGNED: bool = true;
    fn load_aligned(buf: &[u32]) -> Option<Self> {
        buf.get(0).copied().map(u32::from_be)
    }
}
impl ValueStore for u32 {
    fn is_aligned(&self) -> bool {
        true
    }
    fn store_aligned(&self, buf: &mut Vec<u32>) {
        buf.push(self.to_be());
    }
    fn size(&self) -> usize {
        4
    }
}
impl Value<'_> for u64 {
    const ALIGNED: bool = false;
    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 is_aligned(&self) -> bool {
        true
    }
    fn store_aligned(&self, buf: &mut Vec<u32>) {
        buf.push(((self >> 32) as u32).to_be());
        buf.push((*self as u32).to_be());
    }
    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)
    }
}
impl ValueStore for Object<'_> {
    fn is_aligned(&self) -> bool {
        true
    }
    fn store_aligned(&self, buf: &mut Vec<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 is_aligned(&self) -> bool {
        true
    }
    fn store_aligned(&self, buf: &mut Vec<u32>) {
        buf.extend(&self.0);
    }
    fn size(&self) -> usize {
        self.0.len() * 4
    }
}
impl<'a> Value<'a> for &'a [u8] {
    const ALIGNED: bool = false;
    fn load_unaligned(buf: &'a [u8]) -> Option<Self> {
        Some(buf)
    }
}
impl ValueStore for &[u8] {
    fn is_aligned(&self) -> bool {
        false
    }
    fn store_unaligned(&self, buf: &mut Vec<u8>) {
        buf.extend(*self);
    }
    fn size(&self) -> usize {
        self.len()
    }
}