cassandra_cpp/cassandra/policy/
retry.rs

1use crate::cassandra::util::{Protected, ProtectedInner};
2use crate::cassandra_sys::cass_retry_policy_default_new;
3use crate::cassandra_sys::cass_retry_policy_downgrading_consistency_new;
4use crate::cassandra_sys::cass_retry_policy_fallthrough_new;
5use crate::cassandra_sys::cass_retry_policy_free;
6use crate::cassandra_sys::cass_retry_policy_logging_new;
7use crate::cassandra_sys::CassRetryPolicy as _RetryPolicy;
8
9/// The selected retry policy
10#[derive(Debug)]
11pub struct RetryPolicy(*mut _RetryPolicy);
12
13// The underlying C type has no thread-local state, and forbids only concurrent
14// mutation/free: https://datastax.github.io/cpp-driver/topics/#thread-safety
15unsafe impl Send for RetryPolicy {}
16unsafe impl Sync for RetryPolicy {}
17
18impl ProtectedInner<*mut _RetryPolicy> for RetryPolicy {
19    fn inner(&self) -> *mut _RetryPolicy {
20        self.0
21    }
22}
23
24impl Protected<*mut _RetryPolicy> for RetryPolicy {
25    fn build(inner: *mut _RetryPolicy) -> Self {
26        if inner.is_null() {
27            panic!("Unexpected null pointer")
28        };
29        RetryPolicy(inner)
30    }
31}
32
33impl RetryPolicy {
34    /// The default retry policy
35    pub fn default_new() -> Self {
36        unsafe { RetryPolicy::build(cass_retry_policy_default_new()) }
37    }
38
39    /// An auto-CL-downgrading consistency level
40    pub fn downgrading_consistency_new() -> Self {
41        unsafe { RetryPolicy(cass_retry_policy_downgrading_consistency_new()) }
42    }
43
44    /// a fallthrough retry policy
45    pub fn fallthrough_new() -> Self {
46        unsafe { RetryPolicy(cass_retry_policy_fallthrough_new()) }
47    }
48
49    /// The a logging retry policy
50    pub fn logging_new(child_retry_policy: RetryPolicy) -> Self {
51        // TODO: can return NULL
52        unsafe { RetryPolicy::build(cass_retry_policy_logging_new(child_retry_policy.0)) }
53    }
54}
55
56impl Drop for RetryPolicy {
57    fn drop(&mut self) {
58        unsafe {
59            cass_retry_policy_free(self.0);
60        }
61    }
62}