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>
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.
Source§impl<'a> CborBuilder<'a, NoOutput>
impl<'a> CborBuilder<'a, NoOutput>
Source§impl<'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§
Source§impl Default for CborBuilder<'static, WithOutput>
impl Default for CborBuilder<'static, WithOutput>
Source§impl<'a, O: CborOutput> Writer for CborBuilder<'a, O>
impl<'a, O: CborOutput> Writer for CborBuilder<'a, O>
type Output = <O as CborOutput>::Output
Source§fn max_definite(&self) -> Option<u64>
fn max_definite(&self) -> Option<u64>
Source§fn set_max_definite(&mut self, max: Option<u64>)
fn set_max_definite(&mut self, max: Option<u64>)
Source§fn 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
Source§fn 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.Source§fn 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
Source§fn 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
Source§fn 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
Source§fn 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
Source§fn 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
Source§fn write_null(self, tags: impl IntoIterator<Item = u64>) -> Self::Output
fn write_null(self, tags: impl IntoIterator<Item = u64>) -> Self::Output
Source§fn write_undefined(self, tags: impl IntoIterator<Item = u64>) -> Self::Output
fn write_undefined(self, tags: impl IntoIterator<Item = u64>) -> Self::Output
Source§fn 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
Source§fn 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<'_>),
Source§fn 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,
Source§fn 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<'_>),
Source§fn 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,
Source§fn write_canonical(self, bytes: &[u8]) -> Result<Self::Output, ParseError>
fn write_canonical(self, bytes: &[u8]) -> Result<Self::Output, ParseError>
CborOwned::canonical()