#! @file containers.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the traits a container implements to lend its elements: `indexable` #! and `assignable`, which let it be written `d[key]` to read and to write, the #! way the language already writes a fixed array, a pointer, and a view; #! `spannable` and `sliceable`, which lend a view of its elements that reads and #! one that writes, and let it be written `x[a:n]` to take them, as a view is; #! and `extendable`, which has values appended to it, as an #! iterator's `into` appends each element it gives. module core; #! Defines that a type is indexed by a key, giving the element it holds there to #! read. #! #! `d[key]` is `d.at(key)`: the reference the accessor gives, which is read and #! bound as any reference is, so indexing copies nothing. `val e := d[key]` #! refers to the element, and `val e: V = d[key]` copies it. A type whose #! elements are written where they are is `assignable` too, which adds the #! accessor that writes. One that is only ever read is read through `at(&this)` #! however it is held, so writing an element of it is an error where it is #! written. #! #! The accessor is expected to stop a checked program where the key is one the #! value does not hold, as indexing a fixed array outside its length does, with #! a contract, as `expects(key < this._len)`: one that is not checked trusts the #! key, as C does. A #! key that may be one the value does not hold in a program that is right is #! asked of a `get` the type gives beside this trait, returning `maybe<&V>`, as #! each container of std's does. #! #! Traits are satisfied by shape, as they are everywhere else, so a type with #! `at` is indexable whether or not it names the trait. Naming it is still worth #! doing: it says the type means to be indexed, and it is what a `where` clause #! bounds a type parameter by. #! @tparam K The type of the keys it is indexed by. #! @tparam V The type of the elements it holds. pub trait indexable { #! Reaches the element held at a key, to read it. #! @param this The value to index. #! @param key The key of the element. #! @returns The element. #! @remarks A key the value does not hold stops a checked program. def at(&this, key: K): &V; } #! Defines that a type is indexed by a key and written there too: the half of #! indexing that writes, which a type adds to `indexable` where its elements #! may be changed in place. #! #! There are then two accessors, both named `at`: the one `indexable` asks for, #! taking `&this` and giving a `&V`, and this one, taking `&mut this` and giving #! a `&mut V`. The receiver chooses between them, so a value that can be written #! is given the element to write, and one that cannot is given it only to read. #! Both are given the same key, and both stop a checked program where the key #! is one the value does not hold. #! #! Generic code bounded by it writes `d[key] = value` and `d[key] += 1`, which a #! bound of `indexable` alone does not let it, and `position_mutator` writes #! each element of what is indexed by position through it. #! @tparam K The type of the keys it is indexed by. #! @tparam V The type of the elements it holds. pub trait assignable : indexable { #! Reaches the element held at a key, to write it. #! @param this The value to index. #! @param key The key of the element. #! @returns The element. #! @remarks A key the value does not hold stops a checked program. def at(&mut this, key: K): &mut V; } #! A type that lends a view of the elements it holds, as a `string` lends its #! text and a container its elements. #! #! `x[start:count]` is `T::span_of(&x, start, count)`, the way the language #! already slices a fixed array, a pointer, and a view: `count` elements from #! `start`, as a `span`. A type whose elements are written where they are is #! `sliceable` too, and a value of it that can be written is sliced through #! `slice_of` instead. Either bound may be left out, which gives it as `span_of` #! takes it by default: the start as 0, and the count as `~0u`, which stands for #! every element after the start, so `x[2:]` is all but the first two and `x[:]` #! is all of them. #! #! Generic code bounded by it reads the elements of whatever it is given through #! `span_of`, as `C::span_of(c)` for all of them, or `c[a:n]`. A container lends #! the whole view as its `span` property too, as `v.span`, which is what code #! that knows its type reads. #! #! A type gives `span_of` the same defaults, so a call leaving them out calls it #! as it calls the trait's, and is expected to stop a checked program where the #! start or the count reaches past its last element, as slicing a fixed array #! does. Slicing the whole view does both, as `whole[start:]` where `count` is #! `~0u`, and `whole[start:count]` where it isn't. #! @tparam T The type of the elements. pub trait spannable { #! Lends a view of elements of a value, without copying them. #! @param other The value to view. #! @param start The index of the first element viewed. #! @param count How many elements are viewed, or `~0u` for every one after #! the start. #! @returns The view, which is only good until the value changes or closes. #! @remarks A start or count past the last element stops a checked program. def span_of(other: &self, start: u64 = 0, count: u64 = ~0u): span; } #! A type that lends a writable view of the elements it holds, as a container #! lends one of its elements. #! #! `x[start:count]` is `T::slice_of(&mut x, start, count)` where `x` can be #! written, as a `slice` that writes the elements where they are, and #! `T::span_of(&x, start, count)` where it cannot. The bounds are given and left #! out as `spannable` says. #! #! Generic code bounded by it writes the elements of whatever it is given #! through `slice_of`, as `C::slice_of(c)` for all of them, or `c[a:n]` of a #! `c` that can be written. A container lends the whole view as its `slice` #! property too, as `v.slice`, which only a value that can be written reads. #! @tparam T The type of the elements. pub trait sliceable { #! Lends a writable view of elements of a value, without copying them. #! @param other The value to view, which can be written. #! @param start The index of the first element viewed. #! @param count How many elements are viewed, or `~0u` for every one after #! the start. #! @returns The view, which writes the elements where they are, and is only #! good until the value changes or closes. #! @remarks A start or count past the last element stops a checked program. def slice_of(other: &mut self, start: u64 = 0, count: u64 = ~0u): slice; } #! A container values are appended to one at a time, as a `vector` takes its #! elements and a `string` its text. #! @tparam T The type of what is appended. pub trait extendable { #! Appends a value, after those the container holds. #! @param this The container. #! @param value The value, which the container then owns. def append(&mut this, value: T): void; }