pub trait Decode: Sized {
// Required method
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError>;
}
Expand description
Trait that makes a type able to be decoded, akin to serde’s DeserializeOwned
trait.
This trait should be implemented for types which do not have references to data in the reader. For types that contain e.g. &str
and &[u8]
, implement BorrowDecode instead.
Whenever you implement Decode
for your type, the base trait BorrowDecode
is automatically implemented.
This trait will be automatically implemented if you enable the derive
feature and add #[derive(bincode::Decode)]
to your type. Note that if the type contains any lifetimes, BorrowDecode
will be implemented instead.
Implementing this trait manually
If you want to implement this trait for your type, the easiest way is to add a #[derive(bincode::Decode)]
, build and check your target/generated/bincode/
folder. This should generate a <Struct name>_Decode.rs
file.
For this struct:
struct Entity {
pub x: f32,
pub y: f32,
}
It will look something like:
impl bincode::Decode for Entity {
fn decode<D: bincode::de::Decoder>(
decoder: &mut D,
) -> core::result::Result<Self, bincode::error::DecodeError> {
Ok(Self {
x: bincode::Decode::decode(decoder)?,
y: bincode::Decode::decode(decoder)?,
})
}
}
impl<'de> bincode::BorrowDecode<'de> for Entity {
fn borrow_decode<D: bincode::de::BorrowDecoder<'de>>(
decoder: &mut D,
) -> core::result::Result<Self, bincode::error::DecodeError> {
Ok(Self {
x: bincode::BorrowDecode::borrow_decode(decoder)?,
y: bincode::BorrowDecode::borrow_decode(decoder)?,
})
}
}
From here you can add/remove fields, or add custom logic.
To get specific integer types, you can use:
let x: u8 = bincode::Decode::decode(decoder)?;
let x = <u8 as bincode::Decode>::decode(decoder)?;
Required Methods§
Implementations on Foreign Types§
source§impl Decode for NonZeroI32
impl Decode for NonZeroI32
source§impl<A, B, C, D, E, F, G, H, I, J, K, L, M> Decode for (A, B, C, D, E, F, G, H, I, J, K, L, M)where
A: Decode,
B: Decode,
C: Decode,
D: Decode,
E: Decode,
F: Decode,
G: Decode,
H: Decode,
I: Decode,
J: Decode,
K: Decode,
L: Decode,
M: Decode,
impl<A, B, C, D, E, F, G, H, I, J, K, L, M> Decode for (A, B, C, D, E, F, G, H, I, J, K, L, M)where A: Decode, B: Decode, C: Decode, D: Decode, E: Decode, F: Decode, G: Decode, H: Decode, I: Decode, J: Decode, K: Decode, L: Decode, M: Decode,
source§impl Decode for SystemTime
impl Decode for SystemTime
source§impl<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O> Decode for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O)where
A: Decode,
B: Decode,
C: Decode,
D: Decode,
E: Decode,
F: Decode,
G: Decode,
H: Decode,
I: Decode,
J: Decode,
K: Decode,
L: Decode,
M: Decode,
N: Decode,
O: Decode,
impl<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O> Decode for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O)where A: Decode, B: Decode, C: Decode, D: Decode, E: Decode, F: Decode, G: Decode, H: Decode, I: Decode, J: Decode, K: Decode, L: Decode, M: Decode, N: Decode, O: Decode,
source§impl<K, V, S> Decode for HashMap<K, V, S>where
K: Decode + Eq + Hash,
V: Decode,
S: BuildHasher + Default,
impl<K, V, S> Decode for HashMap<K, V, S>where K: Decode + Eq + Hash, V: Decode, S: BuildHasher + Default,
source§impl Decode for NonZeroU128
impl Decode for NonZeroU128
source§impl<A, B, C, D, E, F, G, H> Decode for (A, B, C, D, E, F, G, H)where
A: Decode,
B: Decode,
C: Decode,
D: Decode,
E: Decode,
F: Decode,
G: Decode,
H: Decode,
impl<A, B, C, D, E, F, G, H> Decode for (A, B, C, D, E, F, G, H)where A: Decode, B: Decode, C: Decode, D: Decode, E: Decode, F: Decode, G: Decode, H: Decode,
source§impl<A, B, C, D, E, F, G> Decode for (A, B, C, D, E, F, G)where
A: Decode,
B: Decode,
C: Decode,
D: Decode,
E: Decode,
F: Decode,
G: Decode,
impl<A, B, C, D, E, F, G> Decode for (A, B, C, D, E, F, G)where A: Decode, B: Decode, C: Decode, D: Decode, E: Decode, F: Decode, G: Decode,
source§impl Decode for NonZeroU64
impl Decode for NonZeroU64
source§impl Decode for NonZeroI64
impl Decode for NonZeroI64
source§impl Decode for SocketAddr
impl Decode for SocketAddr
source§impl Decode for NonZeroIsize
impl Decode for NonZeroIsize
source§impl<T> Decode for PhantomData<T>
impl<T> Decode for PhantomData<T>
source§impl Decode for NonZeroU16
impl Decode for NonZeroU16
source§impl Decode for AtomicBool
impl Decode for AtomicBool
source§impl Decode for NonZeroI16
impl Decode for NonZeroI16
source§impl<A, B, C, D, E, F, G, H, I, J, K, L> Decode for (A, B, C, D, E, F, G, H, I, J, K, L)where
A: Decode,
B: Decode,
C: Decode,
D: Decode,
E: Decode,
F: Decode,
G: Decode,
H: Decode,
I: Decode,
J: Decode,
K: Decode,
L: Decode,
impl<A, B, C, D, E, F, G, H, I, J, K, L> Decode for (A, B, C, D, E, F, G, H, I, J, K, L)where A: Decode, B: Decode, C: Decode, D: Decode, E: Decode, F: Decode, G: Decode, H: Decode, I: Decode, J: Decode, K: Decode, L: Decode,
source§impl<A, B, C, D, E> Decode for (A, B, C, D, E)where
A: Decode,
B: Decode,
C: Decode,
D: Decode,
E: Decode,
impl<A, B, C, D, E> Decode for (A, B, C, D, E)where A: Decode, B: Decode, C: Decode, D: Decode, E: Decode,
source§impl Decode for AtomicUsize
impl Decode for AtomicUsize
source§impl Decode for SocketAddrV4
impl Decode for SocketAddrV4
source§impl<A, B, C, D, E, F, G, H, I, J> Decode for (A, B, C, D, E, F, G, H, I, J)where
A: Decode,
B: Decode,
C: Decode,
D: Decode,
E: Decode,
F: Decode,
G: Decode,
H: Decode,
I: Decode,
J: Decode,
impl<A, B, C, D, E, F, G, H, I, J> Decode for (A, B, C, D, E, F, G, H, I, J)where A: Decode, B: Decode, C: Decode, D: Decode, E: Decode, F: Decode, G: Decode, H: Decode, I: Decode, J: Decode,
source§impl<'cow, T> Decode for Cow<'cow, T>where
T: ToOwned + ?Sized,
<T as ToOwned>::Owned: Decode,
impl<'cow, T> Decode for Cow<'cow, T>where T: ToOwned + ?Sized, <T as ToOwned>::Owned: Decode,
source§impl<A, B, C, D, E, F> Decode for (A, B, C, D, E, F)where
A: Decode,
B: Decode,
C: Decode,
D: Decode,
E: Decode,
F: Decode,
impl<A, B, C, D, E, F> Decode for (A, B, C, D, E, F)where A: Decode, B: Decode, C: Decode, D: Decode, E: Decode, F: Decode,
source§impl Decode for SocketAddrV6
impl Decode for SocketAddrV6
source§impl Decode for NonZeroUsize
impl Decode for NonZeroUsize
source§impl<A, B, C, D, E, F, G, H, I, J, K> Decode for (A, B, C, D, E, F, G, H, I, J, K)where
A: Decode,
B: Decode,
C: Decode,
D: Decode,
E: Decode,
F: Decode,
G: Decode,
H: Decode,
I: Decode,
J: Decode,
K: Decode,
impl<A, B, C, D, E, F, G, H, I, J, K> Decode for (A, B, C, D, E, F, G, H, I, J, K)where A: Decode, B: Decode, C: Decode, D: Decode, E: Decode, F: Decode, G: Decode, H: Decode, I: Decode, J: Decode, K: Decode,
source§impl<A, B, C, D, E, F, G, H, I, J, K, L, M, N> Decode for (A, B, C, D, E, F, G, H, I, J, K, L, M, N)where
A: Decode,
B: Decode,
C: Decode,
D: Decode,
E: Decode,
F: Decode,
G: Decode,
H: Decode,
I: Decode,
J: Decode,
K: Decode,
L: Decode,
M: Decode,
N: Decode,
impl<A, B, C, D, E, F, G, H, I, J, K, L, M, N> Decode for (A, B, C, D, E, F, G, H, I, J, K, L, M, N)where A: Decode, B: Decode, C: Decode, D: Decode, E: Decode, F: Decode, G: Decode, H: Decode, I: Decode, J: Decode, K: Decode, L: Decode, M: Decode, N: Decode,
source§impl<T> Decode for RangeInclusive<T>where
T: Decode,
impl<T> Decode for RangeInclusive<T>where T: Decode,
source§impl Decode for AtomicIsize
impl Decode for AtomicIsize
source§impl<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P> Decode for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P)where
A: Decode,
B: Decode,
C: Decode,
D: Decode,
E: Decode,
F: Decode,
G: Decode,
H: Decode,
I: Decode,
J: Decode,
K: Decode,
L: Decode,
M: Decode,
N: Decode,
O: Decode,
P: Decode,
impl<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P> Decode for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P)where A: Decode, B: Decode, C: Decode, D: Decode, E: Decode, F: Decode, G: Decode, H: Decode, I: Decode, J: Decode, K: Decode, L: Decode, M: Decode, N: Decode, O: Decode, P: Decode,
source§impl Decode for NonZeroI128
impl Decode for NonZeroI128
source§impl<A, B, C, D, E, F, G, H, I> Decode for (A, B, C, D, E, F, G, H, I)where
A: Decode,
B: Decode,
C: Decode,
D: Decode,
E: Decode,
F: Decode,
G: Decode,
H: Decode,
I: Decode,
impl<A, B, C, D, E, F, G, H, I> Decode for (A, B, C, D, E, F, G, H, I)where A: Decode, B: Decode, C: Decode, D: Decode, E: Decode, F: Decode, G: Decode, H: Decode, I: Decode,
source§impl Decode for NonZeroU32
impl Decode for NonZeroU32
Implementors§
impl<T> Decode for Compat<T>where T: DeserializeOwned,
serde
only.