cassandra_cpp/cassandra/
custom_payload.rs

1use crate::cassandra::error::*;
2use crate::cassandra::util::{Protected, ProtectedInner};
3
4use crate::cassandra_sys::cass_custom_payload_free;
5use crate::cassandra_sys::cass_custom_payload_new;
6
7use crate::cassandra_sys::cass_custom_payload_set_n;
8use crate::cassandra_sys::CassCustomPayload as _CassCustomPayload;
9use std::collections::HashMap;
10use std::os::raw::c_char;
11
12pub type CustomPayloadResponse = HashMap<String, Vec<u8>>;
13
14/// Custom payloads not fully supported yet
15#[derive(Debug)]
16pub struct CustomPayload(*mut _CassCustomPayload);
17
18// The underlying C type has no thread-local state, and forbids only concurrent
19// mutation/free: https://datastax.github.io/cpp-driver/topics/#thread-safety
20unsafe impl Send for CustomPayload {}
21unsafe impl Sync for CustomPayload {}
22
23impl ProtectedInner<*mut _CassCustomPayload> for CustomPayload {
24    #[inline(always)]
25    fn inner(&self) -> *mut _CassCustomPayload {
26        self.0
27    }
28}
29
30impl Protected<*mut _CassCustomPayload> for CustomPayload {
31    fn build(inner: *mut _CassCustomPayload) -> Self {
32        if inner.is_null() {
33            panic!("Unexpected null pointer")
34        };
35        CustomPayload(inner)
36    }
37}
38
39impl Default for CustomPayload {
40    /// creates a new custom payload
41    fn default() -> Self {
42        unsafe { CustomPayload(cass_custom_payload_new()) }
43    }
44}
45
46impl CustomPayload {
47    /// Sets an item to the custom payload.
48    pub fn set(&self, name: String, value: &[u8]) -> Result<()> {
49        unsafe {
50            let name_ptr = name.as_ptr() as *const c_char;
51            cass_custom_payload_set_n(self.0, name_ptr, name.len(), value.as_ptr(), value.len());
52            Ok(())
53        }
54    }
55}
56
57impl Drop for CustomPayload {
58    fn drop(&mut self) {
59        unsafe { cass_custom_payload_free(self.0) }
60    }
61}