surrealdb_core/kvs/
key.rs

1/// A trait for types which can be encoded as a kv-store key.
2pub trait KeyEncode {
3	fn encode(&self) -> Result<Vec<u8>, crate::err::Error> {
4		let mut buf = Vec::new();
5		self.encode_into(&mut buf)?;
6		Ok(buf)
7	}
8
9	fn encode_owned(self) -> Result<Vec<u8>, crate::err::Error>
10	where
11		Self: Sized,
12	{
13		self.encode()
14	}
15
16	/// Push the bytes this key would encode into the buffer.
17	///
18	/// Implementation can make no assumption about the contents of the buffer.
19	/// The buffer should not be cleared and if there are bytes present in the buffer they should
20	/// also be present when this function returns.
21	fn encode_into(&self, buffer: &mut Vec<u8>) -> Result<(), crate::err::Error>;
22
23	fn encode_owned_into(self, buffer: &mut Vec<u8>) -> Result<(), crate::err::Error>
24	where
25		Self: Sized,
26	{
27		self.encode_into(buffer)
28	}
29}
30
31/// A trait for types which can be decoded from a kv-store key bytes.
32pub trait KeyDecode<'a> {
33	fn decode(bytes: &'a [u8]) -> Result<Self, crate::err::Error>
34	where
35		Self: Sized;
36}
37
38pub trait KeyDecodeOwned: for<'a> KeyDecode<'a> {
39	/// Decode the key from an owned vector.
40	///
41	/// A lot of kv query methods return vectors for keys, which some key types might be able to
42	/// use to more effeciently decode the data.
43	///
44	/// The default implementation just calls decode
45	fn decode_from_vec(bytes: Vec<u8>) -> Result<Self, crate::err::Error>
46	where
47		Self: Sized,
48	{
49		Self::decode(&bytes)
50	}
51}
52
53impl KeyEncode for Vec<u8> {
54	fn encode(&self) -> Result<Vec<u8>, crate::err::Error> {
55		Ok(self.clone())
56	}
57
58	fn encode_owned(self) -> Result<Vec<u8>, crate::err::Error> {
59		Ok(self)
60	}
61
62	fn encode_into(&self, buffer: &mut Vec<u8>) -> Result<(), crate::err::Error> {
63		buffer.extend_from_slice(self);
64		Ok(())
65	}
66
67	fn encode_owned_into(self, buffer: &mut Vec<u8>) -> Result<(), crate::err::Error> {
68		if buffer.is_empty() {
69			// we can just move self into the buffer since there is no data.
70			*buffer = self;
71		} else {
72			// we can't overwrite the buffer so instead copy self into it.
73			buffer.extend_from_slice(&self);
74		}
75		Ok(())
76	}
77}
78
79impl<K: KeyEncode> KeyEncode for &K {
80	fn encode_into(&self, buffer: &mut Vec<u8>) -> Result<(), crate::err::Error> {
81		(*self).encode_into(buffer)
82	}
83}
84
85impl KeyEncode for &str {
86	fn encode_into(&self, buffer: &mut Vec<u8>) -> Result<(), crate::err::Error> {
87		buffer.extend_from_slice(self.as_bytes());
88		Ok(())
89	}
90}
91
92impl KeyEncode for &[u8] {
93	fn encode_into(&self, buffer: &mut Vec<u8>) -> Result<(), crate::err::Error> {
94		buffer.extend_from_slice(self);
95		Ok(())
96	}
97}
98
99impl KeyDecode<'_> for Vec<u8> {
100	fn decode(bytes: &[u8]) -> Result<Self, crate::err::Error>
101	where
102		Self: Sized,
103	{
104		Ok(bytes.to_vec())
105	}
106}
107
108impl KeyDecodeOwned for Vec<u8> {
109	fn decode_from_vec(bytes: Vec<u8>) -> Result<Self, crate::err::Error> {
110		Ok(bytes)
111	}
112}
113
114impl<'a> KeyDecode<'a> for () {
115	fn decode(_: &'a [u8]) -> Result<Self, crate::err::Error>
116	where
117		Self: Sized,
118	{
119		Ok(())
120	}
121}
122
123impl KeyDecodeOwned for () {
124	fn decode_from_vec(_: Vec<u8>) -> Result<Self, crate::err::Error>
125	where
126		Self: Sized,
127	{
128		Ok(())
129	}
130}
131
132/// Implements KeyEncode and KeyDecode uusing storekey and deserialize and serialize
133/// implementations.
134macro_rules! impl_key {
135	($name:ident$(<$l:lifetime>)?) => {
136		impl$(<$l>)? crate::kvs::KeyEncode for $name $(<$l>)?{
137			fn encode(&self) -> Result<Vec<u8>, crate::err::Error> {
138				Ok(storekey::serialize(self)?)
139			}
140
141			fn encode_into(&self, buffer: &mut Vec<u8>) -> Result<(), crate::err::Error> {
142				Ok(storekey::serialize_into(buffer, self)?)
143			}
144		}
145
146		impl_key!(@decode $name $(,$l)?);
147	};
148
149	(@decode $name:ident, $l:lifetime) => {
150		impl<$l> crate::kvs::KeyDecode<$l> for $name<$l>{
151			fn decode(bytes: &$l[u8]) -> Result<Self, crate::err::Error> {
152				Ok(storekey::deserialize(bytes)?)
153			}
154		}
155	};
156
157	(@decode $name:ident) => {
158		impl<'a> crate::kvs::KeyDecode<'a> for $name{
159			fn decode(bytes: &'a[u8]) -> Result<Self, crate::err::Error> {
160				Ok(storekey::deserialize(bytes)?)
161			}
162		}
163
164		impl crate::kvs::KeyDecodeOwned for $name {
165			fn decode_from_vec(bytes: Vec<u8>) -> Result<Self, crate::err::Error> {
166				Ok(storekey::deserialize(bytes.as_slice())?)
167			}
168		}
169	};
170}
171pub(crate) use impl_key;