sov_state/codec/
borsh_codec.rs1use super::{StateCodec, StateKeyCodec};
2use crate::codec::StateValueCodec;
3
4#[derive(Debug, Default, PartialEq, Eq, Clone, borsh::BorshDeserialize, borsh::BorshSerialize)]
6pub struct BorshCodec;
7
8impl<K> StateKeyCodec<K> for BorshCodec
9where
10 K: borsh::BorshSerialize + borsh::BorshDeserialize,
11{
12 fn encode_key(&self, value: &K) -> Vec<u8> {
13 value.try_to_vec().expect("Failed to serialize key")
14 }
15}
16
17impl<V> StateValueCodec<V> for BorshCodec
18where
19 V: borsh::BorshSerialize + borsh::BorshDeserialize,
20{
21 type Error = std::io::Error;
22
23 fn encode_value(&self, value: &V) -> Vec<u8> {
24 value.try_to_vec().expect("Failed to serialize value")
25 }
26
27 fn try_decode_value(&self, bytes: &[u8]) -> Result<V, Self::Error> {
28 V::try_from_slice(bytes)
29 }
30}
31
32impl StateCodec for BorshCodec {
33 type KeyCodec = Self;
34 type ValueCodec = Self;
35
36 fn key_codec(&self) -> &Self::KeyCodec {
37 self
38 }
39
40 fn value_codec(&self) -> &Self::ValueCodec {
41 self
42 }
43}