mirror of https://git.sr.ht/~stygianentity/bincode
Updated documentation of Encode, added an example to lib.rs
This commit is contained in:
parent
dae645f675
commit
bd994e354d
|
|
@ -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.
|
/// 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.
|
/// 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> {
|
pub trait Decode: for<'de> BorrowDecode<'de> {
|
||||||
/// Attempt to decode this type with the given [Decode].
|
/// Attempt to decode this type with the given [Decode].
|
||||||
fn decode<D: Decoder>(decoder: D) -> Result<Self, DecodeError>;
|
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.
|
/// 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 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 {
|
pub trait BorrowDecode<'de>: Sized {
|
||||||
/// Attempt to decode this type with the given [BorrowDecode].
|
/// Attempt to decode this type with the given [BorrowDecode].
|
||||||
fn borrow_decode<D: BorrowDecoder<'de>>(decoder: D) -> Result<Self, DecodeError>;
|
fn borrow_decode<D: BorrowDecoder<'de>>(decoder: D) -> Result<Self, DecodeError>;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
//! Contains
|
|
||||||
|
|
||||||
use super::{write::Writer, Encoder};
|
use super::{write::Writer, Encoder};
|
||||||
use crate::{config::Config, utils::Sealed};
|
use crate::{config::Config, utils::Sealed};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,9 @@ pub mod write;
|
||||||
|
|
||||||
pub use self::encoder::EncoderImpl;
|
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 {
|
pub trait Encode {
|
||||||
/// Encode a given type.
|
/// Encode a given type.
|
||||||
fn encode<E: Encoder>(&self, encoder: E) -> Result<(), EncodeError>;
|
fn encode<E: Encoder>(&self, encoder: E) -> Result<(), EncodeError>;
|
||||||
|
|
|
||||||
33
src/lib.rs
33
src/lib.rs
|
|
@ -20,6 +20,39 @@
|
||||||
//! |atomic| Yes |All `Atomic*` integer types, e.g. `AtomicUsize`, and `AtomicBool`||
|
//! |atomic| Yes |All `Atomic*` integer types, e.g. `AtomicUsize`, and `AtomicBool`||
|
||||||
//! |derive| Yes |||Enables the `Encode` and `Decode` derive macro|
|
//! |derive| Yes |||Enables the `Encode` and `Decode` derive macro|
|
||||||
//! |serde | No |TODO|TODO|TODO|
|
//! |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")]
|
#![doc(html_root_url = "https://docs.rs/bincode/2.0.0-alpha.0")]
|
||||||
#![crate_name = "bincode"]
|
#![crate_name = "bincode"]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue