Struct frunk::hlist::HCons

source ·
pub struct HCons<H, T> {
    pub head: H,
    pub tail: T,
}
Expand description

Represents the most basic non-empty HList. Its value is held in head while its tail is another HList.

Fields§

§head: H§tail: T

Implementations§

Returns the head of the list and the tail of the list as a tuple2. The original list is consumed

Examples
let h = hlist!("hi");
let (h, tail) = h.pop();
assert_eq!(h, "hi");
assert_eq!(tail, hlist![]);
Run

Returns the length of a given HList

Examples
let h = hlist![1, "hi"];
assert_eq!(h.len(), 2);
Run

Prepend an item to the current HList

Examples
let h1 = hlist![1, "hi"];
let h2 = h1.prepend(true);
let (a, (b, c)) = h2.into_tuple2();
assert_eq!(a, true);
assert_eq!(b, 1);
assert_eq!(c, "hi");
Run

Consume the current HList and return an HList with the requested shape.

sculpt allows us to extract/reshape/scult the current HList into another shape, provided that the requested shape’s types are are contained within the current HList.

The Indices type parameter allows the compiler to figure out that Ts and Self can be morphed into each other.

Examples
let h = hlist![9000, "joe", 41f32, true];
let (reshaped, remainder): (Hlist![f32, i32, &str], _) = h.sculpt();
assert_eq!(reshaped, hlist![41f32, 9000, "joe"]);
assert_eq!(remainder, hlist![true]);
Run

Reverse the HList.

Examples
assert_eq!(hlist![].into_reverse(), hlist![]);

assert_eq!(
    hlist![1, "hello", true, 42f32].into_reverse(),
    hlist![42f32, true, "hello", 1],
)
Run

Return an HList where the contents are references to the original HList on which this method was called.

Examples
assert_eq!(hlist![].to_ref(), hlist![]);

assert_eq!(hlist![1, true].to_ref(), hlist![&1, &true]);
Run

Return an HList where the contents are mutable references to the original HList on which this method was called.

Examples
assert_eq!(hlist![].to_mut(), hlist![]);

assert_eq!(hlist![1, true].to_mut(), hlist![&mut 1, &mut true]);
Run

Apply a function to each element of an HList.

This transforms some Hlist![A, B, C, ..., E] into some Hlist![T, U, V, ..., Z]. A variety of types are supported for the folder argument:

  • An hlist![] of closures (one for each element).
  • A single closure (for mapping an HList that is homogenous).
  • A single Poly.
Examples
use ::frunk::HNil;

assert_eq!(HNil.map(HNil), HNil);

let h = hlist![1, false, 42f32];

// Sadly we need to help the compiler understand the bool type in our mapper

let mapped = h.to_ref().map(hlist![
    |&n| n + 1,
    |b: &bool| !b,
    |&f| f + 1f32]);
assert_eq!(mapped, hlist![2, true, 43f32]);

// There is also a value-consuming version that passes values to your functions
// instead of just references:

let mapped2 = h.map(hlist![
    |n| n + 3,
    |b: bool| !b,
    |f| f + 8959f32]);
assert_eq!(mapped2, hlist![4, true, 9001f32]);
Run

Perform a left fold over an HList.

This transforms some Hlist![A, B, C, ..., E] into a single value by visiting all of the elements in left-to-right order. A variety of types are supported for the mapper argument:

  • An hlist![] of closures (one for each element).
  • A single closure (for folding an HList that is homogenous).

The accumulator can freely change type over the course of the call. When called with a list of N functions, an expanded form of the implementation with type annotations might look something like this:

let acc: Acc0 = init_value;
let acc: Acc1 = f1(acc, x1);
let acc: Acc2 = f2(acc, x2);
let acc: Acc3 = f3(acc, x3);
...
let acc: AccN = fN(acc, xN);
acc
Run
Examples
let nil = hlist![];

assert_eq!(nil.foldl(hlist![], 0), 0);

let h = hlist![1, false, 42f32];

let folded = h.to_ref().foldl(
    hlist![
        |acc, &i| i + acc,
        |acc, b: &bool| if !b && acc > 42 { 9000f32 } else { 0f32 },
        |acc, &f| f + acc
    ],
    1
);

assert_eq!(42f32, folded);

// There is also a value-consuming version that passes values to your folding
// functions instead of just references:

let folded2 = h.foldl(
    hlist![
        |acc, i| i + acc,
        |acc, b: bool| if !b && acc > 42 { 9000f32 } else { 0f32 },
        |acc, f| f + acc
    ],
    8918
);

assert_eq!(9042f32, folded2)
Run

Perform a right fold over an HList.

This transforms some Hlist![A, B, C, ..., E] into a single value by visiting all of the elements in reverse order. A variety of types are supported for the mapper argument:

  • An hlist![] of closures (one for each element).
  • A single closure (for folding an HList that is homogenous), taken by reference.

The accumulator can freely change type over the course of the call.

Comparison to foldl

While the order of element traversal in foldl may seem more natural, foldr does have its use cases, in particular when it is used to build something that reflects the structure of the original HList (such as folding an HList of Options into an Option of an HList). An implementation of such a function using foldl will tend to reverse the list, while foldr will tend to preserve its order.

The reason for this is because foldr performs what is known as “structural induction;” it can be understood as follows:

  • Write out the HList in terms of h_cons and HNil.
  • Substitute each h_cons with a function, and substitute HNil with init
the list:
    h_cons(x1, h_cons(x2, h_cons(x3, ...h_cons(xN, HNil))...)))

becomes:
       f1( x1,    f2( x2,    f3( x3, ...   fN( xN, init))...)))
