Crate nlist

Crate nlist 

Source
Expand description

Provides an inline-allocated list which statically tracks its length, and type-based integer/boolean representations which don’t require (additional) bounds for operators.

§Example

§Splitting and recombining

This example shows how NLists can be split and recombined in const, even if the length is generic, so long as the length is known to be greater than the split index.

use nlist::{NList, Peano, PeanoInt, nlist, peano};

const LIST: NList<u128, Peano!(7)> = transform(nlist![3, 5, 8, 13, 21, 34, 55]);

assert_eq!(LIST, nlist![21, 34, 55, 103, 105, 108, 113]);

type SplitIndex = Peano!(4);

const fn transform<L>(
    list: NList<u128, peano::Add<SplitIndex, L>>,
) -> NList<u128, peano::Add<SplitIndex, L>>
where
    L: PeanoInt,
{
    // if we use `let` to destructure instead of `konst::destructure`,
    // we get a "destructor cannot be evaluated at compile-time" error as of Rust 1.83
    konst::destructure!{(before, after) = list.split_at::<SplitIndex>()}
     
    // math spice: using arithmetic properties to coerce equal generic lengths.
    // 
    // Alternatively, you can pass  `peano::eq().unwrap_eq()` to `coerce_len`
    // for an easier, but panic prone, approach:
    // ```
    // return after.concat(map_add_100(before)).coerce_len(peano::eq().unwrap_eq())
    // ```
    // 
    // coercing `NList<u128, L - 0>` to `NList<u128, L>`
    let coerced_after = after.coerce_len(peano::proofs::sub_identity::<L>());
 
    coerced_after.concat(map_add_100(before))
        // coercing `NList<u128, L + SplitIndex>` to `NList<u128, SplitIndex + L>`
        .coerce_len(peano::proofs::commutative_add::<L, SplitIndex>())
}
 
// Adds 100 to all elemenst of an NList
const fn map_add_100<L: PeanoInt>(list: NList<u128, L>) -> NList<u128, L> {
    nlist::rec_map!(list, |elem, rest| (elem + 100, map_add_100(rest)))
}

§Crate features

  • "alloc"(enabled by default): enables methods that take or return Vec

§No-std support

nlist is #![no_std], it can be used anywhere Rust can be used.

§Minimum Supported Rust Version

nlist requires Rust 1.83.0.

Re-exports§

pub use typewit;
pub use crate::peano::PeanoInt;
pub use crate::peano::PeanoWit;
pub use crate::peano::PlusOne;
pub use crate::peano::Zero;

Modules§

boolean
Traits and operations for type-level booleans
peano
Type-level integers which use a unary representation
receiver
Abstraction over T/&'a T/&'a mut T

Macros§

Peano
Converts an integer constant to a peano integer
nlist
Constructs an NList
nlist_pat
Macro for using NList in patterns.
peano
Converts an integer constant to a peano integer
rec_all
Helper for writing const fn equivalents of NList::all
rec_any
Helper for writing const fn equivalents of NList::any
rec_find_map
Helper for writing const fn equivalents of NList::find_map
rec_fold
Helper for writing const fn equivalents of NList::fold
rec_for_each
Helper for writing const fn equivalents of NList::for_each
rec_from_fn
Helper for writing const fn equivalents of NList::from_fn
rec_map
Helper for writing const fn equivalents of NList::map
unlist
Destructures an NList by value into its elements

Structs§

Cons
A node of NList with one element and the rest of the list.
NList
Inline-allocated list of T which statically tracks its length using the L type parameter.
NListFn
Type-level function (typewit::TypeFn implementor) from L to NList<T, L>
Nil
A node of an empty NList

Type Aliases§

NList2D
Alias for an NList of NLists
Node
The type of the head node in NList<T, L>.