discord_cassandra_cpp/cassandra/
collection.rs1use crate::cassandra::data_type::ConstDataType;
2use crate::cassandra::data_type::DataType;
3use crate::cassandra::error::*;
4use crate::cassandra::inet::Inet;
5use crate::cassandra::tuple::Tuple;
6use crate::cassandra::user_type::UserType;
7use crate::cassandra::util::{Protected, ProtectedInner};
8use crate::cassandra::uuid::Uuid;
9
10use crate::cassandra_sys::cass_collection_append_bool;
11use crate::cassandra_sys::cass_collection_append_bytes;
12use crate::cassandra_sys::cass_collection_append_collection;
13use crate::cassandra_sys::cass_collection_append_decimal;
14use crate::cassandra_sys::cass_collection_append_double;
15use crate::cassandra_sys::cass_collection_append_float;
16use crate::cassandra_sys::cass_collection_append_inet;
17use crate::cassandra_sys::cass_collection_append_int16;
18use crate::cassandra_sys::cass_collection_append_int32;
19use crate::cassandra_sys::cass_collection_append_int64;
20use crate::cassandra_sys::cass_collection_append_int8;
21use crate::cassandra_sys::cass_collection_append_string_n;
22use crate::cassandra_sys::cass_collection_append_tuple;
23use crate::cassandra_sys::cass_collection_append_uint32;
24use crate::cassandra_sys::cass_collection_append_user_type;
25use crate::cassandra_sys::cass_collection_append_uuid;
26use crate::cassandra_sys::cass_collection_data_type;
27use crate::cassandra_sys::cass_collection_free;
28use crate::cassandra_sys::cass_collection_new;
29use crate::cassandra_sys::cass_collection_new_from_data_type;
30use crate::cassandra_sys::cass_false;
31use crate::cassandra_sys::cass_true;
32use crate::cassandra_sys::CassCollection as _CassCollection;
33use crate::cassandra_sys::CASS_COLLECTION_TYPE_LIST;
34use crate::cassandra_sys::CASS_COLLECTION_TYPE_MAP;
35use crate::cassandra_sys::CASS_COLLECTION_TYPE_SET;
36
37use std::os::raw::c_char;
38
39pub trait CassCollection {
49 type Value;
51
52 fn new(item_count: usize) -> Self;
54
55 fn new_from_data_type(value: DataType, item_count: usize) -> Self;
57
58 fn data_type(&self) -> ConstDataType;
60
61 fn append_int8(&mut self, value: i8) -> Result<&mut Self>;
63
64 fn append_int16(&mut self, value: i16) -> Result<&mut Self>;
66
67 fn append_int32(&mut self, value: i32) -> Result<&mut Self>;
69
70 fn append_uint32(&mut self, value: u32) -> Result<&mut Self>;
72
73 fn append_int64(&mut self, value: i64) -> Result<&mut Self>;
76
77 fn append_float(&mut self, value: f32) -> Result<&mut Self>;
79
80 fn append_double(&mut self, value: f64) -> Result<&mut Self>;
82
83 fn append_bool(&mut self, value: bool) -> Result<&mut Self>;
85
86 fn append_string(&mut self, value: &str) -> Result<&mut Self>;
88
89 fn append_bytes(&mut self, value: Vec<u8>) -> Result<&mut Self>;
91
92 fn append_uuid(&mut self, value: Uuid) -> Result<&mut Self>;
94
95 fn append_inet(&mut self, value: Inet) -> Result<&mut Self>;
97
98 fn append_list(&mut self, value: List) -> Result<&mut Self>;
100
101 fn append_set(&mut self, value: Set) -> Result<&mut Self>;
103
104 fn append_map(&mut self, value: Map) -> Result<&mut Self>;
106
107 fn append_tuple(&mut self, value: Tuple) -> Result<&mut Self>;
109
110 fn append_user_type(&mut self, value: &UserType) -> Result<&mut Self>;
112}
113
114#[derive(Debug)]
116pub struct List(*mut _CassCollection);
117
118unsafe impl Send for List {}
121
122impl ProtectedInner<*mut _CassCollection> for List {
123 fn inner(&self) -> *mut _CassCollection {
124 self.0
125 }
126}
127
128impl Protected<*mut _CassCollection> for List {
129 fn build(inner: *mut _CassCollection) -> Self {
130 if inner.is_null() {
131 panic!("Unexpected null pointer")
132 };
133 List(inner)
134 }
135}
136
137impl ProtectedInner<*mut _CassCollection> for Map {
138 fn inner(&self) -> *mut _CassCollection {
139 self.0
140 }
141}
142
143impl Protected<*mut _CassCollection> for Map {
144 fn build(inner: *mut _CassCollection) -> Self {
145 if inner.is_null() {
146 panic!("Unexpected null pointer")
147 };
148 Map(inner)
149 }
150}
151
152impl ProtectedInner<*mut _CassCollection> for Set {
153 fn inner(&self) -> *mut _CassCollection {
154 self.0
155 }
156}
157
158impl Protected<*mut _CassCollection> for Set {
159 fn build(inner: *mut _CassCollection) -> Self {
160 if inner.is_null() {
161 panic!("Unexpected null pointer")
162 };
163 Set(inner)
164 }
165}
166
167impl Drop for List {
168 fn drop(&mut self) {
169 unsafe { cass_collection_free(self.0) }
170 }
171}
172
173impl CassCollection for List {
174 type Value = _CassCollection;
175
176 fn new(item_count: usize) -> Self {
178 unsafe { List::build(cass_collection_new(CASS_COLLECTION_TYPE_LIST, item_count)) }
179 }
180
181 fn new_from_data_type(value: DataType, item_count: usize) -> Self {
182 unsafe {
183 List(cass_collection_new_from_data_type(
184 value.inner(),
185 item_count,
186 ))
187 }
188 }
189
190 fn data_type(&self) -> ConstDataType {
192 unsafe { ConstDataType::build(cass_collection_data_type(self.inner())) }
193 }
194
195 fn append_int8(&mut self, value: i8) -> Result<&mut Self> {
197 unsafe { cass_collection_append_int8(self.inner(), value).to_result(self) }
198 }
199
200 fn append_int16(&mut self, value: i16) -> Result<&mut Self> {
202 unsafe { cass_collection_append_int16(self.inner(), value).to_result(self) }
203 }
204
205 fn append_int32(&mut self, value: i32) -> Result<&mut Self> {
207 unsafe { cass_collection_append_int32(self.inner(), value).to_result(self) }
208 }
209
210 fn append_uint32(&mut self, value: u32) -> Result<&mut Self> {
212 unsafe { cass_collection_append_uint32(self.inner(), value).to_result(self) }
213 }
214
215 fn append_int64(&mut self, value: i64) -> Result<&mut Self> {
218 unsafe { cass_collection_append_int64(self.inner(), value).to_result(self) }
219 }
220
221 fn append_float(&mut self, value: f32) -> Result<&mut Self> {
223 unsafe { cass_collection_append_float(self.inner(), value).to_result(self) }
224 }
225
226 fn append_double(&mut self, value: f64) -> Result<&mut Self> {
228 unsafe { cass_collection_append_double(self.inner(), value).to_result(self) }
229 }
230
231 fn append_bool(&mut self, value: bool) -> Result<&mut Self> {
233 unsafe {
234 cass_collection_append_bool(self.inner(), if value { cass_true } else { cass_false })
235 .to_result(self)
236 }
237 }
238
239 fn append_string(&mut self, value: &str) -> Result<&mut Self> {
241 unsafe {
242 let value_ptr = value.as_ptr() as *const c_char;
243 let result = cass_collection_append_string_n(self.inner(), value_ptr, value.len());
244 result.to_result(self)
245 }
246 }
247
248 fn append_bytes(&mut self, value: Vec<u8>) -> Result<&mut Self> {
250 unsafe {
251 let bytes = cass_collection_append_bytes(self.inner(), value[..].as_ptr(), value.len());
252 bytes.to_result(self)
253 }
254 }
255
256 fn append_uuid(&mut self, value: Uuid) -> Result<&mut Self> {
258 unsafe { cass_collection_append_uuid(self.inner(), value.inner()).to_result(self) }
259 }
260
261 fn append_inet(&mut self, value: Inet) -> Result<&mut Self> {
263 unsafe { cass_collection_append_inet(self.inner(), value.inner()).to_result(self) }
264 }
265
266 fn append_list(&mut self, value: List) -> Result<&mut Self> {
268 unsafe { cass_collection_append_collection(self.inner(), value.0).to_result(self) }
269 }
270
271 fn append_set(&mut self, value: Set) -> Result<&mut Self> {
273 unsafe { cass_collection_append_collection(self.inner(), value.0).to_result(self) }
274 }
275
276 fn append_map(&mut self, value: Map) -> Result<&mut Self> {
278 unsafe { cass_collection_append_collection(self.inner(), value.0).to_result(self) }
279 }
280
281 fn append_tuple(&mut self, value: Tuple) -> Result<&mut Self> {
283 unsafe { cass_collection_append_tuple(self.inner(), value.inner()).to_result(self) }
284 }
285
286 fn append_user_type(&mut self, value: &UserType) -> Result<&mut Self> {
288 unsafe { cass_collection_append_user_type(self.inner(), value.inner()).to_result(self) }
289 }
290}
291
292#[derive(Debug)]
294pub struct Set(*mut _CassCollection);
295
296unsafe impl Send for Set {}
299
300impl Drop for Set {
301 fn drop(&mut self) {
302 unsafe { cass_collection_free(self.inner()) }
303 }
304}
305
306impl CassCollection for Set {
311 type Value = _CassCollection;
312
313 fn new(item_count: usize) -> Self {
315 unsafe { Set(cass_collection_new(CASS_COLLECTION_TYPE_SET, item_count)) }
316 }
317
318 fn new_from_data_type(value: DataType, item_count: usize) -> Self {
319 unsafe {
320 Set(cass_collection_new_from_data_type(
321 value.inner(),
322 item_count,
323 ))
324 }
325 }
326 fn data_type(&self) -> ConstDataType {
328 unsafe { ConstDataType::build(cass_collection_data_type(self.inner())) }
329 }
330
331 fn append_int8(&mut self, value: i8) -> Result<&mut Self> {
333 unsafe { cass_collection_append_int8(self.inner(), value).to_result(self) }
334 }
335
336 fn append_int16(&mut self, value: i16) -> Result<&mut Self> {
338 unsafe { cass_collection_append_int16(self.inner(), value).to_result(self) }
339 }
340
341 fn append_int32(&mut self, value: i32) -> Result<&mut Self> {
343 unsafe { cass_collection_append_int32(self.inner(), value).to_result(self) }
344 }
345
346 fn append_uint32(&mut self, value: u32) -> Result<&mut Self> {
348 unsafe { cass_collection_append_uint32(self.inner(), value).to_result(self) }
349 }
350
351 fn append_int64(&mut self, value: i64) -> Result<&mut Self> {
354 unsafe { cass_collection_append_int64(self.inner(), value).to_result(self) }
355 }
356
357 fn append_float(&mut self, value: f32) -> Result<&mut Self> {
359 unsafe { cass_collection_append_float(self.inner(), value).to_result(self) }
360 }
361
362 fn append_double(&mut self, value: f64) -> Result<&mut Self> {
364 unsafe { cass_collection_append_double(self.inner(), value).to_result(self) }
365 }
366
367 fn append_bool(&mut self, value: bool) -> Result<&mut Self> {
369 unsafe {
370 cass_collection_append_bool(self.inner(), if value { cass_true } else { cass_false })
371 .to_result(self)
372 }
373 }
374
375 fn append_string(&mut self, value: &str) -> Result<&mut Self> {
377 unsafe {
378 let value_ptr = value.as_ptr() as *const c_char;
379 let result = cass_collection_append_string_n(self.inner(), value_ptr, value.len());
380 result.to_result(self)
381 }
382 }
383
384 fn append_bytes(&mut self, value: Vec<u8>) -> Result<&mut Self> {
386 unsafe {
387 let bytes = cass_collection_append_bytes(self.inner(), value[..].as_ptr(), value.len());
388 bytes.to_result(self)
389 }
390 }
391
392 fn append_uuid(&mut self, value: Uuid) -> Result<&mut Self> {
394 unsafe { cass_collection_append_uuid(self.inner(), value.inner()).to_result(self) }
395 }
396
397 fn append_inet(&mut self, value: Inet) -> Result<&mut Self> {
399 unsafe { cass_collection_append_inet(self.inner(), value.inner()).to_result(self) }
400 }
401
402 fn append_list(&mut self, value: List) -> Result<&mut Self> {
404 unsafe { cass_collection_append_collection(self.inner(), value.0).to_result(self) }
405 }
406
407 fn append_set(&mut self, value: Set) -> Result<&mut Self> {
409 unsafe { cass_collection_append_collection(self.inner(), value.0).to_result(self) }
410 }
411
412 fn append_map(&mut self, value: Map) -> Result<&mut Self> {
414 unsafe { cass_collection_append_collection(self.inner(), value.0).to_result(self) }
415 }
416
417 fn append_tuple(&mut self, value: Tuple) -> Result<&mut Self> {
419 unsafe { cass_collection_append_tuple(self.inner(), value.inner()).to_result(self) }
420 }
421
422 fn append_user_type(&mut self, value: &UserType) -> Result<&mut Self> {
424 unsafe { cass_collection_append_user_type(self.inner(), value.inner()).to_result(self) }
425 }
426}
427
428#[derive(Debug)]
430pub struct Map(*mut _CassCollection);
431
432unsafe impl Send for Map {}
435
436impl Drop for Map {
437 fn drop(&mut self) {
438 unsafe { cass_collection_free(self.0) }
439 }
440}
441
442impl CassCollection for Map {
443 type Value = _CassCollection;
444 fn new(item_count: usize) -> Self {
446 unsafe { Map(cass_collection_new(CASS_COLLECTION_TYPE_MAP, item_count)) }
447 }
448
449 fn new_from_data_type(value: DataType, item_count: usize) -> Self {
450 unsafe {
451 Map(cass_collection_new_from_data_type(
452 value.inner(),
453 item_count,
454 ))
455 }
456 }
457
458 fn data_type(&self) -> ConstDataType {
460 unsafe { ConstDataType::build(cass_collection_data_type(self.inner())) }
461 }
462
463 fn append_int8(&mut self, value: i8) -> Result<&mut Self> {
465 unsafe { cass_collection_append_int8(self.inner(), value).to_result(self) }
466 }
467
468 fn append_int16(&mut self, value: i16) -> Result<&mut Self> {
470 unsafe { cass_collection_append_int16(self.inner(), value).to_result(self) }
471 }
472
473 fn append_int32(&mut self, value: i32) -> Result<&mut Self> {
475 unsafe { cass_collection_append_int32(self.inner(), value).to_result(self) }
476 }
477
478 fn append_uint32(&mut self, value: u32) -> Result<&mut Self> {
480 unsafe { cass_collection_append_uint32(self.inner(), value).to_result(self) }
481 }
482
483 fn append_int64(&mut self, value: i64) -> Result<&mut Self> {
486 unsafe { cass_collection_append_int64(self.inner(), value).to_result(self) }
487 }
488
489 fn append_float(&mut self, value: f32) -> Result<&mut Self> {
491 unsafe { cass_collection_append_float(self.inner(), value).to_result(self) }
492 }
493
494 fn append_double(&mut self, value: f64) -> Result<&mut Self> {
496 unsafe { cass_collection_append_double(self.inner(), value).to_result(self) }
497 }
498
499 fn append_bool(&mut self, value: bool) -> Result<&mut Self> {
501 unsafe {
502 cass_collection_append_bool(self.inner(), if value { cass_true } else { cass_false })
503 .to_result(self)
504 }
505 }
506
507 fn append_string(&mut self, value: &str) -> Result<&mut Self> {
509 unsafe {
510 let value_ptr = value.as_ptr() as *const c_char;
511 let result = cass_collection_append_string_n(self.inner(), value_ptr, value.len());
512 result.to_result(self)
513 }
514 }
515
516 fn append_bytes(&mut self, value: Vec<u8>) -> Result<&mut Self> {
518 unsafe {
519 let bytes = cass_collection_append_bytes(self.inner(), value[..].as_ptr(), value.len());
520 bytes.to_result(self)
521 }
522 }
523
524 fn append_uuid(&mut self, value: Uuid) -> Result<&mut Self> {
526 unsafe { cass_collection_append_uuid(self.inner(), value.inner()).to_result(self) }
527 }
528
529 fn append_inet(&mut self, value: Inet) -> Result<&mut Self> {
531 unsafe { cass_collection_append_inet(self.inner(), value.inner()).to_result(self) }
532 }
533
534 fn append_list(&mut self, value: List) -> Result<&mut Self> {
536 unsafe { cass_collection_append_collection(self.inner(), value.0).to_result(self) }
537 }
538
539 fn append_set(&mut self, value: Set) -> Result<&mut Self> {
541 unsafe { cass_collection_append_collection(self.inner(), value.0).to_result(self) }
542 }
543
544 fn append_map(&mut self, value: Map) -> Result<&mut Self> {
546 unsafe { cass_collection_append_collection(self.inner(), value.0).to_result(self) }
547 }
548
549 fn append_tuple(&mut self, value: Tuple) -> Result<&mut Self> {
551 unsafe { cass_collection_append_tuple(self.inner(), value.inner()).to_result(self) }
552 }
553
554 fn append_user_type(&mut self, value: &UserType) -> Result<&mut Self> {
556 unsafe { cass_collection_append_user_type(self.inner(), value.inner()).to_result(self) }
557 }
558}