CborBuilder

Struct CborBuilder 

Source
pub struct CborBuilder<'a, O: CborOutput> { /* private fields */ }
Expand description

Builder for a single CBOR value.

CborOwned::canonical uses the default configuration, which implies writing bytes into a fresh Vec<u8> and finally moving them into a SmallVec. You can minimise allocations by reusing the build buffer, and you can influence whether definite or indefinite length encoding is used for arrays and dictionaries.

§Example

use cbor_data::{Cbor, CborBuilder, Writer};

// this could come from a thread-local in real code:
let mut build_buffer = Vec::new();
// buffer will be cleared before use
build_buffer.extend_from_slice(b"some garbage");

let bytes_from_elsewhere = [0x82, 1, 2];
assert_eq!(Cbor::checked(&bytes_from_elsewhere).unwrap().to_string(), "[1, 2]");

let cbor = CborBuilder::with_scratch_space(&mut build_buffer)
    .with_max_definite_size(Some(1))
    .write_canonical(bytes_from_elsewhere.as_ref())
    .unwrap();

// now it is using indefinite-length encoding, since the array has more than 1 item
assert_eq!(cbor.to_string(), "[_ 1, 2]");
assert_eq!(cbor.as_slice(), [0x9f, 1, 2, 0xff]);

Implementations§

Source§

impl<'a> CborBuilder<'a, WithOutput>

Source

pub fn new() -> Self

Create a builder that writes into its own fresh vector.

Source

