core:linear
Arrays of plain data in linear memory, outside the GC heap.
A LinearArray<T> owns one block from the program's allocator and gives it
back when it drops. An element is any AnyBits type: an integer, a float,
v128 or a newtype over one, which every byte pattern is a value of. The
engine checks no element access against the array, since linear memory has
no per-allocation bounds; each access states its range as a contract
instead, which loop_version_bce deletes in a loop that keeps the index in
range.
The array is affine, so one block has one owner, and no reference into it
outlives that owner. Its address is never handed out, and every element is
written before anything reads it. It crosses a component boundary only as a
ListView<T>, which a synchronous import's list<T> parameter takes and any
ListArg converts to: there it lends its own block for the call. See WEP:
Linear Arrays.
Synopsis
let mut weights = LinearArray::<f32>::filled(4, 0.0);
weights[2] = 1.5;
assert weights[2] == 1.5;
assert weights.to_list() == [0.0, 0.0, 1.5, 0.0];
Traits
pub affine trait ListArg<T: AnyBits> with ()
What converts to a &ListView<T> argument: List<T> by copying its
elements into a block of the view's own, &LinearArray<T> by lending its
block.
fn lowered(&self) -> ListView<T>
Structs
pub affine struct ListView<T: AnyBits>
Ts lying in linear memory in the layout the Canonical ABI gives a
list<T>, read-only. A call argument converts to a &ListView<T>
parameter from any ListArg<T>, and the view drops when the statement
holding the call ends. Only core:* writes one, so a view frees no block it
did not allocate.
Fields are private.
pub fn len(&self) -> i32
pub fn is_empty(&self) -> bool
pub fn at(&self, index: i32) -> T
contract 0 <= index < self.len
The element at index.
pub fn to_list(&self) -> List<T>
The elements, copied into a List.
impl Drop for ListView<T>
fn drop(self)
impl IndexValue<i32> for ListView<T>
fn index_value(&self, index: i32) -> Self::Output
pub affine struct LinearArray<T: AnyBits>
A fixed-length array of T in linear memory.
Fields are private.
pub fn filled(len: i32, value: T) -> LinearArray<T>
contract 0 <= len <= i32::MAX / builtin::size_of::<T>()
An array of len elements, each value.
pub fn from_slice<S: AsSlice<Elem = T>>(source: &S) -> LinearArray<T>
An array holding a copy of each element of source.
pub fn len(&self) -> i32
pub fn is_empty(&self) -> bool
pub fn at(&self, index: i32) -> T
contract 0 <= index < self.len()
The element at index.
pub fn set(&mut self, index: i32, value: T)
contract 0 <= index < self.len()
Writes value at index.
pub fn duplicate(&self) -> LinearArray<T>
A second array holding the same elements.
pub fn to_list(&self) -> List<T>
The elements, copied into a List.
pub fn cast<U: AnyBits>(self) -> LinearArray<U>
contract self.len() * builtin::size_of::<T>() % builtin::size_of::<U>() == 0
The same bytes as an array of U, read little-endian, with no element
copied. The array moves, so its bytes are never seen as two types at once.
pub fn to_list_of<U: AnyBits>(&self, start: i32, end: i32) -> List<U>
contract 0 <= start <= end <= self.len()contract (end - start) % builtin::size_of::<U>() == 0
The bytes from start to end as Us, read little-endian at any
alignment, copied into a List: a part of a file read_linear read,
decoded with no copy of the bytes before it.
pub fn to_string_of(&self, start: i32, end: i32) -> Result<String, Utf8Error>
contract 0 <= start <= end <= self.len()
The bytes from start to end as text, copied once into a String.
pub fn of_bytes(bytes: &ByteList, start: i32, len: i32, zeros: i32 = 0) -> LinearArray<u8>
contract 0 <= start && 0 <= len <= bytes.len() - startcontract 0 <= zeros <= i32::MAX - len
A copy of bytes[start..<start + len], followed by zeros zero bytes.
pub fn to_byte_list(&self, len: i32) -> List<u8>
contract 0 <= len <= self.len()
The first len bytes, copied into a List.
pub fn load_at<U: AnyBits>(&self, offset: i32) -> U
contract 0 <= offset <= self.len() - builtin::size_of::<U>()
The U whose bytes begin at offset, read little-endian at any
alignment.
pub fn store_at<U: AnyBits>(&mut self, offset: i32, value: U)
contract 0 <= offset <= self.len() - builtin::size_of::<U>()
Writes value little-endian at offset, at any alignment.
pub fn copy_from(&mut self, at: i32, source: &LinearArray<u8>, from: i32, count: i32)
contract 0 <= countcontract 0 <= at <= self.len() - countcontract 0 <= from <= source.len() - count
Copies source[from..<from + count] to self[at..<at + count]. The two
ranges may overlap, which reads as if through a buffer.
pub fn lengthen(&mut self, len: i32)
contract self.len() <= len
Lengthens the array to len bytes, the new ones unwritten: the caller
writes each before reading it. The block at least doubles where it
grows, so growing a byte at a time copies each byte a bounded number of
times.
pub fn read_stream(stream: &Stream<u8>, expected: i32 = 0) -> LinearArray<u8> with Stream
contract expected >= 0
Every byte stream delivers until its writer drops. A host copy lands
in the array's own block, so no byte passes through the GC heap; a
handler's items are copied in. The block takes expected bytes up
front and grows only for a stream that runs past them.
pub fn read_stream_prefix(stream: &Stream<u8>, len: i32) -> LinearArray<u8> with Stream
contract len >= 0
The first len bytes stream delivers, or every byte where it ends
sooner, read as read_stream reads them. No copy asks for a byte past
len, so the stream may go on for any length after them.
impl IndexValue<i32> for LinearArray<T>
fn index_value(&self, index: i32) -> Self::Output
impl IndexAssign<i32> for LinearArray<T>
fn index_assign(&mut self, index: i32, value: Self::Output)
pub affine struct LinearBuffer
Bytes in linear memory, read from a stream a part at a time, or held whole:
what a parser reads a stream through with no byte lifted into the GC heap.
bytes() holds what was read, and the part not yet consumed begins at
start(). Both change only where fill reads more or consume drops some.
Fields are private.
pub fn of(bytes: LinearArray<u8>) -> LinearBuffer
A buffer holding bytes and nothing more to read.
pub fn of_stream(stream: Stream<u8>) -> LinearBuffer
A buffer reading stream, which it drops once the stream ends or the
buffer drops.
pub fn bytes(&self) -> &LinearArray<u8>
What was read, from start() on not yet consumed.
pub fn start(&self) -> i32
Where in bytes() the part not yet consumed begins.
pub fn len(&self) -> i32
The bytes read and not yet consumed.
pub fn position(&self) -> i64
How far into the stream start() is.
pub fn fill(&mut self, n: i32) -> bool with Stream
contract n >= 0
Read until n bytes are not yet consumed, or the stream ends. Answers
whether n are.
pub fn consume(&mut self, n: i32)
contract 0 <= n <= self.bytes.len - self.start
Drop the next n bytes.
