Skip to main content

commonware_storage/qmdb/any/
value.rs

1//! Type-state for operations with fixed or variable size.
2
3use commonware_codec::{CodecFixedShared, CodecShared};
4use std::marker::PhantomData;
5
6mod sealed {
7    use commonware_codec::CodecShared;
8
9    /// A wrapper around a value to indicate whether it is fixed or variable size.
10    /// Having separate wrappers for fixed and variable size values allows us to use the same
11    /// operation type for both fixed and variable size values, while still being able to
12    /// parameterize the operation encoding by the value type.
13    pub trait ValueEncoding: Clone + Send + Sync {
14        /// The wrapped value type.
15        type Value: CodecShared + Clone;
16    }
17}
18
19pub use sealed::ValueEncoding;
20
21/// A fixed-size, clonable value.
22#[derive(Clone, Debug, PartialEq, Eq)]
23pub struct FixedEncoding<V: FixedValue>(PhantomData<V>);
24
25impl<V: FixedValue> sealed::ValueEncoding for FixedEncoding<V> {
26    type Value = V;
27}
28
29/// A variable-size, clonable value.
30#[derive(Clone, Debug, PartialEq, Eq)]
31pub struct VariableEncoding<V: VariableValue>(PhantomData<V>);
32
33impl<V: VariableValue> sealed::ValueEncoding for VariableEncoding<V> {
34    type Value = V;
35}
36
37/// A fixed-size, clonable value.
38pub trait FixedValue: CodecFixedShared + Clone {}
39impl<T: CodecFixedShared + Clone> FixedValue for T {}
40
41/// A variable-size, clonable value.
42pub trait VariableValue: CodecShared + Clone {}
43impl<T: CodecShared + Clone> VariableValue for T {}