Skip to content
CryoCryo home

bytes

import std::io::bytes; · source

Fixed-width integers to and from bytes, in a stated byte order. Each method is generic over the integer type and names its order in full — big_endian (most significant byte first, the network order of the wire protocols) or little_endian (least significant first) — so a call site says which layout it reads or writes, whatever the host's own order is. The width is the type's: a u16 is two bytes, an i64 eight.

import std::io::bytes::{ ByteReader, ByteWriter };
import std::io::error::{ IoError };

function round_trip() -> Result<u32, IoError> {
    mut out: Array<u8> = Array::new();
    mut writer: ByteWriter = ByteWriter::new(&out);
    writer.write_big_endian::<u16>(0x0102);
    writer.write_little_endian::<u32>(7);

    mut reader: ByteReader = ByteReader::new(out.as_slice());
    const tag: u16 = reader.read_big_endian::<u16>()?;
    const length: u32 = reader.read_little_endian::<u32>()?;
    return Result::Ok(length + (tag as u32));
}

function main() -> int {
    return if (round_trip().unwrap() == 7 + 0x0102) { 0 } else { 1 };
}

Types

ByteReader

type struct ByteReader {
    private bytes:    &Slice<u8>;
    private position: i64;
}

Walks a borrowed &Slice<u8> front to back, decoding one integer per call. The slice's storage must outlive the reader. A read that needs more bytes than remain fails with UnexpectedEof and leaves the position where it was. position() is the offset of the next byte, remaining() the bytes left, and set_position moves to an absolute offset — one past the end is allowed, and every read from there fails.

Constructors

static new(bytes: &Slice<u8>) -> ByteReader;

A reader at the first byte of bytes. The slice is borrowed: its storage must outlive the reader.

Methods

position(&this) -> i64;

The offset of the next byte a read takes.

set_position(mut &this, position: i64) -> void;

Move to the absolute offset position. An offset past the end is allowed, and every read from there fails; a negative one panics.

remaining(&this) -> i64;

The number of bytes left to read, 0 at or past the end.

read_big_endian<T>(mut &this) -> Result<T, IoError> where T: Integer;

The next T, most significant byte first: as many bytes as T is wide. Like every read here, it fails with UnexpectedEof, and does not move, when too few bytes remain.

read_little_endian<T>(mut &this) -> Result<T, IoError> where T: Integer;

The next T, least significant byte first: as many bytes as T is wide. Fails with UnexpectedEof, without moving, when too few bytes remain.

ByteWriter

type struct ByteWriter {
    private array:   mut Array<u8>*;
    private region:  mut &Slice<u8>;
    private written: i64;
}

new(out) appends to a borrowed Array<u8>, after whatever it already holds; over(region) writes into a fixed region from its first byte — a header packed into a buffer a C call reads, a digest's output block. Writes return nothing: a write panics if the array cannot grow, or if it would pass the end of an over region. position() is how many bytes have been written.

Constructors

static new(out: mut &Array<u8>) -> ByteWriter;

A writer appending to out, after whatever it already holds.

static over(region: mut &Slice<u8>) -> ByteWriter;

A writer into region, from its first byte: a header packed into a buffer a C call reads, a digest's output block.

Methods

position(&this) -> i64;

How many bytes this writer has written - into a region, the offset of the next write.

write_big_endian<T>(mut &this, value: T) -> void where T: Integer;

Write value most significant byte first, as many bytes as T is wide. Like every write here, it panics when a writer made by over runs past the end of its region.

write_little_endian<T>(mut &this, value: T) -> void where T: Integer;

Write value least significant byte first, as many bytes as T is wide.