Trait hlist::Find
[−]
[src]
pub trait Find<T, I> { fn get(&self) -> &T; fn get_mut(&mut self) -> &mut T; }
Find<T, I>
is implemented for an HList
if index I
of the HList
is a T
Rust's type inferencer can often produce a correct I
if there is exactly one T
in the HList
.
use hlist::{HList, Nil, Find}; // The type of list is Cons<i64, Cons<i32, Nil>> let list = Nil.push(0i32).push(1i64); // Here list satisfies the trait Find<i64, Here>. // The compiler infers the second type parameter. let a: i64 = *list.get(); assert!(a == 1); // Here list satisfies the trait Find<i32, There<Here>>. let b: i32 = *list.get(); assert!(b == 0);
Functions that need to look up values of a type in an HList given to them should get the index from the call site:
use hlist::{HList, Nil, Find}; fn foo<I, L: Find<i32, I>>(list: &L) -> i32 { *list.get() } let list = Nil.push("foo").push(5i32).push("bar"); assert!(foo(&list) == 5);
When foo()
is called, the compiler figures out the appropriate value for I
.
Required Methods
fn get(&self) -> &T
Retrieves a &T
.
Allows for type inferencing to act like type-directed search.
use hlist::{HList, Nil, Find}; let list = Nil.push(0i32).push(1i64); let a: i64 = *list.get(); assert!(a == 1);
fn get_mut(&mut self) -> &mut T
Retrieves a &mut T
.
Allows for type inferencing to act like type-directed search.
use hlist::{HList, Nil, Find}; let mut list = Nil.push(0i32).push(1i64); *list.get_mut() = 5i32; let a: i32 = *list.get(); assert!(a == 5);