pub trait Decode<Context>: Sized {
// Required method
fn decode<D: Decoder<Context = Context>>(
decoder: &mut D,
) -> Result<Self, DecodeError>;
}Expand description
Trait that makes a type able to be decoded, akin to serde’s DeserializeOwned trait.
Some types may require specific contexts. For example, to decode arena-based collections, an arena allocator must be provided as a context. In these cases, the context type Context should be specified or bounded.
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 derive Decode for your type, the base trait BorrowDecode is automatically implemented.
This trait will be automatically implemented with unbounded Context 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<Context> bincode::Decode<Context> for Entity {
fn decode<D: bincode::de::Decoder<Context = Context>>(
decoder: &mut D,
) -> core::result::Result<Self, bincode::error::DecodeError> {
Ok(Self {
x: bincode::Decode::decode(decoder)?,
y: bincode::Decode::decode(decoder)?,
})
}
}
impl<'de, Context> bincode::BorrowDecode<'de, Context> for Entity {
fn borrow_decode<D: bincode::de::BorrowDecoder<'de, Context = Context>>(
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::<Context>::decode(decoder)?;
let x = <u8 as bincode::Decode::<Context>>::decode(decoder)?;You can use Context to require contexts for decoding a type:
use bincode::de::Decoder;
use bincode::error::DecodeError;
struct BytesInArena<'a>(bumpalo::collections::Vec<'a, u8>);
impl<'a> bincode::Decode<&'a bumpalo::Bump> for BytesInArena<'a> {
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
todo!()
}Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl<Context> Decode<Context> for SocketAddr
impl<Context> Decode<Context> for SocketAddr
Source§impl<Context> Decode<Context> for SocketAddrV4
impl<Context> Decode<Context> for SocketAddrV4
Source§impl<Context> Decode<Context> for SocketAddrV6
impl<Context> Decode<Context> for SocketAddrV6
Source§impl<Context> Decode<Context> for AtomicBool
Available on target_has_atomic=8 only.
impl<Context> Decode<Context> for AtomicBool
target_has_atomic=8 only.Source§impl<Context> Decode<Context> for AtomicIsize
Available on target_has_atomic=ptr only.
impl<Context> Decode<Context> for AtomicIsize
target_has_atomic=ptr only.Source§impl<Context> Decode<Context> for AtomicUsize
Available on target_has_atomic=ptr only.
impl<Context> Decode<Context> for AtomicUsize
target_has_atomic=ptr only.Source§impl<Context> Decode<Context> for SystemTime
impl<Context> Decode<Context> for SystemTime
Source§impl<Context> Decode<Context> for NonZeroI16
impl<Context> Decode<Context> for NonZeroI16
Source§impl<Context> Decode<Context> for NonZeroI32
impl<Context> Decode<Context> for NonZeroI32
Source§impl<Context> Decode<Context> for NonZeroI64
impl<Context> Decode<Context> for NonZeroI64
Source§impl<Context> Decode<Context> for NonZeroI128
impl<Context> Decode<Context> for NonZeroI128
Source§impl<Context> Decode<Context> for NonZeroIsize
impl<Context> Decode<Context> for NonZeroIsize
Source§impl<Context> Decode<Context> for NonZeroU16
impl<Context> Decode<Context> for NonZeroU16
Source§impl<Context> Decode<Context> for NonZeroU32
impl<Context> Decode<Context> for NonZeroU32
Source§impl<Context> Decode<Context> for NonZeroU64
impl<Context> Decode<Context> for NonZeroU64
Source§impl<Context> Decode<Context> for NonZeroU128
impl<Context> Decode<Context> for NonZeroU128
Source§impl<Context> Decode<Context> for NonZeroUsize
impl<Context> Decode<Context> for NonZeroUsize
Source§impl<Context, A, B, C, D> Decode<Context> for (A, B, C, D)
impl<Context, A, B, C, D> Decode<Context> for (A, B, C, D)
Source§impl<Context, A, B, C, D, E> Decode<Context> for (A, B, C, D, E)
impl<Context, A, B, C, D, E> Decode<Context> for (A, B, C, D, E)
Source§impl<Context, A, B, C, D, E, F> Decode<Context> for (A, B, C, D, E, F)
impl<Context, A, B, C, D, E, F> Decode<Context> for (A, B, C, D, E, F)
Source§impl<Context, A, B, C, D, E, F, G> Decode<Context> for (A, B, C, D, E, F, G)
impl<Context, A, B, C, D, E, F, G> Decode<Context> for (A, B, C, D, E, F, G)
Source§impl<Context, A, B, C, D, E, F, G, H> Decode<Context> for (A, B, C, D, E, F, G, H)
impl<Context, A, B, C, D, E, F, G, H> Decode<Context> for (A, B, C, D, E, F, G, H)
Source§impl<Context, A, B, C, D, E, F, G, H, I> Decode<Context> for (A, B, C, D, E, F, G, H, I)
impl<Context, A, B, C, D, E, F, G, H, I> Decode<Context> for (A, B, C, D, E, F, G, H, I)
Source§impl<Context, A, B, C, D, E, F, G, H, I, J> Decode<Context> for (A, B, C, D, E, F, G, H, I, J)
impl<Context, A, B, C, D, E, F, G, H, I, J> Decode<Context> for (A, B, C, D, E, F, G, H, I, J)
Source§impl<Context, A, B, C, D, E, F, G, H, I, J, K> Decode<Context> for (A, B, C, D, E, F, G, H, I, J, K)
impl<Context, A, B, C, D, E, F, G, H, I, J, K> Decode<Context> for (A, B, C, D, E, F, G, H, I, J, K)
Source§impl<Context, A, B, C, D, E, F, G, H, I, J, K, L> Decode<Context> for (A, B, C, D, E, F, G, H, I, J, K, L)
impl<Context, A, B, C, D, E, F, G, H, I, J, K, L> Decode<Context> for (A, B, C, D, E, F, G, H, I, J, K, L)
Source§impl<Context, A, B, C, D, E, F, G, H, I, J, K, L, M> Decode<Context> for (A, B, C, D, E, F, G, H, I, J, K, L, M)
impl<Context, A, B, C, D, E, F, G, H, I, J, K, L, M> Decode<Context> for (A, B, C, D, E, F, G, H, I, J, K, L, M)
Source§impl<Context, A, B, C, D, E, F, G, H, I, J, K, L, M, N> Decode<Context> for (A, B, C, D, E, F, G, H, I, J, K, L, M, N)
impl<Context, A, B, C, D, E, F, G, H, I, J, K, L, M, N> Decode<Context> for (A, B, C, D, E, F, G, H, I, J, K, L, M, N)
Source§impl<Context, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O> Decode<Context> for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O)where
A: Decode<Context>,
B: Decode<Context>,
C: Decode<Context>,
D: Decode<Context>,
E: Decode<Context>,
F: Decode<Context>,
G: Decode<Context>,
H: Decode<Context>,
I: Decode<Context>,
J: Decode<Context>,
K: Decode<Context>,
L: Decode<Context>,
M: Decode<Context>,
N: Decode<Context>,
O: Decode<Context>,
impl<Context, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O> Decode<Context> for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O)where
A: Decode<Context>,
B: Decode<Context>,
C: Decode<Context>,
D: Decode<Context>,
E: Decode<Context>,
F: Decode<Context>,
G: Decode<Context>,
H: Decode<Context>,
I: Decode<Context>,
J: Decode<Context>,
K: Decode<Context>,
L: Decode<Context>,
M: Decode<Context>,
N: Decode<Context>,
O: Decode<Context>,
Source§impl<Context, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P> Decode<Context> for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P)where
A: Decode<Context>,
B: Decode<Context>,
C: Decode<Context>,
D: Decode<Context>,
E: Decode<Context>,
F: Decode<Context>,
G: Decode<Context>,
H: Decode<Context>,
I: Decode<Context>,
J: Decode<Context>,
K: Decode<Context>,
L: Decode<Context>,
M: Decode<Context>,
N: Decode<Context>,
O: Decode<Context>,
P: Decode<Context>,
impl<Context, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P> Decode<Context> for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P)where
A: Decode<Context>,
B: Decode<Context>,
C: Decode<Context>,
D: Decode<Context>,
E: Decode<Context>,
F: Decode<Context>,
G: Decode<Context>,
H: Decode<Context>,
I: Decode<Context>,
J: Decode<Context>,
K: Decode<Context>,
L: Decode<Context>,
M: Decode<Context>,
N: Decode<Context>,
O: Decode<Context>,
P: Decode<Context>,
Source§impl<Context, T> Decode<Context> for BinaryHeap<T>
impl<Context, T> Decode<Context> for BinaryHeap<T>
Source§impl<Context, T> Decode<Context> for Arc<[T]>where
T: Decode<Context> + 'static,
Available on target_has_atomic=ptr only.
impl<Context, T> Decode<Context> for Arc<[T]>where
T: Decode<Context> + 'static,
target_has_atomic=ptr only.Source§impl<Context, T> Decode<Context> for Arc<T>where
T: Decode<Context>,
Available on target_has_atomic=ptr only.
impl<Context, T> Decode<Context> for Arc<T>where
T: Decode<Context>,
target_has_atomic=ptr only.