pub trait Writer: Sized {
type Output;
Show 19 methods
// Required methods
fn max_definite(&self) -> Option<u64>;
fn set_max_definite(&mut self, max: Option<u64>);
// Provided methods
fn write_pos(
self,
value: u64,
tags: impl IntoIterator<Item = u64>
) -> Self::Output { ... }
fn write_neg(
self,
value: u64,
tags: impl IntoIterator<Item = u64>
) -> Self::Output { ... }
fn write_bytes(
self,
value: &[u8],
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 { ... }
fn write_str(
self,
value: &str,
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 { ... }
fn write_bool(
self,
value: bool,
tags: impl IntoIterator<Item = u64>
) -> Self::Output { ... }
fn write_null(self, tags: impl IntoIterator<Item = u64>) -> Self::Output { ... }
fn write_undefined(
self,
tags: impl IntoIterator<Item = u64>
) -> Self::Output { ... }
fn write_lit(
self,
value: Literal,
tags: impl IntoIterator<Item = u64>
) -> Self::Output { ... }
fn write_array<F>(
self,
tags: impl IntoIterator<Item = u64>,
f: F
) -> Self::Output
where F: FnOnce(&mut ArrayWriter<'_>) { ... }
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_dict<F>(
self,
tags: impl IntoIterator<Item = u64>,
f: F
) -> Self::Output
where F: FnOnce(&mut DictWriter<'_>) { ... }
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_canonical(self, bytes: &[u8]) -> Result<Self::Output, ParseError> { ... }
fn write_trusting(self, bytes: &[u8]) -> Self::Output { ... }
fn write_item(self, item: &Cbor) -> Self::Output { ... }
}
Expand description
Low-level primitives for emitting CBOR items.
The methods of this trait give you full control over the encoding of values according to the CBOR specification (apart from the technically allowed non-optimal integer encodings). It allows you to emit any item tagged with any number you desire.
If you are looking for convenient methods of writing end-user data types please refer to
the Encoder
trait.
Required Associated Types§
Required Methods§
sourcefn max_definite(&self) -> Option<u64>
fn max_definite(&self) -> Option<u64>
Configured maximum array or dict length up to which definite size encoding is used.
sourcefn set_max_definite(&mut self, max: Option<u64>)
fn set_max_definite(&mut self, max: Option<u64>)
Set the maximum array or dict length up to which definite size encoding is used.
Provided Methods§
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
Write a unsigned value of up to 64 bits. Tags are from outer to inner.
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
Write a negative value of up to 64 bits — the represented number is -1 - value
.
Tags are from outer to inner.
sourcefn 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
Write the given slice as a definite size byte string. Tags are from outer to inner.
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
Write the given slices as a definite size byte string. Tags are from outer to inner.
Example:
let cbor = CborBuilder::default().write_bytes_chunked([&[0][..], &[1, 2][..]], [12]);
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
Write the given slice as a definite size string. Tags are from outer to inner.
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
Write the given slice as a definite size string. Tags are from outer to inner.
Example:
let cbor = CborBuilder::default().write_str_chunked(["a", "b"], [12]);
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
Tags are from outer to inner.
sourcefn write_null(self, tags: impl IntoIterator<Item = u64>) -> Self::Output
fn write_null(self, tags: impl IntoIterator<Item = u64>) -> Self::Output
Tags are from outer to inner.
sourcefn write_undefined(self, tags: impl IntoIterator<Item = u64>) -> Self::Output
fn write_undefined(self, tags: impl IntoIterator<Item = u64>) -> Self::Output
Tags are from outer to inner.
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
Write custom literal value — RFC 8949 §3.3 is required reading. Tags are from outer to inner.
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<'_>),
Write a nested array using the given closure that receives an array builder. Tags are from outer to inner.
let cbor = CborBuilder::default().write_array(None, |builder| {
builder.write_array_ret(None, |builder| {
builder.write_pos(42, None);
});
});
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,
Write a nested array using the given closure that receives an array builder. Tags are from outer to inner.
let (cbor, ret) = CborBuilder::default().write_array_ret(None, |builder| {
builder.write_array_ret(None, |builder| {
builder.write_pos(42, None);
});
42
});
assert_eq!(ret, 42);
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<'_>),
Write a nested dict using the given closure that receives a dict builder. Tags are from outer to inner.
let cbor = CborBuilder::default().write_array(None, |builder | {
builder.write_dict_ret(None, |builder| {
builder.with_key("y", |b| b.write_pos(42, None));
});
});
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,
Write a nested dict using the given closure that receives a dict builder. Tags are from outer to inner.
let (cbor, ret) = CborBuilder::default().write_array_ret(None, |builder | {
builder.write_dict_ret(None, |builder| {
builder.with_key("y", |b| b.write_pos(42, None));
});
42
});
assert_eq!(ret, 42);
sourcefn write_canonical(self, bytes: &[u8]) -> Result<Self::Output, ParseError>
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()
sourcefn write_trusting(self, bytes: &[u8]) -> Self::Output
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.
If those bytes are not valid CBOR you get to keep the pieces!
sourcefn write_item(self, item: &Cbor) -> Self::Output
fn write_item(self, item: &Cbor) -> Self::Output
Write the given CBOR item