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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#![no_std]

// Copyright 2018 lazy-static.rs Developers
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0>. This file may not be
// copied, modified, or distributed except according to those terms.

/*! 
A macro for setting values to slices in batch

Using this macro it is possible to set values to slices
without setting each value individually

# Syntax

```ignore
set_slice! {
    SLICE = VALUE_1, VALUE_2, VALUE_3, ...;          // list
    SLICE = move VALUE;                              // move
    SLICE = clone REFERENCE;                         // clone ref
    SLICE = copy REFERENCE;                          // copy ref
    unsafe SLICE: (SIZE) = ref REFERENCE;            // unsafe copy ref
    ...
}
```
## Variable Definitions
`SLICE: &mut [T]` = name of slice \
`VALUE: impl Deref<Target = [T]> | AsRef<[T]>` = value to be stored in slice \
`SIZE: usize` = a constexpr that specifies the size of the slice \
`REFERENCE: &[T]` = a reference to a slice \
`copy`/`clone` = an identifier that speficies how to handle REFERENCE

## Examples
```rust
# #[macro_use]
# extern crate set_slice;
# fn main() {
let mut slice = [0; 3]; // example slice
let vec = [-1, -2];


set_slice! {
    slice = 1, 2, 3;
    slice[..2] = copy &vec;
}

assert_eq!(slice, [-1, -2, 3]);
# }
```

# Semantics and Notes on Syntax

## list
the list: `VALUE_1`, `VALUE_2`, `VALUE_3`, ... is counted and converted into an array \
after conversion it is has the same semantics as move applied to the new array

## move
the `VALUE` is moved into set_slice and dropped \
the contents of `VALUE` are stored into the slice

## copy
the `REFERENCE` `&[T]` values are copied into the slice \
`T` must implement `Copy`

## clone
the `REFERENCE` `&[T]` values are cloned into the slice \
`T` must implement `Clone`

## copy
the `REFERENCE` `&[T]` values are copied into the slice \
`T` must implement `Copy`

## unsafe copy
**VERY UNSAFE** \
the `REFERENCE` `&[T]` values are copied into the slice \
internally this uses ::core::mem::transmute_copy \
so, use this with caution, as it may cause undefined behaviour \
**VERY UNSAFE**

# Cargo features
This crate allows for use in no-std environment.
*/

#[doc(hidden)]
pub use core::ptr::swap as __swap_ptr;
#[doc(hidden)]
pub use core::mem::transmute_copy as __transmute_copy_mem;

#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! count {
    ( )        => {0usize};
    ($one:tt) => {1usize};
    ($($pairs:tt $_p:tt)*) => {
        count!($($pairs)*) << 1usize
    };
    ($odd:tt $($rest:tt)*) => {
        count!($($rest)*) | 1usize
    };
}

#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! __set_slice_internals {
    (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),* => move $slice:expr, $value:expr) => {{
        const LINE: usize = count!($($ln)*);

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

            assert_eq!(sl, vl, "line {}: value length ({}) is invalid, excepted: {}", LINE, vl, sl);
            slice.swap_with_slice(value);
        }

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

        assert_eq!(il, sl, "ln({}) input length invalid: {}, expected: {}", LINE, il, sl);

        __set_slice_internals!($option slice, input);
    }};
    ($($ln:tt),* => ref $slice:expr, $size:expr, $value:expr) => {{
        const LINE: usize = count!($($ln)*);
        
        #[inline(always)]
        fn set<T>(slice: &mut [T], value: &[T]) {
            let (sl, vl) = (slice.len(), value.len());

            assert_eq!(sl, $size, "line {}: slice length ({}) is invalid, excepted: {}", LINE, sl, $size);
            assert_eq!(vl, $size, "line {}: value length ({}) is invalid, excepted: {}", LINE, vl, $size);
            
            unsafe {
                let slice = &mut *(slice as *mut [T] as *mut [T; $size]);
                let value = &*(value as *const [T] as *const [T; $size]);

                *slice = $crate::__transmute_copy_mem(value);
            }
        }

        let input: &_ = $value;
        let slice = &mut $slice;

        set(slice, input);
    }};
}

