1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use crate::cassandra::util::{Protected, ProtectedInner};
use crate::cassandra_sys::cass_retry_policy_default_new;
use crate::cassandra_sys::cass_retry_policy_downgrading_consistency_new;
use crate::cassandra_sys::cass_retry_policy_fallthrough_new;
use crate::cassandra_sys::cass_retry_policy_free;
use crate::cassandra_sys::cass_retry_policy_logging_new;
use crate::cassandra_sys::CassRetryPolicy as _RetryPolicy;

/// The selected retry policy
#[derive(Debug)]
pub struct RetryPolicy(*mut _RetryPolicy);

// The underlying C type has no thread-local state, and forbids only concurrent
// mutation/free: https://datastax.github.io/cpp-driver/topics/#thread-safety
unsafe impl Send for RetryPolicy {}
unsafe impl Sync for RetryPolicy {}

impl ProtectedInner<*mut _RetryPolicy> for RetryPolicy {
    fn inner(&self) -> *mut _RetryPolicy {
        self.0
    }
}

impl Protected<*mut _RetryPolicy> for RetryPolicy {
    fn build(inner: *mut _RetryPolicy) -> Self {
        if inner.is_null() {
            panic!("Unexpected null pointer")
        };
        RetryPolicy(inner)
    }
}

impl RetryPolicy {
    /// The default retry policy
    pub fn default_new() -> Self {
        unsafe { RetryPolicy::build(cass_retry_policy_default_new()) }
    }

    /// An auto-CL-downgrading consistency level
    pub fn downgrading_consistency_new() -> Self {
        unsafe { RetryPolicy(cass_retry_policy_downgrading_consistency_new()) }
    }

    /// a fallthrough retry policy
    pub fn fallthrough_new() -> Self {
        unsafe { RetryPolicy(cass_retry_policy_fallthrough_new()) }
    }

    /// The a logging retry policy
    pub fn logging_new(child_retry_policy: RetryPolicy) -> Self {
        // TODO: can return NULL
        unsafe { RetryPolicy::build(cass_retry_policy_logging_new(child_retry_policy.0)) }
    }
}

impl Drop for RetryPolicy {
    fn drop(&mut self) {
        unsafe {
            cass_retry_policy_free(self.0);
        }
    }
}