Skip to content

Commit 45586f4

Browse files
committed
add some docs
1 parent 872e13f commit 45586f4

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/de/mod.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ pub use self::decoder::DecoderImpl;
2121

2222
/// Trait that makes a type able to be decoded, akin to serde's `DeserializeOwned` trait.
2323
///
24+
/// 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.
25+
///
2426
/// 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.
2527
///
2628
/// Whenever you implement `Decode` for your type, the base trait `BorrowDecode` is automatically implemented.
2729
///
28-
/// 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.
30+
/// 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.
2931
///
3032
/// # Implementing this trait manually
3133
///
@@ -85,6 +87,19 @@ pub use self::decoder::DecoderImpl;
8587
/// # }
8688
/// # bincode::impl_borrow_decode!(Foo);
8789
/// ```
90+
///
91+
/// You can use `Context` to require contexts for decoding a type:
92+
/// ```
93+
/// # /// # use bumpalo::Bump;
94+
/// use bincode::de::Decoder;
95+
/// use bincode::error::DecodeError;
96+
/// struct BytesInArena<'a>(bumpalo::collections::Vec<'a, u8>);
97+
/// impl<'a> bincode::Decode<&'a bumpalo::Bump> for BytesInArena<'a> {
98+
/// fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
99+
/// todo!()
100+
/// }
101+
/// # }
102+
/// ```
88103
pub trait Decode<Context>: Sized {
89104
/// Attempt to decode this type with the given [Decode].
90105
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError>;
@@ -137,8 +152,10 @@ pub trait Decoder: Sealed {
137152
/// The concrete [Config] type
138153
type C: Config;
139154

155+
/// The decoding context type
140156
type Context;
141157

158+
/// Returns the decoding context
142159
fn context(&mut self) -> &mut Self::Context;
143160

144161
fn with_context<'a, C>(&'a mut self, context: &'a mut C) -> WithContext<'a, Self, C> {

0 commit comments

Comments
 (0)