wezterm_dynamic/
array.rs

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