surrealdb_core/sql/
serde.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use bincode::Options;
use bincode::Result;
use serde::{Deserialize, Serialize};

pub fn serialize<T>(value: &T) -> Result<Vec<u8>>
where
	T: ?Sized + Serialize,
{
	bincode::options()
		// Don't specify a byte limit
		.with_no_limit()
		// Use little-endian data ordering
		.with_little_endian()
		// Use variable-sized integer encoding
		.with_varint_encoding()
		// Serialize the value
		.serialize(value)
}

pub fn deserialize<'a, T>(bytes: &'a [u8]) -> Result<T>
where
	T: Deserialize<'a>,
{
	bincode::options()
		// Don't specify a byte limit
		.with_no_limit()
		// Use little-endian data ordering
		.with_little_endian()
		// Use variable-sized integer encoding
		.with_varint_encoding()
		// Allow any remaining unused data
		.allow_trailing_bytes()
		// Deserialize the value
		.deserialize(bytes)
}