reddb_server/wire/
protocol.rs1use reddb_types::Value;
7
8#[inline]
15pub fn encode_value(buf: &mut Vec<u8>, value: &Value) {
16 reddb_wire::legacy::encode_native_value(buf, value);
17}
18
19#[inline]
20pub fn decode_value(data: &[u8], pos: &mut usize) -> Value {
21 try_decode_value(data, pos).unwrap_or(Value::Null)
22}
23
24#[inline]
25pub fn try_decode_value(data: &[u8], pos: &mut usize) -> Result<Value, &'static str> {
26 let value = reddb_wire::legacy::try_decode_value(data, pos)?;
27 Value::try_from(value)
28}
29
30#[cfg(test)]
31mod allocation_tests {
32 use super::*;
33
34 #[test]
35 fn borrowed_text_and_blob_encoding_never_allocate_temporary_value_copies() {
36 for value in [
37 Value::text("x".repeat(192 * 1024)),
38 Value::Blob(vec![1; 192 * 1024]),
39 ] {
40 let mut buffer =
41 Vec::with_capacity(reddb_wire::legacy::native_value_encoded_len(&value));
42 let (_, allocations) = crate::storage::no_malloc_hot_paths::measure_allocations(|| {
43 encode_value(&mut buffer, &value);
44 });
45 assert_eq!(
46 allocations.allocated_bytes, 0,
47 "borrowed value encoder: {allocations:?}"
48 );
49 assert_eq!(buffer.len(), buffer.capacity());
50 }
51 }
52}