Library for labeled heterogeneous lists.
This crate provides data structures and traits for assembling and using labeled heterogeneous lists. A heterogeneous list is a list of values where each value has a different types. These heterogeneous lists are implemented using cons-lists, which adds some usage flexibility over what is available with built-in Rust tuples.
use lhlist::Label; new_label![MyLabel: u8]; assert_eq!(MyLabel::name(), "MyLabel");
See the new_label documentation for more examples of creating labels.
Labeled lists are created using the lhlist macro, which takes a comma-separated list of label-value pairs. For example,
use lhlist::Label; new_label![SomeNumbers: Vec<u64>]; new_label![SomeNames: Vec<&'static str>]; new_label![Flag: bool]; let my_list = lhlist![ SomeNumbers = vec![0, 4, 5, 2], SomeNames = vec!["hello", "world!"], Flag = false, ]; assert_eq!(my_list[Flag], false);
Internally, the values are contained as LabeledValue structs which associate the label information to the added value.
More details and examples can be found in the documentation for the various accessor methods.
Much like accessing individual element of a list, iteration over a list can be done in two contexts:
- Calling iter to create a ConsIterator which iterates over the LabeledValue objects, or
- Calling iter_values to create a ValuesIterator which iterates over the contained values.
There are also two ways to collect the contents of an iterator into a new cons-list:
- CollectIntoHList collects the contents of an iterator as-is.
When this collection is performed on a ValuesIterator, the new
cons-list does not contain any label information (since the
ValueIteratoronly iterates over the contained values).
- CollectIntoLabeledHList collects the contents of an iterator with a new provided set of labels. This is particularly useful when the types of elements have changed during the iterator process (via MapAdapter) and the old labels are no longer valid (since a label can only has one associated type).
An example of both kinds of collection can be see in the MapAdapter documentation.
Iteration over heterogeneous cons-lists and labeled heterogeneous cons-lists.
Macro for creating type signature for a LCons label-only cons-list.
Macro for creation of a Cons-list.
Macro for creating an instance of an LCons label-only cons-lists.
Macro for creating labeled heterogeneous lists.
Macro for easily creating a label struct.
Main buildling block of a heterogeneous list.
Marker struct signifying
A value along with its label.
The end of a heterogeneous list.
Marker struct signifying
Trait for types that signify
A trait with information about a label.
Provides the length of a cons-list.
Lookup a specific element in a list by label.
Check to see if a target label is a list member.
Conversion trait for types that have a logical
A trait that provides access to contained 'values'.
Create a new cons-list.
A cons-list containing only labels.
A cons-list containing a set of labeled values.