[][src]Macro hyperbole::record

macro_rules! record {
    () => { ... };
    ($( $name:ident ),+ $(,)?) => { ... };
    ($( $name:ident : $type:ty ),+ $(,)?) => { ... };
    ($( $name:ident = $val:expr ),+ $(,)?) => { ... };
    ($( $name:ident ),+ , [ $( $tok:tt )* ]) => { ... };
    ($( $name:ident : $type:ty ),+ , [ $( $tok:tt )* ]) => { ... };
    ($( $name:ident = $val:expr ),+ , [ $( $tok:tt )* ]) => { ... };
}

Expands to either the type of an anonymous record, or a (~consty) expression that evaluates to an instantiated record.

A record is simply an hlist that contains named fields.

That is, record![a: u32, b: u32] is shorthand for Hlist![f![a: u32], f![b: u32]], and record![a = 4, b = 5] is shorthand for hlist![f![a = 4], f![b = 5]].

The syntax is equivalent to f!, but allows for multiple fields of the same form to be provided as a comma separated list.

use hyperbole::{f, record};

let mut one = record![a = 2, b = 4, c = "hi"];

// one is Copy because a, b, and c are Copy:
let two: record![a, b, c] = one;

// types may be specified like in f!:
let and: record![a: u32, b: i32, c: &str] = one;

// accessing fields happens exactly like w/ hlists (as records are hlists):
let a: &f![a] = one.get();
assert_eq!(2, **a);

let a: &mut f![a] = one.get_mut();
**a = 45;

let a: f![a] = one.pluck().0;
assert_eq!(45, *a);

// records (ie hlists) also impl Into for tuples, which may be easier to use:
let (a, b, c) = one.into();
assert_eq!(45, *a);
assert_eq!(4, *b);
assert_eq!("hi", *c);

Partial records

A group of tokens surrounded by brackets ([ ]) may be passed after all named fields to specify additional unnamed fields. Everything within said brackets will be appended to the Hlist! or hlist! invocation unchanged.

use hyperbole::{f, record, Hlist};

let my_record = record![a = 40, b = "hello", ["world"]];

let my_record_infer: record![a, b, [&str]] = my_record;
let my_record_concr: record![a: u32, b: &str, [&str]] = my_record;

let my_record_hlist_infer: Hlist![f![a], f![b], &str] = my_record;
let my_record_hlist_concr: Hlist![f![a: u32], f![b: &str], &str] = my_record;