libutils_array/
conversions.rs1use super::Array;
7
8use alloc::vec::Vec;
10
11
12impl<Type, const M: usize, const N: usize> From<[Type; N]> for Array<Type, M> where [(); M - N]: {
18 fn from(value: [Type; N]) -> Self {return Array::from_iter(value)}
19}
20
21impl<Type, const N: usize, Generator: FnMut(usize) -> Type> From<Generator> for Array<Type, N> {
23 fn from(mut value: Generator) -> Self {
24 let mut array = Self::new();
25 for index in 0..N {
26 array.push(value(index));
27 };
28 return array;
29 }
30}
31
32impl<Type, const N: usize> TryFrom<Vec<Type>> for Array<Type, N> {
34 type Error = &'static str;
35 fn try_from(value: Vec<Type>) -> Result<Self, Self::Error> {return if value.len() > N {
36 Err("not enough capacity in array")
37 } else {Ok(Self::from_iter(value.into_iter()))}}
38}
39
40
41impl<Type, const N: usize> Into<Vec<Type>> for Array<Type, N> {
47 fn into(self) -> Vec<Type> {return Vec::from_iter(self.into_iter())}
48}