Run
Examples
let nil = hlist![];

assert_eq!(nil.foldr(hlist![], 0), 0);

let h = hlist![1, false, 42f32];

let folded = h.foldr(
    hlist![
        |i, acc| i + acc,
        |b: bool, acc| if !b && acc > 42f32 { 9000 } else { 0 },
        |f, acc| f + acc
    ],
    1f32
);

assert_eq!(9001, folded)
Run

Borrow an element by type from an HList.

Examples
let h = hlist![1i32, 2u32, "hello", true, 42f32];

// Often, type inference can figure out the type you want.
// You can help guide type inference when necessary by
// using type annotations.
let b: &bool = h.get();
if !b { panic!("no way!") };

// If space is tight, you can also use turbofish syntax.
// The Index is still left to type inference by using `_`.
match *h.get::<u32, _>() {
    2 => { },
    _ => panic!("it can't be!!"),
}
Run

Mutably borrow an element by type from an HList.

Examples
let mut h = hlist![1i32, true];

// Type inference ensures we fetch the correct type.
*h.get_mut() = false;
*h.get_mut() = 2;
// *h.get_mut() = "neigh";  // Won't compile.

assert_eq!(h, hlist![2i32, false]);
Run

Remove an element by type from an HList.

The remaining elements are returned along with it.

Examples
let list = hlist![1, "hello", true, 42f32];

// Often, type inference can figure out the target type.
let (b, list): (bool, _) = list.pluck();
assert!(b);

// When type inference will not suffice, you can use a turbofish.
// The Index is still left to type inference by using `_`.
let (s, list) = list.pluck::<i32, _>();

// Each time we plucked, we got back a remainder.
// Let's check what's left:
assert_eq!(list, hlist!["hello", 42.0])
Run

Turns an HList into nested Tuple2s, which are less troublesome to pattern match and have a nicer type signature.

Examples
let h = hlist![1, "hello", true, 42f32];

// We now have a much nicer pattern matching experience
let (first,(second,(third, fourth))) = h.into_tuple2();

assert_eq!(first ,       1);
assert_eq!(second, "hello");
assert_eq!(third ,    true);
assert_eq!(fourth,   42f32);
Run

Trait Implementations§

The resulting type after applying the + operator.
Performs the + operation. Read more

Implementation when the pluck target key is in the head.

Returns a pair consisting of the value pointed to by the target key and the remainder.

Implementation when the pluck target key is in the tail.

Returns a pair consisting of the value pointed to by the target key and the remainder.
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Convert a coproduct into another that can hold its variants. Read more
Use functions to fold a coproduct into a single value. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.

Implementation for folding over an HList using a single function that can handle all cases

let h = hlist![1, 2, 3, 4, 5];

let r: isize = h.foldl(|acc, next| acc + next, 0);
assert_eq!(r, 15);
Run
Perform a left fold over an HList. Read more
Perform a left fold over an HList. Read more
Perform a right fold over an HList. Read more
Perform a right fold over an HList. Read more
Returns the length of a given HList type without making use of any references, or in fact, any values at all. Read more
👎Deprecated since 0.1.31: Please use LEN instead
Returns the length of a given HList type without making use of any references, or in fact, any values at all. Read more
👎Deprecated since 0.1.30: Please use len() or static_len() instead.
Returns the length of a given HList Read more
Prepends an item to the current HList Read more
Apply a function to each element of an HList. Read more
Apply a function to each element of an HList. Read more
Apply a function to each element of an HList. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Reverses a given data structure.
The 0 element in the output tuple
The 1 element in the output tuple
Turns an HList into nested Tuple2s, which are less troublesome to pattern match and have a nicer type signature. Read more
The 0 element in the output tuple
The 1 element in the output tuple
Turns an HList into nested Tuple2s, which are less troublesome to pattern match and have a nicer type signature. Read more

Implementation when we have a non-empty HCons holding a label in its head

Turns the current HList into an unlabelled one. Read more
Turns the current HList into a value-labelled one. Read more
Performs the indexed conversion.
Performs the indexed conversion.
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Returns a pair consisting of the value pointed to by the target key and the remainder.

Implementation when the pluck target is in the tail

What is left after you pluck the target from the Self
Remove an element by type from an HList. Read more

Implementation when the pluck target is in head

What is left after you pluck the target from the Self
Remove an element by type from an HList. Read more

Implementation for when we have a non-empty HCons target

Indices is HCons<IndexHead, IndexTail> here because the compiler is being asked to figure out the Index for Plucking the first item of type THead out of Self and the rest (IndexTail) is for the Plucker’s remainder induce.

Consumes the current HList and returns an HList with the requested shape. Read more
Borrow an element by type from an HList. Read more
Mutably borrow an element by type from an HList. Read more
Borrow an element by type from an HList. Read more
Mutably borrow an element by type from an HList. Read more

Allow the combination of any two HLists having the same structure if all of the sub-element types are also Semiups

Associative operation taking which combines two values. Read more

Non-trivial implementation of Transmogrifier where similarly-shaped Source and Target types are both Labelled HLists, but do not immediately transform into one another due to mis-matched fields, possibly recursively so.

Consume this current object and return an object of the Target type. Read more

Implementation of Transmogrifier for when the target is an HList, and the Source is a plucked HList.

Consume this current object and return an object of the Target type. Read more

Implementation of Transmogrifier for when the Target is empty and the Source is non-empty.

Consume this current object and return an object of the Target type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Extract a subset of the possible types in a coproduct (or get the remaining possibilities) Read more

Attempt to extract a value from a subset of the types.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Performs the indexed conversion.
Consumes the current HList and returns an HList with the requested shape. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.