Skip to main content

tattoy_wezterm_dynamic/
array.rs

1use crate::Value;
2use core::cmp::Ordering;
3use core::iter::FromIterator;
4use core::ops::{Deref, DerefMut};
5
6#[cfg(not(feature = "std"))]
7extern crate alloc;
8
9use alloc::vec::Vec;
10
11#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
12pub struct Array {
13    inner: Vec<Value>,
14}
15
16impl Ord for Array {
17    fn cmp(&self, other: &Self) -> Ordering {
18        let self_ptr = self as *const Self;
19        let other_ptr = other as *const Self;
20        self_ptr.cmp(&other_ptr)
21    }
22}
23
24impl PartialOrd for Array {
25    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
26        Some(self.cmp(other))
27    }
28}
29
30impl From<Vec<Value>> for Array {
31    fn from(inner: Vec<Value>) -> Self {
32        Self { inner }
33    }
34}
35
36impl Drop for Array {
37    fn drop(&mut self) {
38        self.inner.drain(..).for_each(crate::drop::safely);
39    }
40}
41
42fn take(array: Array) -> Vec<Value> {
43    let array = core::mem::ManuallyDrop::new(array);
44    unsafe { core::ptr::read(&array.inner) }
45}
46
47impl Array {
48    pub fn new() -> Self {
49        Array { inner: Vec::new() }
50    }
51}
52
53impl Deref for Array {
54    type Target = Vec<Value>;
55
56    fn deref(&self) -> &Self::Target {
57        &self.inner
58    }
59}
60
61impl DerefMut for Array {
62    fn deref_mut(&mut self) -> &mut Self::Target {
63        &mut self.inner
64    }
65}
66
67impl IntoIterator for Array {
68    type Item = Value;
69    type IntoIter = <Vec<Value> as IntoIterator>::IntoIter;
70
71    fn into_iter(self) -> Self::IntoIter {
72        take(self).into_iter()
73    }
74}
75
76impl<'a> IntoIterator for &'a Array {
77    type Item = &'a Value;
78    type IntoIter = <&'a Vec<Value> as IntoIterator>::IntoIter;
79
80    fn into_iter(self) -> Self::IntoIter {
81        self.iter()
82    }
83}
84
85impl<'a> IntoIterator for &'a mut Array {
86    type Item = &'a mut Value;
87    type IntoIter = <&'a mut Vec<Value> as IntoIterator>::IntoIter;
88
89    fn into_iter(self) -> Self::IntoIter {
90        self.iter_mut()
91    }
92}
93
94impl FromIterator<Value> for Array {
95    fn from_iter<I>(iter: I) -> Self
96    where
97        I: IntoIterator<Item = Value>,
98    {
99        Array {
100            inner: Vec::from_iter(iter),
101        }
102    }
103}