[−][src]Module type_freak::list
Typed list that supports insertion, removal and look-up.
Type construction
The TList trait represents a typed list of arbitrary types.
The type LCons forms intermediate nodes, while
LNil type marks the end of list. For a list of u8
, u16
and u32
types:
LCons<u8, LCons<u16, LCons<u32, LNil>>>
Most of the time you don't need to write in this cumbersome way. The TListType macro let you write in more compact syntax. For example,
TListType![u8, u16, u32]
List manipuation
The module ships a collection of type operators to manipulate lists, including LPrepend, LAppend, LInsertAt, LRemoveAt. As the name explains itself, you can append or prepend a type to this list, insert a new type after a some type, or remove a specific type. We can work it out by their type aliases for convenience.
use type_freak::{TListType, list::*}; type List1 = TListType![u8, u16, u32]; type List2 = LPrepend<List1, u64>; // List2 ~= TListType![u64, u8, u16, u32] // is alias of <List1 as LPrepend<List1, u64>>::Output type List3<Index1> = LRemoveAt<List2, u16, Index1>; // List3<_> ~= TListType![u64, u8, u32] type List4<Index1> = LAppend<List3<Index1>, f32>; // List4 ~= TListType![u64, u8, u32, f32] type List5<Index1, Index2> = LInsertAt<List4<Index1>, u8, f64, Index2>; // List5 ~= TListType![u64, u8, f64, u32, f32]
As shown in the example, LInsertAt,
LRemoveAt along with other type operators
have a special Index
generic type argument. It is necessary for
list traversal. Most of the time we can leave it undetermined.
It can be inferred by compiler when constructing concrete type.
let _ = List5::<_, _>::new();
Marker traits
The EmptyTList and NonEmptyTList traits can be used in trait bounds. Suppose you wish to accept a non-empty TList type:
trait ExampleTrait<List: NonEmptytList> { /* ... */ }
Numeric type operators
LReduceMax, LReduceMin,
LReduceSum and LReduceProd
assume all contained types are typenum typed numbers. You may use typenum::consts::*
to work with typenum constants.
use type_freak::{TListType, list::LReduceSum}; use typenum::consts::*; type Value = LReduceSum<TListType![P3, N5, Z0]>; // Value ~= P2
The LToUsizeVec provides a
to_usize_vec to build concrete
Vec<usize>
type.
// Gets vec![3, -5, 0] let values = <TListType![P3, N5, Z0] as LToUsizeVec>::to_usize_vec();
Modules
marker |
Structs
Traits
LAppendOp | A type operator that appends a new type to TList. |
LConcatOp | Concatenates the |
LFilterOp | Filters the values in TList. |
LFoldOp | A type operator that accumulates all values in TList. |
LIndexOfManyOp | Gets indexes of multiple types from TList. |
LIndexOfOp | A type operator that returns the position of |
LInsertAtOp | Inserts a |
LLengthOp | A type operator that gets the length of TList. |
LMapOp | |
LRemoveAtOp | Removes |
LRemoveManyOp | Removes a collection of types from TList. |
LScanOp | A LMap-like operator that maintains internal state. |
LSetEqualOp | Compare if a left-hand-side TList has the same set of types
with |
LSplitOp | A type operator that splits a TList at |
LToUsizeVec | The trait builds a concrete |
LUnzipOp | |
LZipOp | |
TList |