Trait Writer

Source
pub trait Writer {
    // Required method
    fn write_all(&mut self, b: &[u8]);

    // Provided methods
    fn write_u8(&mut self, x: u8) { ... }
    fn write_u16(&mut self, x: u16) { ... }
    fn write_u32(&mut self, x: u32) { ... }
    fn write_u64(&mut self, x: u64) { ... }
    fn write_u128(&mut self, x: u128) { ... }
    fn write_zeros(&mut self, n: usize) { ... }
    fn write<E: Writeable + ?Sized>(&mut self, e: &E) -> EncodeResult<()> { ... }
    fn write_and_consume<E: WriteableOnce>(&mut self, e: E) -> EncodeResult<()> { ... }
    fn write_nested_u8len(&mut self) -> NestedWriter<'_, Self, u8> { ... }
    fn write_nested_u16len(&mut self) -> NestedWriter<'_, Self, u16> { ... }
    fn write_nested_u32len(&mut self) -> NestedWriter<'_, Self, u32> { ... }
}
Expand description

A byte-oriented trait for writing to small arrays.

Most code will want to use the fact that Vec<u8> implements this trait. To define a new implementation, just define the write_all method.

§Examples

You can use a Writer to add bytes explicitly:

use tor_bytes::Writer;
let mut w: Vec<u8> = Vec::new(); // Vec<u8> implements Writer.
w.write_u32(0x12345);
w.write_u8(0x22);
w.write_zeros(3);
assert_eq!(w, &[0x00, 0x01, 0x23, 0x45, 0x22, 0x00, 0x00, 0x00]);

You can also use a Writer to encode things that implement the Writeable trait:

use tor_bytes::{Writer,Writeable};
let mut w: Vec<u8> = Vec::new();
w.write(&4_u16); // The unsigned types all implement Writeable.

// We also provide Writeable implementations for several important types.
use std::net::Ipv4Addr;
let ip = Ipv4Addr::new(127, 0, 0, 1);
w.write(&ip);

assert_eq!(w, &[0x00, 0x04, 0x7f, 0x00, 0x00, 0x01]);

Required Methods§

Source

fn write_all(&mut self, b: &[u8])

Append a slice to the end of this writer.

Provided Methods§

Source

fn write_u8(&mut self, x: u8)

Append a single u8 to this writer.

Source

fn write_u16(&mut self, x: u16)

Append a single u16 to this writer, encoded in big-endian order.

Source

fn write_u32(&mut self, x: u32)

Append a single u32 to this writer, encoded in big-endian order.

Source

fn write_u64(&mut self, x: u64)

Append a single u64 to this writer, encoded in big-endian order.

Source

fn write_u128(&mut self, x: u128)

Append a single u128 to this writer, encoded in big-endian order.

Source

fn write_zeros(&mut self, n: usize)

Write n bytes to this writer, all with the value zero.

NOTE: This implementation is somewhat inefficient, since it allocates a vector. You should probably replace it if you can.

Source

fn write<E: Writeable + ?Sized>(&mut self, e: &E) -> EncodeResult<()>

Encode a Writeable object onto this writer, using its write_onto method.

Source

fn write_and_consume<E: WriteableOnce>(&mut self, e: E) -> EncodeResult<()>

Encode a WriteableOnce object onto this writer, using its write_into method.

Source

fn write_nested_u8len(&mut self) -> NestedWriter<'_, Self, u8>

Arranges to write a u8 length, and some data whose encoding is that length

Prefer to use this function, rather than manual length calculations and ad-hoc write_u8, Using this facility eliminates the need to separately keep track of the lengths.

The returned NestedWriter should be used to write the contents, inside the byte-counted section.

Then you must call finish to finalise the buffer.

Source

fn write_nested_u16len(&mut self) -> NestedWriter<'_, Self, u16>

Arranges to writes a u16 length and some data whose encoding is that length

Source

fn write_nested_u32len(&mut self) -> NestedWriter<'_, Self, u32>

Arranges to writes a u32 length and some data whose encoding is that length

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Writer for Vec<u8>

Vec<u8> is the main type that implements Writer.

Source§

fn write_all(&mut self, bytes: &[u8])

Source§

fn write_u8(&mut self, byte: u8)

Source§

fn write_zeros(&mut self, n: usize)

Source§

impl Writer for BytesMut

Source§

fn write_all(&mut self, bytes: &[u8])

Implementors§