pub trait TryWrite<Ctx = ()> {
// Required method
fn try_write(self, bytes: &mut [u8], ctx: Ctx) -> Result<usize>;
}
Expand description
A data structure that can be serialized.
Types implement this trait can be write()
into a byte slice.
Required Methods§
sourcefn try_write(self, bytes: &mut [u8], ctx: Ctx) -> Result<usize>
fn try_write(self, bytes: &mut [u8], ctx: Ctx) -> Result<usize>
Try to write to a byte slice using a specific context.
Write the value into bytes; the bytes passed in are splitted by offset
and should be written at head.
If successful try_write()
should return the number of bytes written.
§Example
use byte::*;
pub struct HasBool(bool);
impl TryWrite for HasBool {
#[inline]
fn try_write(self, bytes: &mut [u8], _ctx: ()) -> Result<usize> {
check_len(bytes, 1)?;
bytes[0] = if self.0 { u8::max_value() } else { 0 };
Ok(1)
}
}