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
use std::fmt;

/// An array of values.
#[derive(Debug, Default)]
pub struct Array<T: fmt::Display> {
    inner: Vec<T>,
}

impl<T: fmt::Display> From<Vec<T>> for Array<T> {
    fn from(inner: Vec<T>) -> Self {
        Self { inner }
    }
}

impl<T: fmt::Display> Array<T> {
    /// Returns `true` if the array contains no elements.
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    /// Returns the length of the array.
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Add a new value to the end of the array.
    pub fn push(&mut self, val: impl Into<T>) {
        self.inner.push(val.into());
    }

    /// Create a new array.
    pub const fn new() -> Self {
        Self { inner: Vec::new() }
    }
}

impl<T: fmt::Display> fmt::Display for Array<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("[")?;

        for (i, val) in self.inner.iter().enumerate() {
            val.fmt(f)?;

            if i < self.inner.len() - 1 {
                f.write_str(", ")?;
            }
        }

        f.write_str("]")?;

        Ok(())
    }
}