← core

core/array.kur

//! Fixed-length arrays over caller-provided storage.
// core::array -- a fixed-length run of values over storage the caller
// provides. `core` does not care how that storage was obtained (a stack
// array, a static, memory from an allocator or a page frame): the one
// `unsafe` constructor states what it must be, and every access after
// that is bounds-checked against the length it was given.
//
// An array is total: values go in and come out (`get` copies, `set`
// swaps), and nothing ever references an element inside it. That is the
// difference from `alloc::collections::vec::Vec!`, which is branded and
// hands out `Ref`s to its elements. Without references there is nothing to
// brand, so the array is simply linear: it owns exclusive access to its
// storage, and moving it is the only way to share it.
use package as core;

use core::option::{Option, Result};
use core::ptr::offset;
use core::process::trap;

/// Exclusive access to a fixed-length run of initialized values in
/// caller-provided storage. `into_parts` returns that storage to the caller.
forall T.
struct Array! {
    base: *T,
    len: usize,
}

forall T.
impl Array![T] {
    /// The array of the `len` values at `base`.
    ///
    /// Caller: `base` addresses `len` initialized `T`s that nothing else
    /// reads or writes while this array exists.
    unsafe fn from_parts(base: *T, len: usize) -> Array![T] {
        Array!.{ base, len }
    }

    /// Ends the array and gives its storage back, with every element in it.
    fn into_parts(self) -> (*T, usize) {
        let { base, len } = self;
        (base, len)
    }

    /// The number of elements.
    fn len(&self) -> usize {
        let { base, len } = self;
        self = Array!.{ base, len };
        len
    }
}

// A separate `impl` block for `set`'s premise: a raw read or write is at a
// type of non-zero size, which `Sized[T]` says of `T`.
forall T where Sized[T].
impl Array![T] {

    /// Puts `value` at `i` and hands back what was there, or hands `value`
    /// back unplaced when `i` is out of range. Nothing is dropped either way,
    /// so this works for linear elements too.
    fn set(&self, i: usize, value: T) -> Result[T, T] {
        let { base, len } = self;
        if i >= len {
            self = Array!.{ base, len };
            return Result::Err.(value);
        }
        // `i < len`, inside the storage `from_parts` was given.
        let slot = unsafe offset(base, i);
        let old = unsafe *slot;
        unsafe *slot = value;
        self = Array!.{ base, len };
        Result::Ok.(old)
    }
}

forall T where Copy[T], Sized[T].
impl Array![T] {
    /// A copy of the element at `i`, or nothing when `i` is out of range.
    fn get(&self, i: usize) -> Option[T] {
        let { base, len } = self;
        if i >= len {
            self = Array!.{ base, len };
            return Option::None;
        }
        // `i < len`, inside the storage `from_parts` was given.
        let value = unsafe *(unsafe offset(base, i));
        self = Array!.{ base, len };
        Option::Some.(value)
    }

    /// A copy of the element at `i`; stops the program
    /// (`core::process::trap`) when `i` is out of range.
    fn at(&self, i: usize) -> T {
        match &self.get(i) {
            Some(v) => v,
            None => trap(),
        }
    }
}