reach_core/storage/
db.rs

1// SPDX-FileCopyrightText: 2024 eaon <eaon@posteo.net>
2// SPDX-License-Identifier: EUPL-1.2
3
4use std::fmt::Debug;
5use std::marker::PhantomData;
6
7use crate::storage;
8use crate::traits::*;
9use crate::wire;
10
11use redb::{Key, TypeName, Value};
12
13#[derive(Debug)]
14pub struct Prost<T, const S: usize = 0> {
15    _type: PhantomData<T>,
16}
17
18impl<T, const S: usize> Value for Prost<T, S>
19where
20    T: ProstDecode + ProstEncode + Debug,
21{
22    type SelfType<'a>
23        = T
24    where
25        Self: 'a;
26
27    type AsBytes<'a>
28        = Vec<u8>
29    where
30        Self: 'a;
31
32    fn fixed_width() -> Option<usize> {
33        match S {
34            0 => None,
35            _ => Some(S),
36        }
37    }
38
39    fn from_bytes<'a>(data: &'a [u8]) -> Self::SelfType<'a>
40    where
41        Self: 'a,
42    {
43        T::decode(data).expect("Bytes shouldn't be corrupted on disk or in memory.")
44    }
45
46    fn as_bytes<'a, 'b: 'a>(value: &'a Self::SelfType<'b>) -> Self::AsBytes<'a>
47    where
48        Self: 'a,
49        Self: 'b,
50    {
51        value.encode_to_vec()
52    }
53
54    fn type_name() -> TypeName {
55        TypeName::new(&format!("Prost<{}>", std::any::type_name::<T>()))
56    }
57}
58
59impl<T, const S: usize> Key for Prost<T, S>
60where
61    T: ProstDecode + ProstEncode + Debug,
62{
63    fn compare(data1: &[u8], data2: &[u8]) -> std::cmp::Ordering {
64        data1.cmp(data2)
65    }
66}
67
68pub type Envelope = Prost<wire::Envelope>;
69pub type ReachableVerifyingKeys = Prost<wire::ReachableVerifyingKeys, 1669>;
70pub type ReachablePublicKeys = Prost<wire::ReachablePublicKeys, 1956>;
71pub type SharedPublicKeys = Prost<wire::SharedPublicKeys, 5885>;
72pub type MessageVault = Prost<wire::MessageVault>;
73pub type GenericVault = Prost<storage::GenericVault>;