Slice iterators v0.1.0

Source
The iterators an array, vector or deque hands out to walk its elements by copy or by pointer.

ValueIterator<T>

pub struct ValueIterator<T> {
    // private fields
}

An iterator that yields each element of a run by copy. The element type must be copyable: a move-only element yielded by value would leave the container holding something it no longer owns. Use ReferenceIterator for those.

Source

Type parameters

NameDescription
Tthe element type

Constructors

ValueIterator

pub func ValueIterator(items: T[..]) -> ValueIterator<T>

An iterator over items, positioned before the first element. items is borrowed and must outlive the iterator. Cannot fail. Runs in constant time and allocates nothing. The storage must not be grown, freed or otherwise moved while this is alive.

NameTypeDescription
itemsT[..]the elements to take

Returns: a cursor yielding copies of those elements

Source

Implements Iterator

ValueIterator<T> conforms to Iterator.

Next

pub func Next(self: &var ValueIterator<T>) -> T?

The next element, or none once the run is exhausted. self is borrowed mutably and advanced. An exhausted iterator keeps reporting none. Cannot fail. Runs in constant time and allocates nothing.

Returns: the next element, or none at the end

Source

ReferenceIterator<T>

pub struct ReferenceIterator<T> {
    // private fields
}

An iterator that yields a pointer to each element of a run. Works for any element type, including one that owns something and may not be copied: the pointer borrows the element where it sits rather than taking it.

Source

Type parameters

NameDescription
Tthe element type

Constructors

ReferenceIterator

pub func ReferenceIterator(items: T[..]) -> ReferenceIterator<T>

An iterator over items, positioned before the first element. items is borrowed and must outlive the iterator. Cannot fail. Runs in constant time and allocates nothing. Every pointer this yields points into the storage, so the storage must not be grown, freed or moved while this or any pointer it yielded is still in use.

NameTypeDescription
itemsT[..]the elements to take

Returns: a cursor yielding those elements' addresses

Source

Implements Iterator

ReferenceIterator<T> conforms to Iterator.

Next

pub func Next(self: &var ReferenceIterator<T>) -> (*T)?

A pointer to the next element, or none once the run is exhausted. self is borrowed mutably and advanced. The pointer borrows the element and takes ownership of nothing. Cannot fail. Runs in constant time and allocates nothing.

Returns: the next element, or none at the end

Source

MutableReferenceIterator<T>

pub struct MutableReferenceIterator<T> {
    // private fields
}

An iterator that yields a writable pointer to each element of a run. Elements remain in their storage, so move-only elements can be modified without copying or moving them.

Source

Type parameters

NameDescription
Tthe element type

Constructors

MutableReferenceIterator

pub func MutableReferenceIterator(items: var T[..]) -> MutableReferenceIterator<T>

An iterator over items, positioned before the first element. Cannot fail. Runs in constant time and allocates nothing. The storage must outlive the iterator and every pointer it yields. Do not grow, shrink, free, or move the storage while any of them is in use. Coordinate access through aliases to the same elements.

NameTypeDescription
itemsvar T[..]the elements to borrow for writing

Returns: a cursor yielding writable addresses

Source

Implements Iterator

MutableReferenceIterator<T> conforms to Iterator.

Next

pub func Next(self: &var MutableReferenceIterator<T>) -> (*var T)?

The next writable element pointer, or none once exhausted. Advances the cursor without copying, moving, or destroying an element. An exhausted cursor stays exhausted. Cannot fail. Runs in constant time and allocates nothing.

Returns: the next element's address, or none at the end

Source