pub struct TlvWriter<'a> { /* private fields */ }Expand description
A streaming TLV encoder that appends to a caller-provided Vec<u8>.
Implementations§
Source§impl<'a> TlvWriter<'a>
impl<'a> TlvWriter<'a>
Sourcepub fn new(out: &'a mut Vec<u8>) -> Self
pub fn new(out: &'a mut Vec<u8>) -> Self
Construct a writer that appends to out. The writer borrows out
mutably; release the borrow by dropping the writer.
Sourcepub fn put_bool(&mut self, tag: Tag, v: bool) -> Result<()>
pub fn put_bool(&mut self, tag: Tag, v: bool) -> Result<()>
Emit a boolean value with the given tag.
§Errors
Currently infallible; returns Ok(()) always. The Result return
type is reserved for future I/O-backed writers.
Sourcepub fn put_null(&mut self, tag: Tag) -> Result<()>
pub fn put_null(&mut self, tag: Tag) -> Result<()>
Emit a null value with the given tag.
§Errors
Currently infallible; returns Ok(()) always. The Result return
type is reserved for future I/O-backed writers.
Sourcepub fn put_uint(&mut self, tag: Tag, v: u64) -> Result<()>
pub fn put_uint(&mut self, tag: Tag, v: u64) -> Result<()>
Emit an unsigned integer with the given tag. The minimum-width encoding (1, 2, 4, or 8 bytes) is chosen automatically per Matter Core Spec §A.2.
§Errors
Currently infallible; returns Ok(()) always. The Result return
type is reserved for future I/O-backed writers.
Sourcepub fn put_int(&mut self, tag: Tag, v: i64) -> Result<()>
pub fn put_int(&mut self, tag: Tag, v: i64) -> Result<()>
Emit a signed integer with the given tag. The minimum-width encoding (1, 2, 4, or 8 bytes) is chosen automatically per Matter Core Spec §A.2.
§Errors
Currently infallible; returns Ok(()) always. The Result return
type is reserved for future I/O-backed writers.
Sourcepub fn put_float(&mut self, tag: Tag, v: f32) -> Result<()>
pub fn put_float(&mut self, tag: Tag, v: f32) -> Result<()>
Emit a single-precision IEEE 754 float with the given tag.
§Errors
Currently infallible; returns Ok(()) always. The Result return
type is reserved for future I/O-backed writers.
Sourcepub fn put_double(&mut self, tag: Tag, v: f64) -> Result<()>
pub fn put_double(&mut self, tag: Tag, v: f64) -> Result<()>
Emit a double-precision IEEE 754 float with the given tag.
§Errors
Currently infallible; returns Ok(()) always. The Result return
type is reserved for future I/O-backed writers.
Sourcepub fn put_utf8(&mut self, tag: Tag, v: &str) -> Result<()>
pub fn put_utf8(&mut self, tag: Tag, v: &str) -> Result<()>
Emit a UTF-8 string with the given tag. The minimum-width length field (1, 2, 4, or 8 bytes) is chosen automatically.
§Errors
Returns Error::LengthOverflow if the string is longer than
u64::MAX bytes (impossible in practice on any supported platform,
but the return type is Result for portability).
Sourcepub fn put_bytes(&mut self, tag: Tag, v: &[u8]) -> Result<()>
pub fn put_bytes(&mut self, tag: Tag, v: &[u8]) -> Result<()>
Emit an octet string with the given tag. The minimum-width length field (1, 2, 4, or 8 bytes) is chosen automatically.
§Errors
Returns Error::LengthOverflow if the slice is longer than
u64::MAX bytes (impossible in practice on any supported platform,
but the return type is Result for portability).
Sourcepub fn put_preencoded(&mut self, tag: Tag, element: &[u8]) -> Result<()>
pub fn put_preencoded(&mut self, tag: Tag, element: &[u8]) -> Result<()>
Splice an already-encoded TLV element into the stream under a new
tag, replacing the element’s own tag control.
element MUST be a single complete TLV element encoded with an
anonymous tag (one control octet, no tag bytes), e.g. the output
of another TlvWriter that began with start_structure(Tag::Anonymous).
Used to embed pre-encoded command-fields / payloads under a context
tag without re-parsing them.
§Errors
Returns Error::UnexpectedEof if element is empty,
Error::InvalidTagControl if element does not begin with an
anonymous-tagged control octet, or Error::InvalidElementType if
the element is a bare end-of-container marker (0x18), which is a
delimiter, not a complete element.
Sourcepub fn start_structure(&mut self, tag: Tag) -> Result<()>
pub fn start_structure(&mut self, tag: Tag) -> Result<()>
Begin a structure with the given tag. Children must be emitted
with their own put_* / write_value calls; the structure is
closed with Self::end_container.
§Errors
Currently infallible; returns Ok(()) always. The Result return
type is reserved for future I/O-backed writers.
Sourcepub fn start_array(&mut self, tag: Tag) -> Result<()>
pub fn start_array(&mut self, tag: Tag) -> Result<()>
Begin an array with the given tag. Children MUST be emitted with
Tag::Anonymous per the Matter spec.
§Errors
Currently infallible; returns Ok(()) always. The Result return
type is reserved for future I/O-backed writers.
Sourcepub fn start_list(&mut self, tag: Tag) -> Result<()>
pub fn start_list(&mut self, tag: Tag) -> Result<()>
Begin a list with the given tag. List members may carry any tag form (including anonymous).
§Errors
Currently infallible; returns Ok(()) always. The Result return
type is reserved for future I/O-backed writers.
Sourcepub fn end_container(&mut self) -> Result<()>
pub fn end_container(&mut self) -> Result<()>
Emit the end-of-container marker (0x18) closing the most
recently-opened container. The marker has no tag.
§Errors
Currently infallible; returns Ok(()) always. The Result return
type is reserved for future I/O-backed writers.
Sourcepub fn write_value(&mut self, tag: Tag, value: &Value) -> Result<()>
pub fn write_value(&mut self, tag: Tag, value: &Value) -> Result<()>
Walk a Value tree and emit the appropriate sequence of TLV
elements. Scalar variants are dispatched to the corresponding
put_* method; container variants (Structure, Array, List)
recursively encode all members and close with Self::end_container.
Array elements are always written with Tag::Anonymous regardless of
what tag is stored in the Value, enforcing the Matter spec requirement
that array elements carry no tag.
Container nesting is bounded by MAX_DEPTH, mirroring the reader’s
limit. A Value tree nested deeper than that is rejected with
Error::ContainerTooDeep rather than risking a stack overflow on a
hostile or buggy input tree.
§Errors
Error::ContainerTooDeep— thevaluetree nests containers more thanMAX_DEPTHlevels deep.- Any error returned by the underlying
put_*or container method.