discord_cassandra_cpp/cassandra/
tuple.rs

1use crate::cassandra::collection::{List, Map, Set};
2use crate::cassandra::data_type::ConstDataType;
3use crate::cassandra::data_type::DataType;
4use crate::cassandra::error::*;
5use crate::cassandra::inet::Inet;
6use crate::cassandra::user_type::UserType;
7use crate::cassandra::util::{Protected, ProtectedInner};
8use crate::cassandra::uuid::Uuid;
9
10use crate::cassandra_sys::cass_false;
11use crate::cassandra_sys::cass_true;
12use crate::cassandra_sys::cass_tuple_data_type;
13use crate::cassandra_sys::cass_tuple_free;
14use crate::cassandra_sys::cass_tuple_new;
15use crate::cassandra_sys::cass_tuple_new_from_data_type;
16use crate::cassandra_sys::cass_tuple_set_bool;
17use crate::cassandra_sys::cass_tuple_set_bytes;
18use crate::cassandra_sys::cass_tuple_set_collection;
19use crate::cassandra_sys::cass_tuple_set_decimal;
20use crate::cassandra_sys::cass_tuple_set_double;
21use crate::cassandra_sys::cass_tuple_set_float;
22use crate::cassandra_sys::cass_tuple_set_inet;
23use crate::cassandra_sys::cass_tuple_set_int16;
24use crate::cassandra_sys::cass_tuple_set_int32;
25use crate::cassandra_sys::cass_tuple_set_int64;
26use crate::cassandra_sys::cass_tuple_set_int8;
27use crate::cassandra_sys::cass_tuple_set_null;
28use crate::cassandra_sys::cass_tuple_set_string_n;
29use crate::cassandra_sys::cass_tuple_set_tuple;
30use crate::cassandra_sys::cass_tuple_set_uint32;
31use crate::cassandra_sys::cass_tuple_set_user_type;
32use crate::cassandra_sys::cass_tuple_set_uuid;
33use crate::cassandra_sys::CassTuple as _Tuple;
34
35use std::net::IpAddr;
36use std::os::raw::c_char;
37
38/// A tuple of values.
39#[derive(Debug)]
40pub struct Tuple(*mut _Tuple);
41
42// The underlying C type has no thread-local state, but does not support access
43// from multiple threads: https://datastax.github.io/cpp-driver/topics/#thread-safety
44unsafe impl Send for Tuple {}
45
46impl ProtectedInner<*mut _Tuple> for Tuple {
47    fn inner(&self) -> *mut _Tuple {
48        self.0
49    }
50}
51
52impl Protected<*mut _Tuple> for Tuple {
53    fn build(inner: *mut _Tuple) -> Self {
54        if inner.is_null() {
55            panic!("Unexpected null pointer")
56        };
57        Tuple(inner)
58    }
59}
60
61impl Tuple {
62    /// Creates a new tuple.
63    pub fn new(item_count: usize) -> Self {
64        unsafe { Tuple(cass_tuple_new(item_count)) }
65    }
66
67    /// Creates a new tuple from an existing data type.
68    pub fn new_from_data_type(data_type: DataType) -> Tuple {
69        unsafe { Tuple(cass_tuple_new_from_data_type(data_type.inner())) }
70    }
71
72    /// Gets the data type of a tuple.
73    pub fn data_type(&mut self) -> ConstDataType {
74        unsafe { ConstDataType::build(cass_tuple_data_type(self.0)) }
75    }
76
77    /// Sets an null in a tuple at the specified index.
78    pub fn set_null(&mut self, index: usize) -> Result<&mut Self> {
79        unsafe { cass_tuple_set_null(self.0, index).to_result(self) }
80    }
81
82    /// Sets a "tinyint" in a tuple at the specified index.
83    pub fn set_int8(&mut self, index: usize, value: i8) -> Result<&mut Self> {
84        unsafe { cass_tuple_set_int8(self.0, index, value).to_result(self) }
85    }
86
87    /// Sets an "smallint" in a tuple at the specified index.
88    pub fn set_int16(&mut self, index: usize, value: i16) -> Result<&mut Self> {
89        unsafe { cass_tuple_set_int16(self.0, index, value).to_result(self) }
90    }
91
92    /// Sets an "int" in a tuple at the specified index.
93    pub fn set_int32(&mut self, index: usize, value: i32) -> Result<&mut Self> {
94        unsafe { cass_tuple_set_int32(self.0, index, value).to_result(self) }
95    }
96
97    /// Sets a "date" in a tuple at the specified index.
98    pub fn set_uint32(&mut self, index: usize, value: u32) -> Result<&mut Self> {
99        unsafe { cass_tuple_set_uint32(self.0, index, value).to_result(self) }
100    }
101
102    /// Sets a "bigint", "counter", "timestamp" or "time" in a tuple at the
103    /// specified index.
104    pub fn set_int64(&mut self, index: usize, value: i64) -> Result<&mut Self> {
105        unsafe { cass_tuple_set_int64(self.0, index, value).to_result(self) }
106    }
107
108    /// Sets a "float" in a tuple at the specified index.
109    pub fn set_float(&mut self, index: usize, value: f32) -> Result<&mut Self> {
110        unsafe { cass_tuple_set_float(self.0, index, value).to_result(self) }
111    }
112
113    /// Sets a "double" in a tuple at the specified index.
114    pub fn set_double(&mut self, index: usize, value: f64) -> Result<&mut Self> {
115        unsafe { cass_tuple_set_double(self.0, index, value).to_result(self) }
116    }
117
118    /// Sets a "boolean" in a tuple at the specified index.
119    pub fn set_bool(&mut self, index: usize, value: bool) -> Result<&mut Self> {
120        unsafe {
121            cass_tuple_set_bool(self.0, index, if value { cass_true } else { cass_false })
122                .to_result(self)
123        }
124    }
125
126    /// Sets an "ascii", "text" or "varchar" in a tuple at the specified index.
127    pub fn set_string<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
128    where
129        S: Into<String>,
130    {
131        unsafe {
132            let value_str = value.into();
133            let value_ptr = value_str.as_ptr() as *const c_char;
134            cass_tuple_set_string_n(self.0, index, value_ptr, value_str.len()).to_result(self)
135        }
136    }
137
138    /// Sets a "blob", "varint" or "custom" in a tuple at the specified index.
139    pub fn set_bytes(&mut self, index: usize, value: Vec<u8>) -> Result<&mut Self> {
140        unsafe { cass_tuple_set_bytes(self.0, index, value.as_ptr(), value.len()).to_result(self) }
141    }
142
143    /// Sets a "uuid" or "timeuuid" in a tuple at the specified index.
144    pub fn set_uuid<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
145    where
146        S: Into<Uuid>,
147    {
148        unsafe { cass_tuple_set_uuid(self.0, index, value.into().inner()).to_result(self) }
149    }
150
151    /// Sets an "inet" in a tuple at the specified index.
152    pub fn set_inet(&mut self, index: usize, value: Inet) -> Result<&mut Self> {
153        unsafe { cass_tuple_set_inet(self.0, index, value.inner()).to_result(self) }
154    }
155
156    /// Sets a list in a tuple at the specified index.
157    pub fn set_list<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
158    where
159        S: Into<List>,
160    {
161        unsafe { cass_tuple_set_collection(self.0, index, value.into().inner()).to_result(self) }
162    }
163
164    /// Sets a map in a tuple at the specified index.
165    pub fn set_map<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
166    where
167        S: Into<Map>,
168    {
169        unsafe { cass_tuple_set_collection(self.0, index, value.into().inner()).to_result(self) }
170    }
171
172    /// Sets a set" in a tuple at the specified index.
173    pub fn set_set<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
174    where
175        S: Into<Set>,
176    {
177        unsafe { cass_tuple_set_collection(self.0, index, value.into().inner()).to_result(self) }
178    }
179
180    /// Sets a "tuple" in a tuple at the specified index.
181    pub fn set_tuple(&mut self, index: usize, value: Tuple) -> Result<&mut Self> {
182        unsafe { cass_tuple_set_tuple(self.0, index, value.0).to_result(self) }
183    }
184
185    /// Sets a "udt" in a tuple at the specified index.
186    pub fn set_user_type(&mut self, index: usize, value: &UserType) -> Result<&mut Self> {
187        unsafe { cass_tuple_set_user_type(self.0, index, value.inner()).to_result(self) }
188    }
189}
190
191impl Drop for Tuple {
192    fn drop(&mut self) {
193        unsafe { cass_tuple_free(self.0) }
194    }
195}