Updated documentation of Encode, added an example to lib.rs

This commit is contained in:
Victor Koenders 2021-10-21 14:45:16 +02:00
parent dae645f675
commit bd994e354d
4 changed files with 39 additions and 4 deletions

View File

@ -17,6 +17,8 @@ pub use self::decoder::DecoderImpl;
/// 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.
pub trait Decode: for<'de> BorrowDecode<'de> {
/// Attempt to decode this type with the given [Decode].
fn decode<D: Decoder>(decoder: D) -> Result<Self, DecodeError>;
@ -25,6 +27,8 @@ pub trait Decode: for<'de> BorrowDecode<'de> {
/// Trait that makes a type able to be decoded, akin to serde's `Deserialize` trait.
///
/// This trait should be implemented for types that contain borrowed data, like `&str` and `&[u8]`. If your type does not have borrowed data, consider implementing [Decode] instead.
///
/// This trait will be automatically implemented if you enable the `derive` feature and add `#[derive(bincode::Decode)]` to a type with a lifetime.
pub trait BorrowDecode<'de>: Sized {
/// Attempt to decode this type with the given [BorrowDecode].
fn borrow_decode<D: BorrowDecoder<'de>>(decoder: D) -> Result<Self, DecodeError>;

View File

@ -1,5 +1,3 @@
//! Contains
use super::{write::Writer, Encoder};
use crate::{config::Config, utils::Sealed};

View File

@ -11,9 +11,9 @@ pub mod write;
pub use self::encoder::EncoderImpl;
/// Any source that can encode types. This type is most notably implemented for [Encoder].
/// Any source that can be encoded. This trait should be implemented for all types that you want to be able to use with any of the `encode_with` methods.
///
/// [Encoder]: ../struct.Encoder.html
/// This trait will be automatically implemented if you enable the `derive` feature and add `#[derive(bincode::Encode)]` to your trait.
pub trait Encode {
/// Encode a given type.
fn encode<E: Encoder>(&self, encoder: E) -> Result<(), EncodeError>;

View File

@ -20,6 +20,39 @@
//! |atomic| Yes |All `Atomic*` integer types, e.g. `AtomicUsize`, and `AtomicBool`||
//! |derive| Yes |||Enables the `Encode` and `Decode` derive macro|
//! |serde | No |TODO|TODO|TODO|
//!
//! # Example
//!
//! ```rust
//! use bincode::config::Configuration;
//!
//! let mut slice = [0u8; 100];
//!
//! // You can encode any type that implements `enc::Encode`.
//! // You can automatically implement this trait on custom types with the `derive` feature.
//! let input = (
//! 0u8,
//! 10u32,
//! 10000i128,
//! 'a',
//! [0u8, 1u8, 2u8, 3u8]
//! );
//!
//! let length = bincode::encode_into_slice(
//! input,
//! &mut slice,
//! Configuration::standard()
//! ).unwrap();
//!
//! let slice = &slice[..length];
//! println!("Bytes written: {:?}", slice);
//!
//! // Decoding works the same as encoding.
//! // The trait used is `de::Decode`, and can also be automatically implemented with the `derive` feature.
//! let decoded: (u8, u32, i128, char, [u8; 4]) = bincode::decode_from_slice(slice, Configuration::standard()).unwrap();
//!
//! assert_eq!(decoded, input);
//! ```
#![doc(html_root_url = "https://docs.rs/bincode/2.0.0-alpha.0")]
#![crate_name = "bincode"]