1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use crate::{arrayvec::ArrayVec, errors::CapacityError, len::LengthType, mem::SpareMemoryPolicy};
use core::{convert::TryFrom, mem, ptr};

impl<T, L, SM, const C: usize> TryFrom<&[T]> for ArrayVec<T, L, SM, C>
where
    T: Clone,
    L: LengthType,
    SM: SpareMemoryPolicy<T>,
{
    type Error = CapacityError;

    #[inline]
    fn try_from(s: &[T]) -> Result<Self, Self::Error> {
        if s.len() > Self::CAPACITY {
            return Err(CapacityError {});
        }
        let mut tmp = Self::new();
        unsafe {
            tmp._clone_from_unchecked(s);
        }
        Ok(tmp)
    }
}

impl<T, L, SM, const C: usize> TryFrom<&mut [T]> for ArrayVec<T, L, SM, C>
where
    T: Clone,
    L: LengthType,
    SM: SpareMemoryPolicy<T>,
{
    type Error = CapacityError;

    #[inline]
    fn try_from(s: &mut [T]) -> Result<Self, Self::Error> {
        if s.len() > Self::CAPACITY {
            return Err(CapacityError {});
        }
        let mut tmp = Self::new();
        unsafe {
            tmp._clone_from_unchecked(s);
        }
        Ok(tmp)
    }
}

impl<T, L, SM, const C: usize, const N: usize> TryFrom<[T; N]> for ArrayVec<T, L, SM, C>
where
    L: LengthType,
    SM: SpareMemoryPolicy<T>,
{
    type Error = CapacityError;

    #[inline]
    fn try_from(a: [T; N]) -> Result<Self, Self::Error> {
        if N > Self::CAPACITY {
            return Err(CapacityError {});
        }
        let mut tmp = Self::new();
        unsafe {
            ptr::copy_nonoverlapping(a.as_ptr(), tmp.as_mut_ptr(), N);
            tmp.set_len(N);
        }
        mem::forget(a);
        Ok(tmp)
    }
}

#[cfg(test)]
mod testing {
    use crate as cds;
    use cds::{arrayvec::ArrayVec, errors::CapacityError, len::Usize, mem::Uninitialized};
    type A = ArrayVec<u64, Usize, Uninitialized, 7>;

    #[test]
    fn test_try_from_slice() {
        let a = A::try_from([1, 2, 3].as_ref()).unwrap();
        assert_eq!(a, [1, 2, 3]);
    }

    #[test]
    fn test_try_from_slice_err() {
        assert!(matches!(
            A::try_from([1, 2, 3, 4, 5, 6, 7, 8].as_ref()),
            Err(CapacityError)
        ));
    }

    #[test]
    fn test_try_from_mut_slice() {
        let a = A::try_from([1, 2, 3].as_mut()).unwrap();
        assert_eq!(a, [1, 2, 3]);
    }

    #[test]
    fn test_try_from_mut_slice_err() {
        assert!(matches!(
            A::try_from([1, 2, 3, 4, 5, 6, 7, 8].as_mut()),
            Err(CapacityError)
        ));
    }

    #[test]
    fn test_try_from_array() {
        let a = A::try_from([1, 2, 3]).unwrap();
        assert_eq!(a, [1, 2, 3]);
    }

    #[test]
    fn test_try_from_array_err() {
        assert!(matches!(
            A::try_from([1, 2, 3, 4, 5, 6, 7, 8]),
            Err(CapacityError)
        ));
    }
}