Memory streams v0.1.0

Source
A reader and a writer over bytes in memory, with seeking.

ByteReader

pub struct ByteReader {
    // private fields
}

A reader of a slice of bytes, from its start. Copy. Borrows the bytes, which must outlive it.

Source

Associated functions

New

pub func New(bytes: char8[..]) -> ByteReader

A reader of bytes, at their start.

NameTypeDescription
byteschar8[..]the bytes to read, which must outlive the reader

Returns: the reader

Source

Methods

Position

pub func Position(self: &ByteReader) -> uint

How many bytes have been read or skipped.

Returns: the position

Source

Remaining

pub func Remaining(self: &ByteReader) -> char8[..]

The bytes not yet read.

Returns: them

Source

Implements Reader

ByteReader conforms to Reader.

Read

pub func Read(
    self: &var ByteReader,
    into: var char8[..]
) -> uint ! IoError

Copies the next bytes into into, as many as fit.

NameTypeDescription
intovar char8[..]where the bytes go

Returns: how many were copied, or fails with EndOfStream once every byte has been read

Source

Implements Seeker

ByteReader conforms to Seeker.

Seek

pub func Seek(
    self: &var ByteReader,
    origin: SeekFrom,
    offset: int64
) -> uint64 ! IoError

Moves the position, anywhere from the start to the end of the bytes.

NameTypeDescription
originSeekFromwhat offset counts from
offsetint64how far, which may be negative

Returns: the new position, or fails with InvalidInput for one before the start or past the end

Source

ByteWriter

pub struct ByteWriter {
    // private fields
}

A writer that keeps what is written in a growing block of memory. Not copied. Writing at a position before the end, after a seek, overwrites what is there and extends past it.

Source

Associated functions

New

pub func New(allocator: Allocator) -> ByteWriter

An empty writer that takes its storage from allocator as it grows.

NameTypeDescription
allocatorAllocatorwhere the storage comes from

Returns: the writer

Source

Methods

Bytes

pub func Bytes(self: &ByteWriter) -> char8[..]

Everything written so far. The view lasts until the next write, Clear or Release.

Returns: the bytes

Source

Length

pub func Length(self: &ByteWriter) -> uint

How many bytes have been written.

Returns: the length

Source

Clear

pub func Clear(self: &var ByteWriter)

Forgets what was written, keeping the storage for what comes next.

Source

Release

pub func Release(self: &var ByteWriter)

Gives the storage back and empties the writer.

Source

Implements Writer

ByteWriter conforms to Writer.

Write

pub func Write(
    self: &var ByteWriter,
    bytes: char8[..]
) -> uint ! IoError

Writes all of bytes at the position, growing the storage as needed.

NameTypeDescription
byteschar8[..]the bytes to write

Returns: how many were written, which is all of them, or fails with LimitExceeded when storage runs out

Source

Flush

pub func Flush(self: &var ByteWriter) -> ! IoError

Does nothing: the bytes are already where they go.

Returns: nothing

Source

Implements Seeker

ByteWriter conforms to Seeker.

Seek

pub func Seek(
    self: &var ByteWriter,
    origin: SeekFrom,
    offset: int64
) -> uint64 ! IoError

Moves the position at which the next write lands, anywhere from the start to the end of what was written.

NameTypeDescription
originSeekFromwhat offset counts from
offsetint64how far, which may be negative

Returns: the new position, or fails with InvalidInput for one before the start or past the end

Source

Destructor

~ByteWriter

func ~ByteWriter(self: &var ByteWriter)

Source