Trait byte::TryWrite
[−]
[src]
pub trait TryWrite<Ctx = ()> {
fn try_write(self, bytes: &mut [u8], ctx: Ctx) -> Result<usize>;
}A data structure that can be serialized. Types implement this trait can be write() into byte slice.
Required Methods
fn try_write(self, bytes: &mut [u8], ctx: Ctx) -> Result<usize>
Try to write to bytes using context.
Write the value into bytes; the passed-in bytes is implicitly splitted by offset and should be write at head.
If success, try_write() should return the number of bytes it consumed.
Example
use byte::*; // Demo type showing how to write boolean into bytes. // This functionality is already provided by this crate. pub struct Bool(bool); impl TryWrite for Bool { #[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) } }
Error
If try_write() returns Error::BadOffset, it will be translated into Error::Incomplete.
See byte::Error documentation for details.
Implementors
impl<'a> TryWrite for &'a strimpl TryWrite<Endian> for u8impl TryWrite<Endian> for u16impl TryWrite<Endian> for u32impl TryWrite<Endian> for u64impl TryWrite<Endian> for i8impl TryWrite<Endian> for i16impl TryWrite<Endian> for i32impl TryWrite<Endian> for i64impl TryWrite<Endian> for usizeimpl TryWrite<Endian> for isizeimpl<'a> TryWrite<Endian> for f32impl<'a> TryWrite<Endian> for f64impl<'a> TryWrite for &'a [u8]impl TryWrite for bool