cdrs/types/
tuple.rs

1use std::net::IpAddr;
2use time::Timespec;
3use uuid::Uuid;
4
5use crate::error::{column_is_empty_err, Error, Result};
6use crate::frame::frame_result::{CTuple, ColType, ColTypeOption, ColTypeOptionValue};
7use crate::types::blob::Blob;
8use crate::types::data_serialization_types::*;
9use crate::types::decimal::Decimal;
10use crate::types::list::List;
11use crate::types::map::Map;
12use crate::types::udt::UDT;
13use crate::types::{ByIndex, CBytes, IntoRustByIndex};
14
15use std::hash::{Hash, Hasher};
16
17#[derive(Debug)]
18pub struct Tuple {
19    data: Vec<(ColTypeOption, CBytes)>,
20}
21
22impl PartialEq for Tuple {
23    fn eq(&self, other: &Tuple) -> bool {
24        if self.data.len() != other.data.len() {
25            return false;
26        }
27        for (s, o) in self.data.iter().zip(other.data.iter()) {
28            if s.1 != o.1 {
29                return false;
30            }
31        }
32        true
33    }
34}
35
36impl Eq for Tuple {}
37
38impl Hash for Tuple {
39    fn hash<H: Hasher>(&self, state: &mut H) {
40        for data in &self.data {
41            data.1.hash(state);
42        }
43    }
44}
45
46impl Tuple {
47    pub fn new<'a>(data: Vec<CBytes>, metadata: &'a CTuple) -> Tuple {
48        let meta_iter = metadata.types.iter();
49
50        let acc = Vec::with_capacity(metadata.types.len());
51        let d = meta_iter.zip(data.iter()).fold(acc, |mut a, v| {
52            let (val_type, val_b) = v;
53            a.push((val_type.clone(), val_b.clone()));
54            a
55        });
56
57        Tuple { data: d }
58    }
59}
60
61impl ByIndex for Tuple {}
62
63into_rust_by_index!(Tuple, Blob);
64into_rust_by_index!(Tuple, String);
65into_rust_by_index!(Tuple, bool);
66into_rust_by_index!(Tuple, i64);
67into_rust_by_index!(Tuple, i32);
68into_rust_by_index!(Tuple, i16);
69into_rust_by_index!(Tuple, i8);
70into_rust_by_index!(Tuple, f64);
71into_rust_by_index!(Tuple, f32);
72into_rust_by_index!(Tuple, IpAddr);
73into_rust_by_index!(Tuple, Uuid);
74into_rust_by_index!(Tuple, List);
75into_rust_by_index!(Tuple, Map);
76into_rust_by_index!(Tuple, UDT);
77into_rust_by_index!(Tuple, Tuple);
78into_rust_by_index!(Tuple, Timespec);
79into_rust_by_index!(Tuple, Decimal);