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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use std::{cell::RefCell, iter::FromIterator, ops::{DerefMut, Deref, Add}, rc::Rc};
use crate::{RantValue, RantTuple, RantTupleHandle};


/// Reference handle for a Rant list
#[derive(Debug, Clone, PartialEq)]
pub struct RantListHandle(Rc<RefCell<RantList>>);

impl RantListHandle {
  /// Makes a copy of the underlying list and returns a handle containing it.
  pub fn cloned(&self) -> Self {
    Self(Rc::new(RefCell::new((*self.0.borrow()).clone())))
  }
}

impl From<RantList> for RantListHandle {
  #[inline]
  fn from(list: RantList) -> Self {
    Self(Rc::new(RefCell::new(list)))
  }
}

impl Deref for RantListHandle {
  type Target = RefCell<RantList>;
  #[inline]
  fn deref(&self) -> &Self::Target {
    self.0.as_ref()
  }
}

/// Represents Rant's `list` type, which stores an ordered, mutable collection of values.
#[derive(Debug, Clone, PartialEq)]
pub struct RantList(Vec<RantValue>);

impl RantList {
  /// Creates an empty RantList.
  pub fn new() -> Self {
    Self(vec![])
  }

  /// Creates an empty RantList with the specified initial capacity.
  pub fn with_capacity(capacity: usize) -> Self {
    Self(Vec::with_capacity(capacity))
  }

  #[inline(always)]
  pub fn len(&self) -> usize {
    self.0.len()
  }

  #[inline(always)]
  pub fn is_empty(&self) -> bool {
    self.0.is_empty()
  }

  #[inline]
  pub fn into_handle(self) -> RantListHandle {
    RantListHandle::from(self)
  }

  #[inline]
  pub fn into_rant_tuple(self) -> RantTuple {
    RantTuple::from(self.0)
  }

  #[inline]
  pub fn to_rant_tuple(&self) -> RantTuple {
    RantTuple::from(self.0.clone())
  }
}

impl From<Vec<RantValue>> for RantList {
  fn from(list: Vec<RantValue>) -> Self {
    Self(list)
  }
}

impl Default for RantList {
  fn default() -> Self {
    Self::new()
  }
}

impl Deref for RantList {
  type Target = Vec<RantValue>;
  fn deref(&self) -> &Self::Target {
    &self.0
  }
}

impl DerefMut for RantList {
  fn deref_mut(&mut self) -> &mut Self::Target {
    &mut self.0
  }
}

impl FromIterator<RantValue> for RantList {
  fn from_iter<T: IntoIterator<Item = RantValue>>(iter: T) -> Self {
    let mut list = Self::new();
    for item in iter {
      list.push(item);
    }
    list
  }
}

impl IntoIterator for RantList {
  type Item = RantValue;
  type IntoIter = std::vec::IntoIter<Self::Item>;

  fn into_iter(self) -> Self::IntoIter {
    self.0.into_iter()
  }
}

impl Add for RantList {
  type Output = RantList;

  fn add(self, rhs: RantList) -> Self::Output {
    self.into_iter().chain(rhs.into_iter()).collect::<RantList>()
  }
}

impl Add<&RantList> for RantList {
  type Output = RantList;

  fn add(self, rhs: &RantList) -> Self::Output {
    self.into_iter().chain(rhs.iter().cloned()).collect::<RantList>()
  }  
}

impl Add<RantTuple> for RantList {
  type Output = RantList;

  fn add(self, rhs: RantTuple) -> Self::Output {
    self.into_iter().chain(rhs.into_iter()).collect::<RantList>()
  }
}

impl Add<&RantTuple> for RantList {
  type Output = RantList;

  fn add(self, rhs: &RantTuple) -> Self::Output {
    self.into_iter().chain(rhs.iter().cloned()).collect::<RantList>()
  }
}

impl Add<RantList> for &RantList {
  type Output = RantList;

  fn add(self, rhs: RantList) -> Self::Output {
    self.iter().cloned().chain(rhs.into_iter()).collect::<RantList>()
  }
}

impl Add<&RantList> for &RantList {
  type Output = RantList;

  fn add(self, rhs: &RantList) -> Self::Output {
    self.iter().cloned().chain(rhs.iter().cloned()).collect::<RantList>()
  }
}

impl Add<RantTuple> for &RantList {
  type Output = RantList;

  fn add(self, rhs: RantTuple) -> Self::Output {
    self.iter().cloned().chain(rhs.into_iter()).collect::<RantList>()
  }
}

impl Add<&RantTuple> for &RantList {
  type Output = RantList;

  fn add(self, rhs: &RantTuple) -> Self::Output {
    self.iter().cloned().chain(rhs.iter().cloned()).collect::<RantList>()
  }
}

impl Add for RantListHandle {
  type Output = RantListHandle;

  fn add(self, rhs: Self) -> Self::Output {
    (&*self.borrow() + &*rhs.borrow()).into_handle()
  }
}

impl Add<RantTupleHandle> for RantListHandle {
  type Output = RantListHandle;

  fn add(self, rhs: RantTupleHandle) -> Self::Output {
    (&*self.borrow() + &*rhs).into_handle()
  }
}