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/
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
sourceimpl Decode for AtomicBool
impl Decode for AtomicBool
sourceimpl Decode for AtomicUsize
impl Decode for AtomicUsize
sourceimpl Decode for AtomicIsize
impl Decode for AtomicIsize
sourceimpl<T> Decode for BinaryHeap<T>where
T: Decode + Ord,
impl<T> Decode for BinaryHeap<T>where
T: Decode + Ord,
sourceimpl<'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,
sourceimpl Decode for SystemTime
impl Decode for SystemTime
sourceimpl Decode for SocketAddr
impl Decode for SocketAddr
sourceimpl Decode for SocketAddrV4
impl Decode for SocketAddrV4
sourceimpl Decode for SocketAddrV6
impl Decode for SocketAddrV6
sourceimpl<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,
sourceimpl<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,
sourceimpl<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,
sourceimpl<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,
sourceimpl<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,
sourceimpl<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,
sourceimpl<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,
sourceimpl<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,
sourceimpl<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,
sourceimpl<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,
sourceimpl<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,
sourceimpl<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,
sourceimpl<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,
sourceimpl Decode for NonZeroU16
impl Decode for NonZeroU16
sourceimpl Decode for NonZeroU32
impl Decode for NonZeroU32
sourceimpl Decode for NonZeroU64
impl Decode for NonZeroU64
sourceimpl Decode for NonZeroU128
impl Decode for NonZeroU128
sourceimpl Decode for NonZeroUsize
impl Decode for NonZeroUsize
sourceimpl Decode for NonZeroI16
impl Decode for NonZeroI16
sourceimpl Decode for NonZeroI32
impl Decode for NonZeroI32
sourceimpl Decode for NonZeroI64
impl Decode for NonZeroI64
sourceimpl Decode for NonZeroI128
impl Decode for NonZeroI128
sourceimpl Decode for NonZeroIsize
impl Decode for NonZeroIsize
sourceimpl<T> Decode for PhantomData<T>
impl<T> Decode for PhantomData<T>
sourceimpl<T> Decode for RangeInclusive<T>where
T: Decode,
impl<T> Decode for RangeInclusive<T>where
T: Decode,
Implementors
impl<T> Decode for Compat<T>where
T: DeserializeOwned,
serde
only.