reifydb_type/value/container/
any.rs1use std::{
5 fmt::{self, Debug},
6 ops::Deref,
7};
8
9use serde::{Deserialize, Deserializer, Serialize, Serializer};
10
11use crate::{
12 storage::{Cow, DataBitVec, DataVec, Storage},
13 util::cowvec::CowVec,
14 value::Value,
15};
16
17pub struct AnyContainer<S: Storage = Cow> {
18 data: S::Vec<Box<Value>>,
19}
20
21impl<S: Storage> Clone for AnyContainer<S> {
22 fn clone(&self) -> Self {
23 Self {
24 data: self.data.clone(),
25 }
26 }
27}
28
29impl<S: Storage> Debug for AnyContainer<S>
30where
31 S::Vec<Box<Value>>: Debug,
32{
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 f.debug_struct("AnyContainer").field("data", &self.data).finish()
35 }
36}
37
38impl<S: Storage> PartialEq for AnyContainer<S>
39where
40 S::Vec<Box<Value>>: PartialEq,
41{
42 fn eq(&self, other: &Self) -> bool {
43 self.data == other.data
44 }
45}
46
47impl Serialize for AnyContainer<Cow> {
48 fn serialize<Ser: Serializer>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error> {
49 #[derive(Serialize)]
50 struct Helper<'a> {
51 data: &'a CowVec<Box<Value>>,
52 }
53 Helper {
54 data: &self.data,
55 }
56 .serialize(serializer)
57 }
58}
59
60impl<'de> Deserialize<'de> for AnyContainer<Cow> {
61 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
62 #[derive(Deserialize)]
63 struct Helper {
64 data: CowVec<Box<Value>>,
65 }
66 let h = Helper::deserialize(deserializer)?;
67 Ok(AnyContainer {
68 data: h.data,
69 })
70 }
71}
72
73impl<S: Storage> Deref for AnyContainer<S> {
74 type Target = [Box<Value>];
75
76 fn deref(&self) -> &Self::Target {
77 self.data.as_slice()
78 }
79}
80
81impl AnyContainer<Cow> {
82 pub fn new(data: Vec<Box<Value>>) -> Self {
83 Self {
84 data: CowVec::new(data),
85 }
86 }
87
88 pub fn with_capacity(capacity: usize) -> Self {
89 Self {
90 data: CowVec::with_capacity(capacity),
91 }
92 }
93
94 pub fn from_vec(data: Vec<Box<Value>>) -> Self {
95 Self {
96 data: CowVec::new(data),
97 }
98 }
99}
100
101impl<S: Storage> AnyContainer<S> {
102 pub fn from_parts(data: S::Vec<Box<Value>>) -> Self {
103 Self {
104 data,
105 }
106 }
107
108 pub fn len(&self) -> usize {
109 DataVec::len(&self.data)
110 }
111
112 pub fn capacity(&self) -> usize {
113 DataVec::capacity(&self.data)
114 }
115
116 pub fn is_empty(&self) -> bool {
117 DataVec::is_empty(&self.data)
118 }
119
120 pub fn clear(&mut self) {
121 DataVec::clear(&mut self.data);
122 }
123
124 pub fn push(&mut self, value: Box<Value>) {
125 DataVec::push(&mut self.data, value);
126 }
127
128 pub fn push_default(&mut self) {
129 DataVec::push(&mut self.data, Box::new(Value::none()));
130 }
131
132 pub fn get(&self, index: usize) -> Option<&Box<Value>> {
133 if index < self.len() {
134 DataVec::get(&self.data, index)
135 } else {
136 None
137 }
138 }
139
140 pub fn is_defined(&self, idx: usize) -> bool {
141 idx < self.len()
142 }
143
144 pub fn is_fully_defined(&self) -> bool {
145 true
146 }
147
148 pub fn data(&self) -> &S::Vec<Box<Value>> {
149 &self.data
150 }
151
152 pub fn data_mut(&mut self) -> &mut S::Vec<Box<Value>> {
153 &mut self.data
154 }
155
156 pub fn as_string(&self, index: usize) -> String {
157 if index < self.len() {
158 format!("{}", self.data[index])
159 } else {
160 "none".to_string()
161 }
162 }
163
164 pub fn get_value(&self, index: usize) -> Value {
165 if index < self.len() {
166 Value::Any(self.data[index].clone())
167 } else {
168 Value::none()
169 }
170 }
171
172 pub fn none_count(&self) -> usize {
173 0
174 }
175
176 pub fn take(&self, num: usize) -> Self {
177 Self {
178 data: DataVec::take(&self.data, num),
179 }
180 }
181
182 pub fn filter(&mut self, mask: &S::BitVec) {
183 let mut new_data = DataVec::spawn(&self.data, DataBitVec::count_ones(mask));
184
185 for (i, keep) in DataBitVec::iter(mask).enumerate() {
186 if keep && i < self.len() {
187 DataVec::push(&mut new_data, self.data[i].clone());
188 }
189 }
190
191 self.data = new_data;
192 }
193
194 pub fn reorder(&mut self, indices: &[usize]) {
195 let mut new_data = DataVec::spawn(&self.data, indices.len());
196
197 for &idx in indices {
198 if idx < self.len() {
199 DataVec::push(&mut new_data, self.data[idx].clone());
200 } else {
201 DataVec::push(&mut new_data, Box::new(Value::none()));
202 }
203 }
204
205 self.data = new_data;
206 }
207
208 pub fn extend(&mut self, other: &Self) -> crate::Result<()> {
209 DataVec::extend_iter(&mut self.data, other.data.iter().cloned());
210 Ok(())
211 }
212}