#stack-allocated #array #heap-allocated #array-vec #vec-t #size #moves

vec_to_array

Moves a heap allocated Vec<T> to an stack allocated array of type T and size N

5 releases

0.2.3 Oct 26, 2023
0.2.2 Oct 26, 2023
0.2.1 Oct 26, 2023
0.2.0 Oct 26, 2023
0.1.0 Oct 26, 2023

#936 in Algorithms

Download history 1/week @ 2024-02-19 8/week @ 2024-02-26 76/week @ 2024-04-01

76 downloads per month
Used in graphrs

MIT license

8KB
108 lines

vec_to_array

Moves a heap allocated Vec into a stack allocated array. In most cases you will want to prefer using try_into (https://doc.rust-lang.org/alloc/vec/struct.Vec.html#impl-TryFrom%3CVec%3CT,+A%3E%3E-for-%5BT;+N%5D) unless you need the array on stack for some reason.

let vec: Vec<i64> = vec![1, 2, 3];
let array: [i64; 3] = vec_to_array!(vec, i64, 3);
assert_eq!(array, [1, 2, 3]);

let vec: Vec<i32> = vec![1, 2, 3];
let array: Result<[i32; 3], VecToArrayError> = try_vec_to_array!(vec, i32, 3);
assert_eq!(array.unwrap(), [1, 2, 3]);

Motivation

For Vec, Into is not implement for arrays greater than a size of 12.

let v: Vec<i32> = vec![0; 768];
let arr: [i32; 768] = v.into(); /// will not compile

let v: Vec<i32> = vec![0; 768];
let arr: Result<[i32; 768], _ > = v.try_into(); /// Will work but is on the heap

Solution this crate adds vec_to_array! and try_vec_to_array!


lib.rs:

Moves a heap allocated Vec<T> to an stack allocated array of type T and size N.

No runtime deps