Memory streams v0.1.0
ByteReader
pub struct ByteReader {
// private fields
}
A reader of a slice of bytes, from its start. Copy.
Borrows the bytes, which must outlive it.
Associated functions
New
pub func New(bytes: char8[..]) -> ByteReader
A reader of bytes, at their start.
| Name | Type | Description |
|---|---|---|
bytes | char8[..] | the bytes to read, which must outlive the reader |
Returns: the reader
Methods
Position
pub func Position(self: &ByteReader) -> uint
How many bytes have been read or skipped.
Returns: the position
Remaining
pub func Remaining(self: &ByteReader) -> char8[..]
The bytes not yet read.
Returns: them
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.
| Name | Type | Description |
|---|---|---|
into | var char8[..] | where the bytes go |
Returns: how many were copied, or fails with EndOfStream once every byte has been read
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.
| Name | Type | Description |
|---|---|---|
origin | SeekFrom | what offset counts from |
offset | int64 | how far, which may be negative |
Returns: the new position, or fails with InvalidInput for one before the start or past the end
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.
Associated functions
New
pub func New(allocator: Allocator) -> ByteWriter
An empty writer that takes its storage from allocator as it grows.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
Returns: the writer
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
Length
pub func Length(self: &ByteWriter) -> uint
How many bytes have been written.
Returns: the length
Clear
pub func Clear(self: &var ByteWriter)
Forgets what was written, keeping the storage for what comes next.
Release
pub func Release(self: &var ByteWriter)
Gives the storage back and empties the writer.
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.
| Name | Type | Description |
|---|---|---|
bytes | char8[..] | the bytes to write |
Returns: how many were written, which is all of them, or fails with LimitExceeded when storage runs out
Flush
pub func Flush(self: &var ByteWriter) -> ! IoError
Does nothing: the bytes are already where they go.
Returns: nothing
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.
| Name | Type | Description |
|---|---|---|
origin | SeekFrom | what offset counts from |
offset | int64 | how far, which may be negative |
Returns: the new position, or fails with InvalidInput for one before the start or past the end
Destructor
~ByteWriter
func ~ByteWriter(self: &var ByteWriter)