diff --git a/src/de/mod.rs b/src/de/mod.rs index 95b28ba..05d4747 100644 --- a/src/de/mod.rs +++ b/src/de/mod.rs @@ -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(decoder: D) -> Result; @@ -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>(decoder: D) -> Result; diff --git a/src/enc/encoder.rs b/src/enc/encoder.rs index 58a8605..f2c83f9 100644 --- a/src/enc/encoder.rs +++ b/src/enc/encoder.rs @@ -1,5 +1,3 @@ -//! Contains - use super::{write::Writer, Encoder}; use crate::{config::Config, utils::Sealed}; diff --git a/src/enc/mod.rs b/src/enc/mod.rs index bf989b1..6f58012 100644 --- a/src/enc/mod.rs +++ b/src/enc/mod.rs @@ -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(&self, encoder: E) -> Result<(), EncodeError>; diff --git a/src/lib.rs b/src/lib.rs index 6b40163..adae6b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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"]