pub fn with_scratch_space(v: &'a mut Vec<u8>) -> Self

Create a builder that clears the given vector and writes into it.

You can use this to reuse a scratch space across multiple values being built, e.g. by keeping the same vector in a thread-local variable.

Source§

impl<'a> CborBuilder<'a, NoOutput>

Source

pub fn append_to(v: &'a mut Vec<u8>) -> Self

Append the CBOR bytes to the given vector and do not return a separate output value.

let mut v = Vec::new();
let result: () = CborBuilder::append_to(&mut v).write_pos(12, None);

assert_eq!(v, vec![12u8])
Source§

impl<'a, O: CborOutput> CborBuilder<'a, O>

Source

pub fn with_max_definite_size(self, max_definite: Option<u64>) -> Self

Configure the limit above which indefinite size encoding will be used.

The default is 255, which is the largest size up to which definite size is at least as compact as indefinite size. Set to 23 to avoid moving bytes around when finishing the array. Set to None to always use indefinite size encoding.

Trait Implementations§

Source§

impl Default for CborBuilder<'static, WithOutput>

Source§

fn default() -> Self

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

impl<'a, O: CborOutput> Writer for CborBuilder<'a, O>

Source§

type Output = <O as CborOutput>::Output

Source§

fn max_definite(&self) -> Option<u64>

Configured maximum array or dict length up to which definite size encoding is used.
Source§

fn set_max_definite(&mut self, max: Option<u64>)

Set the maximum array or dict length up to which definite size encoding is used.
Source§

fn write_pos( self, value: u64, tags: impl IntoIterator<Item = u64>, ) -> Self::Output

Write a unsigned value of up to 64 bits. Tags are from outer to inner.
Source§

fn write_neg( self, value: u64, tags: impl IntoIterator<Item = u64>, ) -> Self::Output

Write a negative value of up to 64 bits — the represented number is -1 - value. Tags are from outer to inner.
Source§

fn write_bytes( self, value: &[u8], tags: impl IntoIterator<Item = u64>, ) -> Self::Output

Write the given slice as a definite size byte string. Tags are from outer to inner.
Source§

fn write_bytes_chunked( self, value: impl IntoIterator<Item = impl AsRef<[u8]>> + Copy, tags: impl IntoIterator<Item = u64>, ) -> Self::Output

Write the given slices as a definite size byte string. Tags are from outer to inner. Read more
Source§

fn write_str( self, value: &str, tags: impl IntoIterator<Item = u64>, ) -> Self::Output

Write the given slice as a definite size string. Tags are from outer to inner.
Source§

fn write_str_chunked( self, value: impl IntoIterator<Item = impl AsRef<str>> + Copy, tags: impl IntoIterator<Item = u64>, ) -> Self::Output

Write the given slice as a definite size string. Tags are from outer to inner. Read more
Source§

fn write_bool( self, value: bool, tags: impl IntoIterator<Item = u64>, ) -> Self::Output

Tags are from outer to inner.
Source§

fn write_null(self, tags: impl IntoIterator<Item = u64>) -> Self::Output

Tags are from outer to inner.
Source§

fn write_undefined(self, tags: impl IntoIterator<Item = u64>) -> Self::Output

Tags are from outer to inner.
Source§

fn write_lit( self, value: Literal, tags: impl IntoIterator<Item = u64>, ) -> Self::Output

Write custom literal value — RFC 8949 §3.3 is required reading. Tags are from outer to inner.
Source§

fn write_array<F>( self, tags: impl IntoIterator<Item = u64>, f: F, ) -> Self::Output
where F: FnOnce(&mut ArrayWriter<'_>),

Write a nested array using the given closure that receives an array builder. Tags are from outer to inner. Read more
Source§

fn write_array_ret<T, F>( self, tags: impl IntoIterator<Item = u64>, f: F, ) -> (Self::Output, T)
where F: FnOnce(&mut ArrayWriter<'_>) -> T,

Write a nested array using the given closure that receives an array builder. Tags are from outer to inner. Read more
Source§

fn write_dict<F>( self, tags: impl IntoIterator<Item = u64>, f: F, ) -> Self::Output
where F: FnOnce(&mut DictWriter<'_>),

Write a nested dict using the given closure that receives a dict builder. Tags are from outer to inner. Read more
Source§

fn write_dict_ret<T, F>( self, tags: impl IntoIterator<Item = u64>, f: F, ) -> (Self::Output, T)
where F: FnOnce(&mut DictWriter<'_>) -> T,

Write a nested dict using the given closure that receives a dict builder. Tags are from outer to inner. Read more
Source§

fn write_canonical(self, bytes: &[u8]) -> Result<Self::Output, ParseError>

Interpret the given bytes as a single CBOR item and write it to this builder, canonicalising its contents like CborOwned::canonical()
Source§

fn write_trusting(self, bytes: &[u8]) -> Self::Output

Assume that the given bytes are a well-formed single CBOR item and write it to this builder. Read more
Source§

fn write_item(self, item: &Cbor) -> Self::Output

Write the given CBOR item

Auto Trait Implementations§

§

impl<'a, O> Freeze for CborBuilder<'a, O>

§

impl<'a, O> RefUnwindSafe for CborBuilder<'a, O>
where O: RefUnwindSafe,

§

impl<'a, O> Send for CborBuilder<'a, O>
where O: Send,

§

impl<'a, O> Sync for CborBuilder<'a, O>
where O: Sync,

§

impl<'a, O> Unpin for CborBuilder<'a, O>
where O: Unpin,

§

impl<'a, O> !UnwindSafe for CborBuilder<'a, O>

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> Encoder for T
where T: Writer,

Source§

fn encode_undefined(self) -> Self::Output

Source§

fn encode_null(self) -> Self::Output

Source§

fn encode_bool(self, value: bool) -> Self::Output

Source§

fn encode_u64(self, value: u64) -> Self::Output

Encode an unsigned integer of at most 64 bit. Read more
Source§

fn encode_i64(self, value: i64) -> Self::Output

Encode a signed integer of at most 64 bit. Read more
Source§

fn encode_f64(self, value: f64) -> Self::Output

Encode a floating-point number of at most 64 bit. Read more
Source§

fn encode_timestamp( self, timestamp: Timestamp, precision: Precision, ) -> Self::Output

Encode a timestamp with given target precision Read more
Source§

fn encode_number(self, number: &Number<'_>) -> Self::Output

Encode a possibly big number Read more
Source§

fn encode_str(self, value: impl AsRef<str>) -> Self::Output

Encode a string. Read more
Source§

fn encode_bytes(self, value: impl AsRef<[u8]>) -> Self::Output

Encode a byte string. Read more
Source§

fn encode_array<F>(self, f: F) -> Self::Output
where F: FnOnce(&mut ArrayWriter<'_>),

Write an array that is then filled by the provided closure using the passed builder. Read more
Source§

fn encode_dict<F>(self, f: F) -> Self::Output
where F: FnOnce(&mut DictWriter<'_>),

Write a dict that is then filled by the provided closure using the passed builder. 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, 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.