Mitsein is a Rust library that provides strongly typed APIs for non-empty collections and views, including (but not limited to) iterators, slices, and vectors.
Basic Usage
Allocating a Vec1 from one or more items (infallibly):
use ;
let xs = from_one;
let xs = from;
let xs = from_head_and_tail;
let xs = vec1!;
Allocating a Vec1 from zero or more items (fallibly):
use *;
let ys = vec!;
let xs = try_from?;
let xs = try_from?;
let xs = try_from_iter?;
Mapping over items in a Vec1:
use *;
let xs = from;
let ys: = xs.into_iter1.map.collect1;
Removing items from a Vec1:
use *;
let mut xs = from;
while let Ok = xs.pop_or_get_only
xs.tail.clear;
Bridging between Iterator and Iterator1:
use iter1;
use *;
let xs = head_and_tail;
let xs: = xs.into_iter.skip.or_non_empty.collect1;
assert_eq!;
let xs = from;
let = xs.iter1.any;
if has_zero
Features and Comparisons
By providing non-empty APIs over both collections and views (i.e., slices
and iterators), Mitsein separates concerns just like standard collection and
iterator APIs. Unlike many other non-empty collection implementations, Mitsein
need not expose combinatorial sets of inherent iterator-like functions in
collections for each pair of receiver and operation. For example, the vec1
crate supports map operations over its Vec1 type via the Vec1::mapped,
Vec1::mapped_ref, and Vec1::mapped_mut functions. Mitsein instead exposes
map operations via Iterator1, which can support any non-empty view or
collection with a more typical API.
Non-empty view APIs also enable borrowing, so Mitsein collection types support
copy-on-write via the standard Cow type, unlike many other non-empty
collection implementations.
Non-empty iterator APIs are largely compatible with standard iterators, allowing non-empty collections to interact ergonomically with collections of zero or more items. Mitsein views and collections use more familiar syntax for mapping, taking, chaining, etc.
Items are stored contiguously or otherwise consistently in Mitsein. This
means that no head item is allocated diffently. For example, the nonempty
crate directly exposes a head item that is, unlike tail items, not allocated
on the heap. This can potentially cause surprising behavior or poor performance
when the item type is large, for example.
Non-empty APIs in collections that exhibit different behavior are distinct from
counterparts in Mitsein. For example, the vec1 crate presents Vec1::pop
and Vec1::remove, which may be unclear in context. Mitsein instead presents
Vec1::pop_or_get_only and Vec1::remove_or_get_only.
Mitsein separates many non-empty error concerns into a segmentation API. The
segmentation API provides a view (called a segment) into collections that
supports topological mutations (unlike slices, for example). This works well
for non-empty collections, which can be segmented prior to otherwise fallible
operations. The nonempty and nunny crates have limited (or no) support
for removals while the vec1 crate provides fallible but bespoke
counterparts.
use *;
let mut xs = from;
xs.tail.clear;
assert_eq!;
let mut xs = from;
xs.tail.rtail.drain;
assert_eq!;
let mut xs = from;
xs.segment.truncate;
assert_eq!;
Mitsein provides complete coverage of ordered collections and heap container
APIs in core and alloc. This notably includes slice, BTreeMap,
BTreeSet, Box, and Arc. The nonempty and vec1 crates lack support
for primitive types like slice and positional collections in alloc beyond
Vec.
Mitsein is a no_std library and alloc is optional. Non-empty slices,
iterators, and arrays can be used in embedded contexts, or any other environment
where OS features or allocation are not available.
Cargo Features
Mitsein provides some optional features and integrations via the following Cargo features.
| Feature | Default | Dependencies | Description |
|---|---|---|---|
alloc |
Yes | alloc |
Non-empty collections that allocate, like Vec1. |
arrayvec |
No | arrayvec |
Non-empty implementation of ArrayVec. |
itertools |
No | itertools |
Combinators from itertools for Iterator1. |
serde |
No | serde, serde_derive |
De/serialization of non-empty collections with serde. |