Struct bitcode::Buffer

source ·
pub struct Buffer(_);
Expand description

A buffer for reusing allocations between any number of calls to Buffer::encode and/or Buffer::decode.

Usage

use bitcode::Buffer;

// We preallocate buffers with capacity 1000. This will allow us to encode and decode without
// any allocations as long as the encoded object takes less than 1000 bytes.
let bytes = 1000;
let mut encode_buf = Buffer::with_capacity(bytes);
let mut decode_buf = Buffer::with_capacity(bytes);

// The object that we will encode.
let target: [u8; 5] = [1, 2, 3, 4, 5];

// We encode into `encode_buf`. This won't cause any allocations.
let encoded: &[u8] = encode_buf.encode(&target).unwrap();
assert!(encoded.len() <= bytes, "oh no we allocated");

// We decode into `decode_buf` because `encoded` is borrowing `encode_buf`.
let decoded: [u8; 5] = decode_buf.decode(&encoded).unwrap();
assert_eq!(target, decoded);

// If we need ownership of `encoded`, we can convert it to a vec.
// This will allocate, but it's still more efficient than calling bitcode::encode.
let _owned: Vec<u8> = encoded.to_vec();

Implementations§

source§

impl Buffer

source

pub fn new() -> Self

Constructs a new buffer without any capacity.

source

pub fn with_capacity(capacity: usize) -> Self

Constructs a new buffer with at least the specified capacity in bytes.

source§

impl Buffer

source

pub fn serialize<T>(&mut self, t: &T) -> Result<&[u8], Error>where T: Serialize + ?Sized,

Available on crate feature serde only.

Serializes a T: Serialize into a &[u8]. Can reuse the buffer’s allocations.

Even if you call to_vec on the &[u8], it’s still more efficient than serialize.

Warning: The format is incompatible with decode and subject to change between versions.

source

pub fn deserialize<T>(&mut self, bytes: &[u8]) -> Result<T, Error>where T: DeserializeOwned,

Available on crate feature serde only.

Deserializes a &[u8] into an instance of T: Deserialize. Can reuse the buffer’s allocations.

Warning: The format is incompatible with encode and subject to change between versions.

source§

impl Buffer

source

pub fn encode<T>(&mut self, t: &T) -> Result<&[u8], Error>where T: Encode + ?Sized,

Encodes a T: Encode into a &[u8]. Can reuse the buffer’s allocations.

Won’t ever return Err unless using #[bitcode(with_serde)].

Even if you call to_vec on the &[u8], it’s still more efficient than encode.

Warning: The format is subject to change between versions.

source

pub fn decode<T>(&mut self, bytes: &[u8]) -> Result<T, Error>where T: Decode,

Decodes a &[u8] into an instance of T: Decode. Can reuse the buffer’s allocations.

Warning: The format is subject to change between versions.

Trait Implementations§

source§

impl Default for Buffer

source§

fn default() -> Buffer

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.