kvstructs/
value_mut.rs

1use crate::{Value, ValueExt, ValueRef};
2use bytes::{Bytes, BytesMut};
3use core::ops::{Deref, DerefMut};
4
5/// ValueMut represents the value info that can be associated with a key, but also the internal
6/// Meta field. The data in the ValueMut is mutable.
7///
8/// # Design for ValueMut
9///
10/// **Note:**
11/// 1. `version` field will not be encoded, it is a helper field.
12/// 2. `expiration` field will be encoded as uvarient, which means after encoded, the size of
13/// this field is less or equal to 8 bytes.
14///
15/// ```text
16/// +----------+-----------------+--------------------+--------------------+--------------------+
17/// |   meta   |    user meta    |     expiration     |      version       |        data        |
18/// +----------+-----------------+--------------------+--------------------+--------------------+
19/// |  1 byte  |      1 byte     |      8 bytes       |      8 bytes       |       n bytes      |
20/// +----------+-----------------+--------------------+--------------------+--------------------+
21/// ```
22#[derive(Default, Debug, Eq, PartialEq, Ord, PartialOrd, Clone, Hash)]
23#[repr(C)]
24pub struct ValueMut {
25    pub(crate) meta: u8,
26    pub(crate) user_meta: u8,
27    pub(crate) expires_at: u64,
28    pub(crate) version: u64, // This field is not serialized. Only for internal usage.
29    pub(crate) value: BytesMut,
30}
31
32impl ValueMut {
33    /// Freeze to Value.
34    #[inline]
35    pub fn freeze(self) -> Value {
36        Value {
37            meta: self.meta,
38            user_meta: self.user_meta,
39            expires_at: self.expires_at,
40            version: self.version,
41            value: self.value.freeze(),
42        }
43    }
44}
45
46impl Deref for ValueMut {
47    type Target = BytesMut;
48
49    fn deref(&self) -> &Self::Target {
50        &self.value
51    }
52}
53
54impl DerefMut for ValueMut {
55    fn deref_mut(&mut self) -> &mut Self::Target {
56        &mut self.value
57    }
58}
59
60impl ValueExt for ValueMut {
61    #[inline]
62    fn as_value_ref(&self) -> ValueRef {
63        ValueRef {
64            meta: self.meta,
65            user_meta: self.user_meta,
66            expires_at: self.expires_at,
67            version: self.version,
68            val: self.parse_value(),
69        }
70    }
71
72    #[inline]
73    fn parse_value(&self) -> &[u8] {
74        self.value.as_ref()
75    }
76
77    /// Unlike `Value` do shallow copy, the implementation for `parse_value_to_bytes`
78    /// in `ValueMut` will full copy.
79    #[inline]
80    fn parse_value_to_bytes(&self) -> Bytes {
81        Bytes::copy_from_slice(self.value.as_ref())
82    }
83
84    #[inline]
85    fn get_meta(&self) -> u8 {
86        self.meta
87    }
88
89    #[inline]
90    fn get_user_meta(&self) -> u8 {
91        self.user_meta
92    }
93
94    #[inline]
95    fn get_expires_at(&self) -> u64 {
96        self.expires_at
97    }
98}
99
100impl ValueMutExt for ValueMut {
101    #[inline]
102    fn parse_value_mut(&mut self) -> &mut [u8] {
103        self.value.as_mut()
104    }
105
106    #[inline]
107    fn set_meta(&mut self, meta: u8) {
108        self.meta = meta
109    }
110
111    #[inline]
112    fn set_user_meta(&mut self, user_meta: u8) {
113        self.user_meta = user_meta
114    }
115
116    #[inline]
117    fn set_expires_at(&mut self, expires_at: u64) {
118        self.expires_at = expires_at
119    }
120}
121
122/// Extensions for `ValueMut`
123pub trait ValueMutExt {
124    /// Returns the mutable data slice store in ValueMut
125    fn parse_value_mut(&mut self) -> &mut [u8];
126
127    /// Set the meta of the value
128    fn set_meta(&mut self, meta: u8);
129
130    /// Set the user meta
131    fn set_user_meta(&mut self, user_meta: u8);
132
133    /// Set the expiration time (unix timestamp) for this value
134    fn set_expires_at(&mut self, expires_at: u64);
135}