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;
19
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, and forbids only concurrent
43// mutation/free: https://datastax.github.io/cpp-driver/topics/#thread-safety
44unsafe impl Send for Tuple {}
45unsafe impl Sync for Tuple {}
46
47impl ProtectedInner<*mut _Tuple> for Tuple {
48    fn inner(&self) -> *mut _Tuple {
49        self.0
50    }
51}
52
53impl Protected<*mut _Tuple> for Tuple {
54    fn build(inner: *mut _Tuple) -> Self {
55        if inner.is_null() {
56            panic!("Unexpected null pointer")
57        };
58        Tuple(inner)
59    }
60}
61
62impl Tuple {
63    /// Creates a new tuple.
64    pub fn new(item_count: usize) -> Self {
65        unsafe { Tuple(cass_tuple_new(item_count)) }
66    }
67
68    /// Creates a new tuple from an existing data type.
69    pub fn new_from_data_type(data_type: DataType) -> Tuple {
70        unsafe { Tuple(cass_tuple_new_from_data_type(data_type.inner())) }
71    }
72
73    /// Gets the data type of a tuple.
74    pub fn data_type(&mut self) -> ConstDataType {
75        unsafe { ConstDataType::build(cass_tuple_data_type(self.0)) }
76    }
77
78    /// Sets an null in a tuple at the specified index.
79    pub fn set_null(&mut self, index: usize) -> Result<&mut Self> {
80        unsafe { cass_tuple_set_null(self.0, index).to_result(self) }
81    }
82
83    /// Sets a "tinyint" in a tuple at the specified index.
84    pub fn set_int8(&mut self, index: usize, value: i8) -> Result<&mut Self> {
85        unsafe { cass_tuple_set_int8(self.0, index, value).to_result(self) }
86    }
87
88    /// Sets an "smallint" in a tuple at the specified index.
89    pub fn set_int16(&mut self, index: usize, value: i16) -> Result<&mut Self> {
90        unsafe { cass_tuple_set_int16(self.0, index, value).to_result(self) }
91    }
92
93    /// Sets an "int" in a tuple at the specified index.
94    pub fn set_int32(&mut self, index: usize, value: i32) -> Result<&mut Self> {
95        unsafe { cass_tuple_set_int32(self.0, index, value).to_result(self) }
96    }
97
98    /// Sets a "date" in a tuple at the specified index.
99    pub fn set_uint32(&mut self, index: usize, value: u32) -> Result<&mut Self> {
100        unsafe { cass_tuple_set_uint32(self.0, index, value).to_result(self) }
101    }
102
103    /// Sets a "bigint", "counter", "timestamp" or "time" in a tuple at the
104    /// specified index.
105    pub fn set_int64(&mut self, index: usize, value: i64) -> Result<&mut Self> {
106        unsafe { cass_tuple_set_int64(self.0, index, value).to_result(self) }
107    }
108
109    /// Sets a "float" in a tuple at the specified index.
110    pub fn set_float(&mut self, index: usize, value: f32) -> Result<&mut Self> {
111        unsafe { cass_tuple_set_float(self.0, index, value).to_result(self) }
112    }
113
114    /// Sets a "double" in a tuple at the specified index.
115    pub fn set_double(&mut self, index: usize, value: f64) -> Result<&mut Self> {
116        unsafe { cass_tuple_set_double(self.0, index, value).to_result(self) }
117    }
118
119    /// Sets a "boolean" in a tuple at the specified index.
120    pub fn set_bool(&mut self, index: usize, value: bool) -> Result<&mut Self> {
121        unsafe {
122            cass_tuple_set_bool(self.0, index, if value { cass_true } else { cass_false })
123                .to_result(self)
124        }
125    }
126
127    /// Sets an "ascii", "text" or "varchar" in a tuple at the specified index.
128    pub fn set_string<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
129    where
130        S: Into<String>,
131    {
132        unsafe {
133            let value_str = value.into();
134            let value_ptr = value_str.as_ptr() as *const c_char;
135            cass_tuple_set_string_n(self.0, index, value_ptr, value_str.len()).to_result(self)
136        }
137    }
138
139    /// Sets a "blob", "varint" or "custom" in a tuple at the specified index.
140    pub fn set_bytes(&mut self, index: usize, value: Vec<u8>) -> Result<&mut Self> {
141        unsafe { cass_tuple_set_bytes(self.0, index, value.as_ptr(), value.len()).to_result(self) }
142    }
143
144    /// Sets a "uuid" or "timeuuid" in a tuple at the specified index.
145    pub fn set_uuid<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
146    where
147        S: Into<Uuid>,
148    {
149        unsafe { cass_tuple_set_uuid(self.0, index, value.into().inner()).to_result(self) }
150    }
151
152    /// Sets an "inet" in a tuple at the specified index.
153    pub fn set_inet(&mut self, index: usize, value: IpAddr) -> Result<&mut Self> {
154        let inet = Inet::from(&value);
155        unsafe { cass_tuple_set_inet(self.0, index, inet.inner()).to_result(self) }
156    }
157
158    /// Sets a list in a tuple at the specified index.
159    pub fn set_list<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
160    where
161        S: Into<List>,
162    {
163        unsafe { cass_tuple_set_collection(self.0, index, value.into().inner()).to_result(self) }
164    }
165
166    /// Sets a map in a tuple at the specified index.
167    pub fn set_map<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
168    where
169        S: Into<Map>,
170    {
171        unsafe { cass_tuple_set_collection(self.0, index, value.into().inner()).to_result(self) }
172    }
173
174    /// Sets a set" in a tuple at the specified index.
175    pub fn set_set<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
176    where
177        S: Into<Set>,
178    {
179        unsafe { cass_tuple_set_collection(self.0, index, value.into().inner()).to_result(self) }
180    }
181
182    /// Sets a "tuple" in a tuple at the specified index.
183    pub fn set_tuple(&mut self, index: usize, value: Tuple) -> Result<&mut Self> {
184        unsafe { cass_tuple_set_tuple(self.0, index, value.0).to_result(self) }
185    }
186
187    /// Sets a "udt" in a tuple at the specified index.
188    pub fn set_user_type(&mut self, index: usize, value: &UserType) -> Result<&mut Self> {
189        unsafe { cass_tuple_set_user_type(self.0, index, value.inner()).to_result(self) }
190    }
191}
192
193impl Drop for Tuple {
194    fn drop(&mut self) {
195        unsafe { cass_tuple_free(self.0) }
196    }
197}