solomon_gremlin/structure/
list.rs

1use crate::GValue;
2
3#[derive(Debug, PartialEq, Clone)]
4pub struct List(Vec<GValue>);
5
6impl Default for List {
7	fn default() -> Self {
8		Self(Default::default())
9	}
10}
11
12impl List {
13	pub fn new(elements: Vec<GValue>) -> Self {
14		List(elements)
15	}
16
17	pub(crate) fn take(self) -> Vec<GValue> {
18		self.0
19	}
20
21	pub fn iter(&self) -> impl Iterator<Item = &GValue> {
22		self.0.iter()
23	}
24
25	pub fn len(&self) -> usize {
26		self.0.len()
27	}
28
29	pub fn is_empty(&self) -> bool {
30		self.0.is_empty()
31	}
32
33	pub fn push(&mut self, value: GValue) {
34		self.0.push(value)
35	}
36
37	pub fn append(&mut self, other: &mut Vec<GValue>) {
38		self.0.append(other)
39	}
40
41	pub fn core(self) -> Vec<GValue> {
42		self.0
43	}
44
45	pub fn last(self) -> Option<GValue> {
46		self.0.last().cloned()
47	}
48
49	pub fn last_mut(&mut self) -> Option<&mut GValue> {
50		self.0.last_mut()
51	}
52}
53
54impl std::iter::IntoIterator for List {
55	type Item = GValue;
56	type IntoIter = std::vec::IntoIter<Self::Item>;
57	fn into_iter(self) -> Self::IntoIter {
58		self.0.into_iter()
59	}
60}
61
62impl Into<List> for Vec<GValue> {
63	fn into(self) -> List {
64		List(self)
65	}
66}
67
68impl From<List> for Vec<GValue> {
69	fn from(list: List) -> Self {
70		list.take()
71	}
72}
73
74impl std::ops::Index<usize> for List {
75	type Output = GValue;
76
77	fn index(&self, key: usize) -> &GValue {
78		self.0.get(key).expect("no entry found for key")
79	}
80}