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
116
117
118
119
120
use std::ops::{Deref, DerefMut};
use std::hash::{Hash, Hasher};
use std::iter::FromIterator;

use serde_json::Value;

use super::{array_to_json, Prop};

#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Array(Vec<Prop>);

unsafe impl Sync for Array {}
unsafe impl Send for Array {}

impl Array {
    #[inline(always)]
    pub fn new() -> Self {
        Array(Vec::new())
    }
    #[inline(always)]
    pub fn with_capacity(cap: usize) -> Self {
        Array(Vec::with_capacity(cap))
    }

    #[inline]
    pub fn push<T>(&mut self, value: T)
    where
        T: Into<Prop>,
    {
        self.0.push(value.into())
    }

    #[inline]
    pub fn to_json(&self) -> Value {
        Value::Array(array_to_json(self))
    }
}

impl Deref for Array {
    type Target = Vec<Prop>;

    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl DerefMut for Array {
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl Hash for Array {
    #[inline]
    fn hash<H>(&self, state: &mut H)
    where
        H: Hasher,
    {
        for v in self {
            v.hash(state);
        }
    }
}

impl IntoIterator for Array {
    type Item = Prop;
    type IntoIter = ::std::vec::IntoIter<Self::Item>;

    #[inline(always)]
    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}
impl<'a> IntoIterator for &'a Array {
    type Item = &'a Prop;
    type IntoIter = ::std::slice::Iter<'a, Prop>;

    #[inline(always)]
    fn into_iter(self) -> Self::IntoIter {
        self.0.iter()
    }
}
impl<'a> IntoIterator for &'a mut Array {
    type Item = &'a mut Prop;
    type IntoIter = ::std::slice::IterMut<'a, Prop>;

    #[inline(always)]
    fn into_iter(self) -> Self::IntoIter {
        self.0.iter_mut()
    }
}

impl<T> From<Vec<T>> for Array
where
    T: Into<Prop>,
{
    #[inline(always)]
    fn from(vec: Vec<T>) -> Self {
        let mut a = Array::new();
        for v in vec {
            a.push(v.into());
        }
        a
    }
}

impl<T> FromIterator<T> for Array
where
    T: Into<Prop>,
{
    #[inline]
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let mut a = Array::new();
        for v in iter {
            a.push(v.into());
        }
        a
    }
}