over/
tup.rs

1//! `Tup` module.
2//! A tuple container which can hold elements of different types.
3
4use crate::parse::format::Format;
5use crate::types::Type;
6use crate::value::Value;
7use crate::{OverError, OverResult, INDENT_STEP};
8use std::fmt;
9use std::slice::Iter;
10use std::sync::Arc;
11
12#[derive(Clone, Debug)]
13struct TupInner {
14    vec: Vec<Value>,
15    inner_tvec: Vec<Type>,
16}
17
18/// `Tup` struct.
19#[derive(Clone, Debug)]
20pub struct Tup {
21    inner: Arc<TupInner>,
22}
23
24impl Tup {
25    /// Returns a new `Tup` from the given vector of `Value`s.
26    pub fn from_vec(values: Vec<Value>) -> Tup {
27        let tvec: Vec<Type> = values.iter().map(|val| val.get_type()).collect();
28
29        Tup {
30            inner: Arc::new(TupInner {
31                vec: values,
32                inner_tvec: tvec,
33            }),
34        }
35    }
36
37    /// Returns a reference to the inner vec of this `Tup`.
38    pub fn vec_ref(&self) -> &Vec<Value> {
39        &self.inner.vec
40    }
41
42    /// Iterates over each `Value` in `self`, applying `Fn` `f`.
43    pub fn with_each<F>(&self, mut f: F)
44    where
45        F: FnMut(&Value),
46    {
47        for value in &self.inner.vec {
48            f(value)
49        }
50    }
51
52    /// Gets the value at `index`.
53    /// Returns an error if `index` is out of bounds.
54    pub fn get(&self, index: usize) -> OverResult<Value> {
55        if index >= self.inner.vec.len() {
56            Err(OverError::TupOutOfBounds(index))
57        } else {
58            Ok(self.inner.vec[index].clone())
59        }
60    }
61
62    /// Returns the type vector of this `Tup`.
63    pub fn inner_type_vec(&self) -> Vec<Type> {
64        self.inner.inner_tvec.clone()
65    }
66
67    /// Returns the length of this `Tup`.
68    pub fn len(&self) -> usize {
69        self.inner.vec.len()
70    }
71
72    /// Returns whether this `Tup` is empty.
73    pub fn is_empty(&self) -> bool {
74        self.inner.vec.is_empty()
75    }
76
77    /// Returns whether `self` and `other` point to the same data.
78    pub fn ptr_eq(&self, other: &Self) -> bool {
79        Arc::ptr_eq(&self.inner, &other.inner)
80    }
81
82    /// Returns an iterator over the Tup.
83    pub fn iter(&self) -> Iter<Value> {
84        self.vec_ref().iter()
85    }
86}
87
88impl Default for Tup {
89    fn default() -> Self {
90        Self::from_vec(vec![])
91    }
92}
93
94impl fmt::Display for Tup {
95    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
96        write!(f, "{}", self.format(true, INDENT_STEP))
97    }
98}
99
100impl From<Vec<Value>> for Tup {
101    fn from(vec: Vec<Value>) -> Self {
102        Self::from_vec(vec)
103    }
104}
105
106impl PartialEq for Tup {
107    fn eq(&self, other: &Self) -> bool {
108        // Quickly return false if the types don't match.
109        if self.inner.inner_tvec != other.inner.inner_tvec {
110            return false;
111        }
112
113        self.inner.vec == other.inner.vec
114    }
115}