Slice iterators v0.1.0
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.
Type parameters
| Name | Description |
|---|---|
T | the 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.
| Name | Type | Description |
|---|---|---|
items | T[..] | the elements to take |
Returns: a cursor yielding copies of those elements
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
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.
Type parameters
| Name | Description |
|---|---|
T | the 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.
| Name | Type | Description |
|---|---|---|
items | T[..] | the elements to take |
Returns: a cursor yielding those elements' addresses
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
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.
Type parameters
| Name | Description |
|---|---|
T | the 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.
| Name | Type | Description |
|---|---|---|
items | var T[..] | the elements to borrow for writing |
Returns: a cursor yielding writable addresses
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