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
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#![no_std]
/// any branch that has an '@' preceeding is internal to the macro
#[macro_export]
macro_rules! set_slice {
    (@count )        => {0usize};
    (@count $one:tt) => {1usize};
    (@count $($pairs:tt $_p:tt)*) => {
        set_slice!(@count $($pairs)*) << 1usize
    };
    (@count $odd:tt $($rest:tt)*) => {
        set_slice!(@count $($rest)*) | 1usize
    };

    (@@copy $slice:expr, $value:expr) => {
        $slice.copy_from_slice($value);
    };
    (@@clone $slice:expr, $value:expr) => {
        $slice.clone_from_slice($value);
    };
    (@@$option:ident $slice:expr, $value:expr) => {
        compile_error!(stringify!(invalid option $option, valid options are copy, clone))
    };
    (@@$($ln:tt),* => $slice:expr, $size:expr, $value:expr) => {{
        const LINE: usize = set_slice!(@count $($ln)*);

        // function used for type inference
        #[inline(always)]
        fn set<T>(slice: &mut [T], value: &mut [T]) {
            let (sl, vl) = (slice.len(), value.len());

            if sl != $size { // validate slice size
                panic!("line {}: slice length ({}) is invalid, excepted: {}", LINE, sl, $size)
            }

            if vl != $size { // validate value size
                panic!("line {}: value length ({}) is invalid, excepted: {}", LINE, vl, $size)
            }
            
            let value = value as *mut [T] as *mut [T; $size];
            let slice = slice as *mut [T] as *mut [T; $size];
            
            unsafe { $crate::core::ptr::swap(slice, value); }
        }

        let mut val = $value; // capture value
        set(&mut $slice, &mut val);
    }};
    (@@$($ln:tt),* => $slice:expr, $option:ident $value:expr) => {{
        let input = $value;
        let slice = &mut $slice;
        let (il, sl) = (input.len(), slice.len());

        if il != sl {
            panic!("ln({}) input length invalid: {}, expected: {}", set_slice!(@count $($ln)*), il, sl)
        }

        set_slice!(@@$option slice, input);
    }};

    // ln is line number, for better error messages
    // it is stored as a list of zeros, which is counted (O(log n)) when
    // the line number is needed

    // this pattern is for values that will be moved into the slice
    (@$($ln:tt),* => $slice:ident: ($size:expr) = $value:expr; $($rest:tt)*) => {
        set_slice!(@@$($ln),* => $slice, $size, $value);
        set_slice!(@$($ln,)* 0 => $($rest)*);
    };
    // this pattern is for values that will be moved into the slice
    (@$($ln:tt),* => $slice:ident[$($range:tt)*]: ($size:expr) = $value:expr; $($rest:tt)*) => {
        set_slice!(@@$($ln),* => $slice[$($range)*], $size, $value);
        set_slice!(@$($ln,)* 0 => $($rest)*);
    };

    (@$($ln:tt),* => $slice:ident[$($range:tt)*]: $($rest:tt)*) => {
        compile_error!("invalid size: size must be an expression surrouned by parentheses");
    };

    // this pattern if for values that will be copied/cloned into the slice
    (@$($ln:tt),* => $slice:ident = $option:ident $value:expr; $($rest:tt)*) => {
        set_slice!(@@$($ln),* => $slice, $option $value);
        set_slice!(@$($ln,)* 0 => $($rest)*);
    };

    // this pattern if for values that will be a list of expressions
    (@$($ln:tt),* => $slice:ident = $($value:expr),+; $($rest:tt)*) => {
        set_slice!(@@$($ln),* => $slice, set_slice!(@count $( $value )+), [$($value),+]);
        set_slice!(@$($ln,)* 0 => $($rest)*);
    };

    // this pattern if for values that will be copied/cloned into the slice
    (@$($ln:tt),* => $slice:ident[$($range:tt)*] = $option:ident $value:expr; $($rest:tt)*) => {
        set_slice!(@@$($ln),* => $slice[$($range)*], $option $value);
        set_slice!(@$($ln,)* 0 => $($rest)*);
    };

    // this pattern if for values that will be a list of expressions
    (@$($ln:tt),* => $slice:ident[$($range:tt)*] = $($value:expr),+; $($rest:tt)*) => {
        set_slice!(@@$($ln),* => $slice[$($range)*], set_slice!(@count $( $value )+), [$($value),+]);
        set_slice!(@$($ln,)* 0 => $($rest)*);
    };

    // this pattern if for values that will be copied/cloned into the slice
    (@$($ln:tt),* => $slice:ident[$($range:tt)*] = ref $value:expr; $($rest:tt)*) => {
        compile_error!("option is missing: value should be of the form: \"{copy, clone} ref value\"")
    };

    (@$($ln:tt),* => $slice:ident[$($range:tt)*] = ; $($rest:tt)*) => {
        compile_error!("there must be a non-zero number of arguments in a list");
    };

    (@$($ln:tt),* => $slice:ident[$($range:tt)*] $($rest:tt)*) => {
        compile_error!("punctuation is missing!");
    };

    (@$($ln:tt),* => ) => {};
    () => {};
    
    (@$($ln:tt),* => [$($range:tt)*] $($rest:tt)*) => {
        compile_error!("missing identifier");
    };
    (@$($ln:tt),* => $($rest:tt)+) => {
        compile_error!("missing rvalue");
    };
    ($($rest:tt)+) => {
        set_slice!(@0 => $($rest)+);
    };
}

#[cfg(test)]
mod tests {
    extern crate std;
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }

    #[test]
    fn test_move_values() {
        let mut v = [0; 6];
        let value = 0;
        let array = [2, 3]; 
        let vec = [4, 5, 6];

        set_slice! {
            v[0..1] = value;
            v[1..3]: (2) = array;
            v[3..]: (3) = vec;
        }
        // println!("{:?}", vec); // COMPILE ERROR: use after move

        assert!(&v == &[0, 2, 3, 4, 5, 6]);
    }

    #[test]
    fn test_full_range() {
        let mut v = [0; 10];

        set_slice! {
            v[..] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9;
        }

        assert!(&v == &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);

        let mut v = [0; 10];

        set_slice! {
            v = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9;
        }

        assert!(&v == &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
    }

    #[test]
    fn set_slice_test() {
        let mut v = [0; 8];
        let values = [4, 5, 6];
        let array = [0, 2];
        let deref = [7, 8];

        set_slice! {
            v[1..=2] = copy &[5, 3];
            v[3..6] = copy &values;
            v[..2] = copy &array;
            v[6..] = copy &deref;
        }
        let _ = values;

        assert!(&v == &[0, 2, 3, 4, 5, 6, 7, 8]);
    }
}