[][src]Macro structural::field_pat

macro_rules! field_pat {
    () => { ... };
    ($f0:pat,$f1:pat,$f2:pat,$f3:pat,$f4:pat,$f5:pat,$f6:pat,$f7:pat $(,)? ) => { ... };
    ($f0:pat,$f1:pat,$f2:pat,$f3:pat,$f4:pat,$f5:pat,$f6:pat,$f7:pat, $($rem:pat),* $(,)? ) => { ... };
    ( $($f:pat),* $(,)? ) => { ... };
}

Macro to destructure the tuple returned by StructuralExt methods that access multiple fields.

This macro is most useful when destructuring a tuple of over 8 fields, since accessing over 8 fields returns a tuple of tuples (8 fields each).

StructuralExt Example

These examples demonstrate both the return value of StructuralExt methods that return over 8 fields, as well as destructuring the return value using the field_pat macro.

Calling StructuralExt::fields:

use structural::{field_pat, fp, StructuralExt};

let arr = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27];

assert_eq!(
    arr.fields(fp!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ,15, 16)),
    (
        (&10, &11, &12, &13, &14, &15, &16, &17),
        (&18, &19, &20, &21, &22, &23, &24, &25),
        (&26,)
    )
);

let field_pat!(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b0, b1, b2, b3, b4, b5, b6)=
    arr.fields(fp!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ,15, 16));

assert_eq!(
    [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b0, b1, b2, b3, b4, b5, b6],
    [&10, &11, &12, &13, &14, &15, &16, &17, &18, &19, &20, &21, &22, &23, &24, &25, &26],
);

Calling StructuralExt::fields_mut:

use structural::{field_pat, fp, StructuralExt};

let mut arr = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27];

assert_eq!(
    arr.fields_mut(fp!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ,15, 16)),
    (
        (&mut 10, &mut 11, &mut 12, &mut 13, &mut 14, &mut 15, &mut 16, &mut 17),
        (&mut 18, &mut 19, &mut 20, &mut 21, &mut 22, &mut 23, &mut 24, &mut 25),
        (&mut 26,)
    )
);

let field_pat!(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b0, b1, b2, b3, b4, b5, b6)=
    arr.fields_mut(fp!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ,15, 16));

assert_eq!(
    [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b0, b1, b2, b3, b4, b5, b6],
    [
        &mut 10, &mut 11, &mut 12, &mut 13, &mut 14, &mut 15, &mut 16, &mut 17,
        &mut 18, &mut 19, &mut 20, &mut 21, &mut 22, &mut 23, &mut 24, &mut 25,
        &mut 26
    ],
);

Calling StructuralExt::into_fields:

use structural::{field_pat, fp, StructuralExt};

let arr = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27];

assert_eq!(
    arr.into_fields(fp!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ,15, 16)),
    (
        (10, 11, 12, 13, 14, 15, 16, 17),
        (18, 19, 20, 21, 22, 23, 24, 25),
        (26,)
    )
);

let field_pat!(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b0, b1, b2, b3, b4, b5, b6)=
    arr.into_fields(fp!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ,15, 16));

assert_eq!(
    [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b0, b1, b2, b3, b4, b5, b6],
    [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26],
);

Example

This demonstrates what values this pattern macro destructures.

use structural::field_pat;

let field_pat!() = ();

let field_pat!(f0) = (0,);

let field_pat!(f0, f1) = (0, 1);

let field_pat!(f0, f1, f2, f3, f4, f5, f6, f7) = (0, 1, 2, 3, 4, 5, 6, 7);

let field_pat!(f0, f1, f2, f3, f4, f5, f6, f7, f8) = (
    (0, 1, 2, 3, 4, 5, 6, 7),
    (8,),
);

let field_pat!(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b0, b1, b2, b3, b4, b5) = (
    (0, 1, 2, 3, 4, 5, 6, 7),
    (8, 9, 10, 11, 12, 13, 14 ,15),
);

let field_pat!(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, b0, b1, b2, b3, b4, b5, b6) = (
    (0, 1, 2, 3, 4, 5, 6, 7),
    (8, 9, 10, 11, 12, 13, 14 ,15),
    (16,),
);