libutils_array/
conversions.rs1use super::Array;
7
8use alloc::{
10 vec::Vec,
11 format
12};
13
14use libutils_issue::{
16 Issue,
17 Severity
18};
19
20
21impl<Type, const M: usize, const N: usize> From<[Type; N]> for Array<Type, M> where [(); M - N]: {
27 fn from(value: [Type; N]) -> Self {return Array::from_iter(value)}
28}
29
30impl<Type, const N: usize, Generator: FnMut(usize) -> Type> From<Generator> for Array<Type, N> {
32 fn from(mut value: Generator) -> Self {
33 let mut array = Self::new();
34 for index in 0..N {
35 array.push(value(index));
36 };
37 return array;
38 }
39}
40
41impl<Type, const N: usize> TryFrom<Vec<Type>> for Array<Type, N> {
43 type Error = Issue;
44 fn try_from(value: Vec<Type>) -> Result<Self, Self::Error> {return if value.len() > N {Err(Issue {
45 name: "Conversion from `Vec` to `Array` failed",
46 description: Some(format!("Vec of length {} but N = {N}", value.len())),
47 severity: Severity::Error
48 })} else {Ok(Self::from_iter(value.into_iter()))}}
49}
50
51
52impl<Type, const N: usize> Into<Vec<Type>> for Array<Type, N> {
58 fn into(self) -> Vec<Type> {return Vec::from_iter(self.into_iter())}
59}