pub struct MessageEncoder<'a> { /* private fields */ }
Expand description
Simplifies encoding data with the Encode
trait.
MessageEncoder
maintains an internal position within a mutable byte slice akin to
std’s Cursor
type, allowing sequential writing of multiple encoded values without
manually tracking offsets.
§Example
let mut buf = [0u8; 16];
let mut enc = MessageEncoder::new(&mut buf);
let value: u16 = 0x1234;
enc.write(&value);
assert_eq!(enc.position(), 2);
Implementations§
Source§impl<'a> MessageEncoder<'a>
impl<'a> MessageEncoder<'a>
Sourcepub const fn new_with_position(data: &'a mut [u8], pos: usize) -> Self
pub const fn new_with_position(data: &'a mut [u8], pos: usize) -> Self
Creates a new encoder starting at the given pos
.
Sourcepub fn write<T: Encode>(&mut self, value: &T)
pub fn write<T: Encode>(&mut self, value: &T)
Encodes a value implementing Encode
into the underlying buffer,
advancing the current position by value.size()
.
Sourcepub const fn set_position(&mut self, pos: usize)
pub const fn set_position(&mut self, pos: usize)
Sets the current write position.
Auto Trait Implementations§
impl<'a> Freeze for MessageEncoder<'a>
impl<'a> RefUnwindSafe for MessageEncoder<'a>
impl<'a> Send for MessageEncoder<'a>
impl<'a> Sync for MessageEncoder<'a>
impl<'a> Unpin for MessageEncoder<'a>
impl<'a> !UnwindSafe for MessageEncoder<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more