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

Create a builder that writes into its own fresh vector.

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.

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])

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

Returns the “default value” for a type. Read more
Configured maximum array or dict length up to which definite size encoding is used.
Write a unsigned value of up to 64 bits. Tags are from outer to inner. Read more
Write a negative value of up to 64 bits — the represented number is -1 - value. Tags are from outer to inner. Read more
Write the given slice as a definite size byte string. Tags are from outer to inner. Read more
Write the given slices as a definite size byte string. Tags are from outer to inner. Read more
Write the given slice as a definite size string. Tags are from outer to inner. Read more
Write the given slice as a definite size string. Tags are from outer to inner. Read more
Tags are from outer to inner.
Tags are from outer to inner.
Tags are from outer to inner.
Write custom literal value — RFC 8949 §3.3 is required reading. Tags are from outer to inner. Read more
Write a nested array using the given closure that receives an array builder. Tags are from outer to inner. Read more
Write a nested array using the given closure that receives an array builder. Tags are from outer to inner. Read more
Write a nested dict using the given closure that receives a dict builder. Tags are from outer to inner. Read more
Write a nested dict using the given closure that receives a dict builder. Tags are from outer to inner. Read more
Interpret the given bytes as a single CBOR item and write it to this builder, canonicalising its contents like CborOwned::canonical() Read more
Assume that the given bytes are a well-formed single CBOR item and write it to this builder. Read more
Write the given CBOR item

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.