/// a macro for setting parts of slices, see crate level docs for more info 
#[macro_export]
macro_rules! set_slice {
    // no range branches
    (@$($ln:tt),* => unsafe $slice:ident: ($size:expr) = ref $value:expr; $($rest:tt)*) => {
        __set_slice_internals!($($ln),* => ref $slice, $size, $value);
        set_slice!(@$($ln,)* 0 => $($rest)*);
    };

    (@$($ln:tt),* => $slice:ident = move $value:expr; $($rest:tt)*) => {
        __set_slice_internals!($($ln),* => move $slice, $value);
        set_slice!(@$($ln,)* 0 => $($rest)*);
    };

    (@$($ln:tt),* => $slice:ident = $option:ident $value:expr; $($rest:tt)*) => {
        __set_slice_internals!($($ln),* => $slice, $option $value);
        set_slice!(@$($ln,)* 0 => $($rest)*);
    };

    (@$($ln:tt),* => $slice:ident = $($value:expr),+; $($rest:tt)*) => {
        __set_slice_internals!($($ln),* => move $slice, [$($value),+]);
        set_slice!(@$($ln,)* 0 => $($rest)*);
    };

    // with range branches
    (@$($ln:tt),* => unsafe $slice:ident[$($range:tt)*]: ($size:expr) = ref $value:expr; $($rest:tt)*) => {
        __set_slice_internals!($($ln),* => ref $slice[$($range)*], $size, $value);
        set_slice!(@$($ln,)* 0 => $($rest)*);
    };

    (@$($ln:tt),* => $slice:ident[$($range:tt)*] = move $value:expr; $($rest:tt)*) => {
        __set_slice_internals!($($ln),* => move $slice[$($range)*], $value);
        set_slice!(@$($ln,)* 0 => $($rest)*);
    };

    (@$($ln:tt),* => $slice:ident[$($range:tt)*] = $option:ident $value:expr; $($rest:tt)*) => {
        __set_slice_internals!($($ln),* => $slice[$($range)*], $option $value);
        set_slice!(@$($ln,)* 0 => $($rest)*);
    };

    (@$($ln:tt),* => $slice:ident[$($range:tt)*] = $($value:expr),+; $($rest:tt)*) => {
        __set_slice_internals!($($ln),* => move $slice[$($range)*], [$($value),+]);
        set_slice!(@$($ln,)* 0 => $($rest)*);
    };

    // errors and terminals
    (@$($ln:tt),* => unsafe $slice:ident[$($range:tt)*]: ($size:expr) = $value:expr; $($rest:tt)*) => {
        compile_error!("Moving values into the slice is safe");
    };
    (@$($ln:tt),* => unsafe $slice:ident: ($size:expr) = $value:expr; $($rest:tt)*) => {
        compile_error!("Moving values into the slice is safe");
    };
    
    (@$($ln:tt),* => $slice:ident[$($range:tt)*]: ($size:expr) = ref $value:expr; $($rest:tt)*) => {
        compile_error!("Copying arbitrary references in unsafe");
    };
    (@$($ln:tt),* => $slice:ident: ($size:expr) = ref $value:expr; $($rest:tt)*) => {
        compile_error!("Copying arbitrary references in unsafe");
    };
    (@$($ln:tt),* => $slice:ident[$($range:tt)*] = ref $value:expr; $($rest:tt)*) => {
        compile_error!("Copying arbitrary references in unsafe");
    };
    (@$($ln:tt),* => $slice:ident= ref $value:expr; $($rest:tt)*) => {
        compile_error!("Copying arbitrary references in unsafe");
    };
    
    (@$($ln:tt),* => unsafe $slice:ident[$($range:tt)*] = ref $value:expr; $($rest:tt)*) => {
        compile_error!("Unkown size: size must be an expression surrouned by parentheses");
    };
    (@$($ln:tt),* => unsafe $slice:ident= ref $value:expr; $($rest:tt)*) => {
        compile_error!("Unkown size: size must be an expression surrouned by parentheses");
    };

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

    (@$($ln:tt),* => $slice:ident = ref $value:expr; $($rest:tt)*) => {
        compile_error!("Option is missing: value should be of the form: \"{copy, clone} ref value\"")
    };

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

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

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

    (@$($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, there is a range, but no slice");
    };
    (@$($ln:tt),* => $($rest:tt)+) => {
        compile_error!("Missing rvalue, there seems to be a missing slice to assign to");
    };
    ($($rest:tt)+) => {
        set_slice!(@0 => $($rest)+);
    };
}

#[cfg(test)]
mod tests {
    #[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] = move array;
            v[3..] = move vec;
        }
        // println!("{:?}", vec); // COMPILE ERROR: use after move

        assert_eq!(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_eq!(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_eq!(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_eq!(v, [0, 2, 3, 4, 5, 6, 7, 8]);
    }

    #[test]
    fn set_slice_test_unsafe() {
        #[derive(PartialEq, Debug)]
        struct A(i32);
        let mut v = [A(0), A(0), A(0), A(0), A(0), A(0), A(0), A(0)];
        let values = [A(4), A(5), A(6)];
        let array = [A(0), A(2)];
        let deref = [A(7), A(8)];

        set_slice! {
            unsafe v[1..=2]: (2) = ref &[A(5), A(3)];
            unsafe v[3..6]: (3) = ref &values;
            unsafe v[..2]: (2) = ref &array;
            unsafe v[6..]: (2) = ref &deref;
        }
        let _ = values;

        assert_eq!(v, [A(0), A(2), A(3), A(4), A(5), A(6), A(7), A(8)]);
    }
}