fuel_core_storage/codec/
raw.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
36
37
//! The module contains the implementation of the `Raw` codec.
//! The codec is used for types that are already represented by bytes
//! and can be deserialized into bytes-based objects.

use crate::codec::{
    Decode,
    Encode,
};

#[cfg(feature = "std")]
use std::borrow::Cow;

#[cfg(not(feature = "std"))]
use alloc::borrow::Cow;

/// The codec is used for types that are already represented by bytes.
pub struct Raw;

impl<T> Encode<T> for Raw
where
    T: ?Sized + AsRef<[u8]>,
{
    type Encoder<'a> = Cow<'a, [u8]> where T: 'a;

    fn encode(t: &T) -> Self::Encoder<'_> {
        Cow::Borrowed(t.as_ref())
    }
}

impl<T> Decode<T> for Raw
where
    for<'a> T: TryFrom<&'a [u8]>,
{
    fn decode(bytes: &[u8]) -> anyhow::Result<T> {
        T::try_from(bytes).map_err(|_| anyhow::anyhow!("Unable to decode bytes"))
    }
}