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.
Constructors
ByteReader
pub func ByteReader(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) -> uint
How many bytes have been read or skipped.
Returns: the position
Remaining
pub func Remaining(&self) -> char8[..]
The bytes not yet read.
Returns: them
Implements Reader
ByteReader conforms to Reader.
Read
pub func Read(
&var self,
into: var char8[..]
) -> uint throws 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(
&var self,
origin: SeekFrom,
offset: int64
) -> uint64 throws 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.
Constructors
ByteWriter
pub func ByteWriter(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) -> char8[..]
Everything written so far. The view lasts until the next write, Clear or Release.
Returns: the bytes
Length
pub func Length(&self) -> uint
How many bytes have been written.
Returns: the length
Clear
pub func Clear(&var self)
Forgets what was written, keeping the storage for what comes next.
Release
pub func Release(&var self)
Gives the storage back and empties the writer.
Implements Writer
ByteWriter conforms to Writer.
Write
pub func Write(
&var self,
bytes: char8[..]
) -> uint throws 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(&var self) throws IoError
Does nothing: the bytes are already where they go.
Returns: nothing
Implements Seeker
ByteWriter conforms to Seeker.
Seek
pub func Seek(
&var self,
origin: SeekFrom,
offset: int64
) -> uint64 throws 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(&var self)