Skip to main content

Encoder

Struct Encoder 

Source
pub struct Encoder { /* private fields */ }
Available on crate feature alloc only.
Expand description

Streaming encoder that writes canonical CBOR directly into a Vec<u8>.

This supports splicing validated canonical bytes.

Implementations§

Source§

impl Encoder

Source

pub const fn new() -> Self

Create a new canonical encoder.

Source

pub fn with_capacity(capacity: usize) -> Self

Create a canonical encoder with pre-allocated capacity.

Source

pub fn len(&self) -> usize

Return the number of bytes written so far.

Source

pub fn is_empty(&self) -> bool

Returns true if no bytes have been written.

Source

pub fn into_vec(self) -> Vec<u8>

Consume and return the encoded bytes.

Source

pub fn into_canonical(self) -> Result<CanonicalCbor, CborError>

Consume and return canonical bytes as a CanonicalCbor.

§Errors

Returns an error if the buffer does not contain exactly one canonical CBOR item.

Source

pub fn clear(&mut self)

Clear the encoder while retaining allocated capacity.

Source

pub fn as_bytes(&self) -> &[u8]

Borrow the bytes emitted so far.

Source

pub fn null(&mut self) -> Result<(), CborError>

Encode CBOR null.

§Errors

Returns an error if writing to the underlying buffer fails.

Source

pub fn bool(&mut self, v: bool) -> Result<(), CborError>

Encode a CBOR boolean.

§Errors

Returns an error if writing to the underlying buffer fails.

Source

pub fn int(&mut self, v: i64) -> Result<(), CborError>

Encode a safe-range integer.

§Errors

Returns an error if the integer is outside the safe range or if encoding fails.

Source

pub fn int_u128(&mut self, v: u128) -> Result<(), CborError>

Encode an unsigned integer, using a bignum when outside the safe range.

§Errors

Returns an error if encoding fails or allocation for the bignum magnitude fails.

Source

pub fn int_i128(&mut self, v: i128) -> Result<(), CborError>

Encode a signed integer, using a bignum when outside the safe range.

§Errors

Returns an error if encoding fails or allocation for the bignum magnitude fails.

Source

pub fn bignum( &mut self, negative: bool, magnitude: &[u8], ) -> Result<(), CborError>

Encode a CBOR bignum (tag 2/3 + byte string magnitude).

§Errors

Returns an error if the magnitude is not canonical or if encoding fails.

Source

pub fn bytes(&mut self, b: &[u8]) -> Result<(), CborError>

Encode a byte string.

§Errors

Returns an error if encoding fails.

Source

pub fn text(&mut self, s: &str) -> Result<(), CborError>

Encode a text string.

§Errors

Returns an error if encoding fails.

Source

pub fn float(&mut self, bits: F64Bits) -> Result<(), CborError>

Encode a float64 bit pattern.

§Errors

Returns an error if encoding fails.

Source

pub fn raw_cbor(&mut self, v: CanonicalCborRef<'_>) -> Result<(), CborError>

Splice already validated canonical CBOR bytes as the next value.

§Errors

Returns an error if writing to the underlying buffer fails.

Source

pub fn raw_value_ref(&mut self, v: CborValueRef<'_>) -> Result<(), CborError>

Splice a canonical sub-value reference.

§Errors

Returns an error if writing to the underlying buffer fails.

Source

pub fn array<F>(&mut self, len: usize, f: F) -> Result<(), CborError>
where F: FnOnce(&mut ArrayEncoder<'_>) -> Result<(), CborError>,

Encode a definite-length array and fill it via the provided builder.

§Errors

Returns an error if encoding fails or if the builder emits a different number of items.

Source

pub fn map<F>(&mut self, len: usize, f: F) -> Result<(), CborError>
where F: FnOnce(&mut MapEncoder<'_>) -> Result<(), CborError>,

Encode a definite-length map and fill it via the provided builder.

§Errors

Returns an error if encoding fails or if the builder emits a different number of entries.

Trait Implementations§

Source§

impl Default for Encoder

Source§

fn default() -> Self

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

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.