Skip to main content

libutils_array/
conversions.rs

1//^
2//^ HEAD
3//^
4
5//> HEAD -> SUPER
6use super::Array;
7
8//> HEAD -> ALLOC
9use alloc::vec::Vec;
10
11
12//^
13//^ FROM
14//^
15
16//> FROM -> FIXED TO VARIABLE
17impl<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
21//> FROM -> FUNCTION
22impl<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
32//> FROM -> VEC
33impl<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
41//^
42//^ INTO
43//^
44
45//> INTO -> VEC
46impl<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}