pub trait Decode<Context>: Sized {
// Required method
fn decode<D>(decoder: &mut D) -> Result<Self, DecodeError>
where D: Decoder<Context = Context>;
}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
fn decode<D>(decoder: &mut D) -> Result<SocketAddr, DecodeError>where
D: Decoder<Context = Context>,
Source§impl<Context> Decode<Context> for SocketAddrV4
impl<Context> Decode<Context> for SocketAddrV4
fn decode<D>(decoder: &mut D) -> Result<SocketAddrV4, DecodeError>where
D: Decoder<Context = Context>,
Source§impl<Context> Decode<Context> for SocketAddrV6
impl<Context> Decode<Context> for SocketAddrV6
fn decode<D>(decoder: &mut D) -> Result<SocketAddrV6, DecodeError>where
D: Decoder<Context = Context>,
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.fn decode<D>(decoder: &mut D) -> Result<AtomicBool, DecodeError>where
D: Decoder,
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.fn decode<D>(decoder: &mut D) -> Result<AtomicIsize, DecodeError>where
D: Decoder,
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.fn decode<D>(decoder: &mut D) -> Result<AtomicUsize, DecodeError>where
D: Decoder,
Source§impl<Context> Decode<Context> for SystemTime
impl<Context> Decode<Context> for SystemTime
fn decode<D>(decoder: &mut D) -> Result<SystemTime, DecodeError>where
D: Decoder<Context = Context>,
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)
fn decode<DE>(decoder: &mut DE) -> Result<(A, B, C, D), DecodeError>where
DE: Decoder<Context = Context>,
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)
fn decode<DE>(decoder: &mut DE) -> Result<(A, B, C, D, E), DecodeError>where
DE: Decoder<Context = Context>,
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)
fn decode<DE>(decoder: &mut DE) -> Result<(A, B, C, D, E, F), DecodeError>where
DE: Decoder<Context = Context>,
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)
fn decode<DE>(decoder: &mut DE) -> Result<(A, B, C, D, E, F, G), DecodeError>where
DE: Decoder<Context = Context>,
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)
fn decode<DE>(decoder: &mut DE) -> Result<(A, B, C, D, E, F, G, H), DecodeError>where
DE: Decoder<Context = Context>,
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)
fn decode<DE>(
decoder: &mut DE,
) -> Result<(A, B, C, D, E, F, G, H, I), DecodeError>where
DE: Decoder<Context = Context>,
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)
fn decode<DE>(
decoder: &mut DE,
) -> Result<(A, B, C, D, E, F, G, H, I, J), DecodeError>where
DE: Decoder<Context = Context>,
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)
fn decode<DE>(
decoder: &mut DE,
) -> Result<(A, B, C, D, E, F, G, H, I, J, K), DecodeError>where
DE: Decoder<Context = Context>,
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)
fn decode<DE>(
decoder: &mut DE,
) -> Result<(A, B, C, D, E, F, G, H, I, J, K, L), DecodeError>where
DE: Decoder<Context = Context>,
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)
fn decode<DE>(
decoder: &mut DE,
) -> Result<(A, B, C, D, E, F, G, H, I, J, K, L, M), DecodeError>where
DE: Decoder<Context = Context>,
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)
fn decode<DE>(
decoder: &mut DE,
) -> Result<(A, B, C, D, E, F, G, H, I, J, K, L, M, N), DecodeError>where
DE: Decoder<Context = Context>,
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>,
fn decode<DE>(
decoder: &mut DE,
) -> Result<(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O), DecodeError>where
DE: Decoder<Context = 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>,
fn decode<DE>(
decoder: &mut DE,
) -> Result<(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P), DecodeError>where
DE: Decoder<Context = Context>,
Source§impl<Context, T> Decode<Context> for BinaryHeap<T>
impl<Context, T> Decode<Context> for BinaryHeap<T>
fn decode<D>(decoder: &mut D) -> Result<BinaryHeap<T>, DecodeError>where
D: Decoder<Context = Context>,
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.