discord_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, but does not support access
14// from multiple threads: https://datastax.github.io/cpp-driver/topics/#thread-safety
15unsafe impl Send for RetryPolicy {}
16
17impl ProtectedInner<*mut _RetryPolicy> for RetryPolicy {
18    fn inner(&self) -> *mut _RetryPolicy {
19        self.0
20    }
21}
22
23impl Protected<*mut _RetryPolicy> for RetryPolicy {
24    fn build(inner: *mut _RetryPolicy) -> Self {
25        if inner.is_null() {
26            panic!("Unexpected null pointer")
27        };
28        RetryPolicy(inner)
29    }
30}
31
32impl RetryPolicy {
33    /// The default retry policy
34    pub fn default_new() -> Self {
35        unsafe { RetryPolicy::build(cass_retry_policy_default_new()) }
36    }
37
38    /// An auto-CL-downgrading consistency level
39    pub fn downgrading_consistency_new() -> Self {
40        unsafe { RetryPolicy(cass_retry_policy_downgrading_consistency_new()) }
41    }
42
43    /// a fallthrough retry policy
44    pub fn fallthrough_new() -> Self {
45        unsafe { RetryPolicy(cass_retry_policy_fallthrough_new()) }
46    }
47
48    /// The a logging retry policy
49    pub fn logging_new(child_retry_policy: RetryPolicy) -> Self {
50        // TODO: can return NULL
51        unsafe { RetryPolicy::build(cass_retry_policy_logging_new(child_retry_policy.0)) }
52    }
53}
54
55impl Drop for RetryPolicy {
56    fn drop(&mut self) {
57        unsafe {
58            cass_retry_policy_free(self.0);
59        }
60    }
61}