Crates.io | set_slice |
lib.rs | set_slice |
version | 0.3.0 |
source | src |
created_at | 2018-07-16 19:35:38.641964 |
updated_at | 2018-07-18 01:10:47.187344 |
description | A macro for assigning values to slices |
homepage | |
repository | https://github.com/KrishnaSannasi/published_crates/tree/master/set_slice |
max_upload_size | |
id | 74554 |
size | 30,428 |
A Rust macro for easily assigning to slices
you can set the entire contents of the slice to whatever you want
let slice = &mut [0; 3] as &mut [i32]; // this is to simulate having only a slice without knowning its size
set_slice! {
slice = 1, 2, 3; // this list is internally counted and converted to an array at compile-time
}
assert_eq!(slice, [1, 2, 3]);
// ... or you can only set parts of the slice
let slice = &mut [0; 5] as &mut [i32];
set_slice! {
slice[..3] = 1, 2, 3;
}
assert_eq!(slice, [1, 2, 3, 0, 0]);
you can also do multiple assigns in one macro call
let slice = &mut [0; 5] as &mut [i32];
set_slice! {
slice[..2] = 1, 2;
slice[3..] = 4, 5;
}
assert_eq!(slice, [1, 2, 0, 4, 5]);
You can use expressions to set to the slices, either as values to be moved in, or as references if they are move values you must specify a const expression size in parentheses
let slice = &mut [0; 5] as &mut [i32];
let array = [1, 2];
let vec = vec![3, 4];
set_slice! {
slice[..2] = move array;
slice[3..] = move vec; // vec is moved into set_slice
}
println!("array = {:?}", array); // fine, array is a copy type
// println!("vec = {:?}", vec); // compile time error, vec is moved into the set_slice and dropped
assert_eq!(slice, [1, 2, 0, 3, 4]);
but you don't have to move into set_slice if you get a reference with references you must specify if the contents should be copied or cloned but they must derive Copy or Clone respectively
let slice = &mut [0; 5] as &mut [i32];
let array = [1, 2];
let vec = vec![3, 4];
set_slice! {
slice[..2] = copy &array; // array is NOT moved into set_slice, and contents are copied
slice[3..] = copy &vec; // vec is NOT moved into set_slice, and contents are copied
}
println!("array = {:?}", array); // this is fine, array was borrowed
println!("vec = {:?}", vec); // this is fine, vec was borrowed
assert_eq!(slice, [1, 2, 0, 3, 4]);
#[derive(Clone, Debug, PartialEq)]
enum A { Zero, One };
let slice: [A; 5] = [A::Zero, A::Zero, A::Zero, A::Zero, A::Zero];
let slice = &mut slice as &mut [A];
let array = [A::One, A::One];
let vec = vec![A::One; 2];
set_slice! {
slice[..2] = clone &array; // array is NOT moved into set_slice, and contents are cloned
slice[3..] = clone &vec; // vec is NOT moved into set_slice, and contents are cloned
// slice[3..] = copy &vec; // this won't work because 'A' is not a copy type
}
println!("array = {:?}", array); // this is fine, array was borrowed
println!("vec = {:?}", vec); // this is fine, vec was borrowed
assert_eq!(slice, [A::One, A::One, A::Zero, A::One, A::One]);
these ranges can be mixed and matched with the other sub-sections
let slice = &mut [0; 3] as &mut [i32];
let init = 1;
let end = 2;
set_slice! {
slice = 1, 2, 3;
slice[..] = 1, 2, 3;
slice[0..] = 1, 2, 3;
slice[..3] = 1, 2, 3;
slice[0..3] = 1, 2, 3;
slice[0..2] = 1, 2;
slice[1..2] = 2;
slice[init..2] = 2;
slice[1..end] = 2;
slice[init..end] = 2;
slice[init..] = 2, 3;
slice[..end] = 1, 2;
}
println!("{:?}", slice);
panic!();
let slice = &mut [0; 3] as &mut [i32];
let vec_move = vec![1, 2, 3];
set_slice! {
slice = move vec_move;
}
let vec_move = vec![1, 2, 3];
set_slice! {
slice[..] = move vec_move;
}
let vec_move = vec![1, 2];
set_slice! {
slice[..2] = move vec_move;
}
let slice = &mut [0; 3] as &mut [i32];
let array = [1, 2, 3];
let vec = vec![1, 2, 3];
// only works if slice implements copy
set_slice! {
slice = copy &vec;
slice = copy &array;
slice[..2] = copy &vec[1..];
slice[..2] = copy &array[1..];
}
// only works if slice implements clone
set_slice! {
slice = clone &vec;
slice = clone &array;
slice[..2] = clone &vec[1..];
slice[..2] = clone &array[1..];
}
// works with any type, but is incredibly unsafe
set_slice! {
unsafe slice: (3) = ref &vec;
unsafe slice: (3) = ref &array;
unsafe slice[..2]: (2) = ref &vec[1..];
unsafe slice[..2]: (2) = ref &array[1..];
}
all of these are when using unsafe slice assignment