Trait tor_bytes::Writeable

source ·
pub trait Writeable {
    // Required method
    fn write_onto<B: Writer + ?Sized>(&self, b: &mut B) -> EncodeResult<()>;
}
Expand description

Trait for an object that can be encoded onto a Writer by reference.

Implement this trait in order to make an object writeable.

Most code won’t need to call this directly, but will instead use it implicitly via the Writer::write() method.

Example

use tor_bytes::{Writeable, Writer, EncodeResult};
#[derive(Debug, Eq, PartialEq)]
struct Message {
  flags: u32,
  cmd: u8
}

impl Writeable for Message {
    fn write_onto<B:Writer+?Sized>(&self, b: &mut B) -> EncodeResult<()> {
        // We'll say that a "Message" is encoded as flags, then command.
        b.write_u32(self.flags);
        b.write_u8(self.cmd);
        Ok(())
    }
}

let msg = Message { flags: 0x43, cmd: 0x07 };
let mut writer: Vec<u8> = Vec::new();
writer.write(&msg);
assert_eq!(writer, &[0x00, 0x00, 0x00, 0x43, 0x07 ]);

Required Methods§

source

fn write_onto<B: Writer + ?Sized>(&self, b: &mut B) -> EncodeResult<()>

Encode this object into the writer b.

Implementations on Foreign Types§

source§

impl Writeable for RsaIdentity

source§

impl Writeable for [u8]

source§

impl Writeable for Ipv6Addr

source§

impl<W: Writeable + ?Sized> Writeable for &W

source§

impl Writeable for PublicKey

source§

impl Writeable for Ed25519Identity

source§

impl<N> Writeable for GenericArray<u8, N>where N: ArrayLength<u8>,

source§

impl Writeable for Signature

source§

impl Writeable for u8

source§

impl Writeable for u128

source§

impl Writeable for Vec<u8>

source§

impl<const N: usize> Writeable for [u8; N]

source§

impl Writeable for SharedSecret

source§

impl Writeable for Ipv4Addr

source§

impl Writeable for u16

source§

impl Writeable for PublicKey

source§

impl Writeable for u32

source§

impl<const N: usize> Writeable for CtByteArray<N>

source§

impl Writeable for u64

Implementors§