pub struct Encoder<'b> { /* private fields */ }Expand description
Serializes CBOR items into a fixed buffer.
On success each method advances the cursor by exactly the item’s
preferred-encoding length. On failure (Error::OUT_OF_SPACE) nothing
is written for that item, so the encoder stays valid and reusable.
Implementations§
Source§impl<'b> Encoder<'b>
impl<'b> Encoder<'b>
Sourcepub const fn new(buf: &'b mut [u8]) -> Self
pub const fn new(buf: &'b mut [u8]) -> Self
Creates an encoder writing into buf, starting at offset 0.
Sourcepub const fn finish(self) -> (&'b [u8], usize)
pub const fn finish(self) -> (&'b [u8], usize)
Consumes the encoder, returning the buffer and the bytes written.
Sourcepub fn uint(&mut self, v: u64) -> Result<&mut Self, Error>
pub fn uint(&mut self, v: u64) -> Result<&mut Self, Error>
Encodes any unsigned integer using its preferred width.
Prefer the typed helpers (u8, u16, …); they compile to
identical code and document the value’s range.
Sourcepub fn nint(&mut self, n: u64) -> Result<&mut Self, Error>
pub fn nint(&mut self, n: u64) -> Result<&mut Self, Error>
Encodes -1 - n for magnitude n (RFC 8949 §3.1).
Magnitudes above i64::MIN’s range remain representable in CBOR,
so this accepts the full u64 range.
Sourcepub fn bytes(&mut self, b: &[u8]) -> Result<&mut Self, Error>
pub fn bytes(&mut self, b: &[u8]) -> Result<&mut Self, Error>
Encodes a byte string header followed by its contents.
Sourcepub fn str(&mut self, s: &str) -> Result<&mut Self, Error>
pub fn str(&mut self, s: &str) -> Result<&mut Self, Error>
Encodes a text string header followed by its UTF-8 contents.
Sourcepub fn undefined(&mut self) -> Result<&mut Self, Error>
pub fn undefined(&mut self) -> Result<&mut Self, Error>
Encodes undefined. RFC 8949 §5.7 discourages its use on the wire,
but the encoding is supported.
Sourcepub fn simple(&mut self, s: Simple) -> Result<&mut Self, Error>
pub fn simple(&mut self, s: Simple) -> Result<&mut Self, Error>
Encodes a simple value.
§Errors
Returns Error::INVALID_SIMPLE when the value would require the
two-byte form while being below 32; RFC 8949 §3.3 forbids that form.
Sourcepub fn tag(&mut self, number: u64) -> Result<&mut Self, Error>
pub fn tag(&mut self, number: u64) -> Result<&mut Self, Error>
Emits a tag header; encode the tagged item immediately after.
Tag semantics live outside this crate (RFC 8949 §3.4): only the tag number travels on the wire.