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