[][src]Macro hyperbole::access

macro_rules! access {
    ($var:ident = $rec:ident . $field:ident) => { ... };
    (mut $var:ident = $rec:ident . $field:ident) => { ... };
    (&$rec:ident . $field:ident) => { ... };
    (&mut $rec:ident . $field:ident) => { ... };
}

Access a named field in an anonymous record.

First form: evaluates to a statement that moves a record field into a variable

Immutable: access!(new_variable = my_record.field_name)

Mutable: access!(mut new_variable = my_record.field_name)

The result will be a statement that extracts a field, binds its value to the specified identifier, and shadows the original record with any remaining fields.

The new record will be declared as mut in all cases.

use hyperbole::{access, record};

let rec = record![x = 1000, y = "hello world"];

access!(s = rec.y);
assert_eq!(s, "hello world");

// NOTE: 'rec' is now eqivalent to 'let mut rec = record![x: 1000]'

access!(n = rec.x);
assert_eq!(n, 1000);

// NOTE: 'rec' is now empty and contains no fields
let _: record![] = rec;

Second form: evaluates to an expression that borrows a field

For immutable borrows: access!(&my_record.field_name)

For mutable borrows: access!(&mut my_record.field_name)

use hyperbole::{access, record};

let mut rec = record![x = 1000, y = "hello world"];

let _: &u64 = access!(&rec.x);
let _: &mut u64 = access!(&mut rec.x);

assert_eq!(1000, *access!(&rec.x));
assert_eq!("hello world", *access!(&rec.y));

*access!(&mut rec.y) = "changed";
assert_eq!("changed", *access!(&rec.y));

// the type and mutability of 'rec' has not changed
let _: record![x: u64, y: &str] = rec;