Took inspirations from the "Parse, don’t validate" article.
This package defines three types: NonEmptySlice
, NonEmptyMutSlice
and NonEmptyVec
. Those types don't implement Deref
or DerefMut
trait,
it is intentional to avoid confusion when resolving methods.
If you want &[T]
, consider using as_slice
or as_mut_slice
methods.
NonEmptySlice
and NonEmptyMutSlice
:
- are borrowed types (not owned type).
- are counterparts of
&[T]
and&mut [T]
. - have same size and similar niche as
&[T]
. - cannot be used without borrowing contents from array, slice or
Vec
.
NonEmptyVec
:
- is an owned types, a counterpart of
Vec<T>
. - doesn't have
push
andpop
methods, because those are fallible operations.
The differences from &[T]
and Vec<T>
:
.len()
returns std'sNonZeroUsize
..is_empty()
is always false.- These methods don't return
None
:first
first_mut
last
last_mut
split_first
split_last
split_first_mut
split_last_mut
#![no_std]
- no external dependencies
- no macros
- instant build time.
Latest stable. I use the new conditional flows in const fn
available only in Rust v1.46.0.
Add this to your Cargo.toml
:
[dependencies]
oom = "0.3.0"
Or assuming you installed cargo-edit
, use:
cargo add oom
That crate uses a representation of:
#[repr(C)]
struct Loaf<T> {
first: [T; 1],
rest: [T],
}
and use &Loaf<T>
or &mut Loaf
at runtime.
- https://github.com/cloudhead/nonempty (MIT license)
- https://github.com/yihuang/non-empty-vec (MIT license)
All the code in this repository is released under the MIT License, for more information read the COPYRIGHT file.