Struct cbor_data::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
sourceimpl<'a> CborBuilder<'a, WithOutput>
impl<'a> CborBuilder<'a, WithOutput>
sourcepub fn with_scratch_space(v: &'a mut Vec<u8>) -> Self
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.
sourceimpl<'a> CborBuilder<'a, NoOutput>
impl<'a> CborBuilder<'a, NoOutput>
sourceimpl<'a, O: CborOutput> CborBuilder<'a, O>
impl<'a, O: CborOutput> CborBuilder<'a, O>
sourcepub fn with_max_definite_size(self, max_definite: Option<u64>) -> Self
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
sourceimpl Default for CborBuilder<'static, WithOutput>
impl Default for CborBuilder<'static, WithOutput>
sourceimpl<'a, O: CborOutput> Writer for CborBuilder<'a, O>
impl<'a, O: CborOutput> Writer for CborBuilder<'a, O>
type Output = <O as CborOutput>::Output
sourcefn max_definite(&self) -> Option<u64>
fn max_definite(&self) -> Option<u64>
sourcefn write_pos(
self,
value: u64,
tags: impl IntoIterator<Item = u64>
) -> Self::Output
fn write_pos(
self,
value: u64,
tags: impl IntoIterator<Item = u64>
) -> Self::Output
sourcefn write_neg(
self,
value: u64,
tags: impl IntoIterator<Item = u64>
) -> Self::Output
fn write_neg(
self,
value: u64,
tags: impl IntoIterator<Item = u64>
) -> Self::Output
-1 - value.
Tags are from outer to inner. Read moresourcefn write_bytes(
self,
value: &[u8],
tags: impl IntoIterator<Item = u64>
) -> Self::Output
fn write_bytes(
self,
value: &[u8],
tags: impl IntoIterator<Item = u64>
) -> Self::Output
sourcefn write_bytes_chunked(
self,
value: impl IntoIterator<Item = impl AsRef<[u8]>> + Copy,
tags: impl IntoIterator<Item = u64>
) -> Self::Output
fn write_bytes_chunked(
self,
value: impl IntoIterator<Item = impl AsRef<[u8]>> + Copy,
tags: impl IntoIterator<Item = u64>
) -> Self::Output
sourcefn write_str(
self,
value: &str,
tags: impl IntoIterator<Item = u64>
) -> Self::Output
fn write_str(
self,
value: &str,
tags: impl IntoIterator<Item = u64>
) -> Self::Output
sourcefn write_str_chunked(
self,
value: impl IntoIterator<Item = impl AsRef<str>> + Copy,
tags: impl IntoIterator<Item = u64>
) -> Self::Output
fn write_str_chunked(
self,
value: impl IntoIterator<Item = impl AsRef<str>> + Copy,
tags: impl IntoIterator<Item = u64>
) -> Self::Output
sourcefn write_bool(
self,
value: bool,
tags: impl IntoIterator<Item = u64>
) -> Self::Output
fn write_bool(
self,
value: bool,
tags: impl IntoIterator<Item = u64>
) -> Self::Output
sourcefn write_null(self, tags: impl IntoIterator<Item = u64>) -> Self::Output
fn write_null(self, tags: impl IntoIterator<Item = u64>) -> Self::Output
sourcefn write_undefined(self, tags: impl IntoIterator<Item = u64>) -> Self::Output
fn write_undefined(self, tags: impl IntoIterator<Item = u64>) -> Self::Output
sourcefn write_lit(
self,
value: Literal,
tags: impl IntoIterator<Item = u64>
) -> Self::Output
fn write_lit(
self,
value: Literal,
tags: impl IntoIterator<Item = u64>
) -> Self::Output
sourcefn write_array<F>(self, tags: impl IntoIterator<Item = u64>, f: F) -> Self::Outputwhere
F: FnOnce(&mut ArrayWriter<'_>),
fn write_array<F>(self, tags: impl IntoIterator<Item = u64>, f: F) -> Self::Outputwhere
F: FnOnce(&mut ArrayWriter<'_>),
sourcefn write_array_ret<T, F>(
self,
tags: impl IntoIterator<Item = u64>,
f: F
) -> (Self::Output, T)where
F: FnOnce(&mut ArrayWriter<'_>) -> T,
fn write_array_ret<T, F>(
self,
tags: impl IntoIterator<Item = u64>,
f: F
) -> (Self::Output, T)where
F: FnOnce(&mut ArrayWriter<'_>) -> T,
sourcefn write_dict<F>(self, tags: impl IntoIterator<Item = u64>, f: F) -> Self::Outputwhere
F: FnOnce(&mut DictWriter<'_>),
fn write_dict<F>(self, tags: impl IntoIterator<Item = u64>, f: F) -> Self::Outputwhere
F: FnOnce(&mut DictWriter<'_>),
sourcefn write_dict_ret<T, F>(
self,
tags: impl IntoIterator<Item = u64>,
f: F
) -> (Self::Output, T)where
F: FnOnce(&mut DictWriter<'_>) -> T,
fn write_dict_ret<T, F>(
self,
tags: impl IntoIterator<Item = u64>,
f: F
) -> (Self::Output, T)where
F: FnOnce(&mut DictWriter<'_>) -> T,
sourcefn write_canonical(self, bytes: &[u8]) -> Result<Self::Output, ParseError>
fn write_canonical(self, bytes: &[u8]) -> Result<Self::Output, ParseError>
CborOwned::canonical() Read more