1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#[cfg(test)]
use crate::Vector;

/// Create a vector out of named arguments
///
/// First token is the length of the `Vector`, so `vector! { 4 }` is a `Vec4`
/// After the length you can use `x`, `y`, `z`, `w` and `s`, `t`, `u`, `v` to
/// define the 1st, 2nd, 3rd and 4th elements, respectively. Remaining fields
/// are filled with `Default::default`
///
/// # Example
/// ```rust
/// use definitive::{Vector, vector};
///
/// assert_eq!(
///     vector! {
///         4,
///         y: 2,
///         z: 3
///     },
///     Vector::from([0, 2, 3, 0])
/// );
/// ```
#[macro_export]
macro_rules! vector {
    () => {
        Vector::from([])
    };
    ($l:expr, $($n:ident : $e:expr),+) => {
        {
            let mut __inner = [Default::default(); $l];

            $(
                __inner[vector!(secret____; $n)] = $e;
            )*

            Vector::from(__inner)
        }
    };
    (secret____; x) => { 0 };
    (secret____; y) => { 1 };
    (secret____; z) => { 2 };
    (secret____; w) => { 3 };
    (secret____; s) => { 0 };
    (secret____; t) => { 1 };
    (secret____; u) => { 2 };
    (secret____; v) => { 3 };
}

#[test]
fn empty() {
    assert_eq!(vector! {}, Vector::from([0u8; 0]));
}

#[test]
fn four() {
    assert_eq!(vector! { 4, x: 2, y: 4 }, Vector::from([2, 4, 0, 0]));
}