use Session;
use cassandra::error::{CassError, CassErrorResult};
use cassandra::prepared::PreparedStatement;
use cassandra::result::CassResult;
use cassandra::util::Protected;
use cassandra_sys::CASS_OK;
use cassandra_sys::CassFuture as _Future;
use cassandra_sys::CassFutureCallback as _CassFutureCallback;
use cassandra_sys::cass_future_custom_payload_item;
use cassandra_sys::cass_future_custom_payload_item_count;
use cassandra_sys::cass_future_error_code;
use cassandra_sys::cass_future_error_message;
use cassandra_sys::cass_future_free;
use cassandra_sys::cass_future_get_error_result;
use cassandra_sys::cass_future_get_prepared;
use cassandra_sys::cass_future_get_result;
use cassandra_sys::cass_future_ready;
use cassandra_sys::cass_future_set_callback;
use cassandra_sys::cass_future_wait;
use cassandra_sys::cass_future_wait_timed;
use cassandra_sys::cass_true;
use errors::*;
use std::mem;
use std::os::raw;
use std::slice;
use std::str;
#[derive(Debug)]
pub struct Future(*mut _Future);
#[derive(Debug)]
pub struct FutureCallback(_CassFutureCallback);
impl Drop for Future {
fn drop(&mut self) { unsafe { cass_future_free(self.0) } }
}
impl Future {
pub unsafe fn set_callback(&mut self, callback: FutureCallback, data: *mut raw::c_void) -> Result<&mut Self> {
cass_future_set_callback(self.0, callback.0, data).to_result(self).chain_err(|| "")
}
pub fn ready(&mut self) -> bool { unsafe { cass_future_ready(self.0) == cass_true } }
pub fn wait(self) -> Result<()> {
unsafe {
cass_future_wait(self.0);
self.error_code()
}
}
pub fn wait_timed(&mut self, timeout_us: u64) -> bool {
unsafe { cass_future_wait_timed(self.0, timeout_us) == cass_true }
}
pub fn get_result(&self) -> CassResult { unsafe { CassResult::build((cass_future_get_result(self.0))) } }
pub fn get_error_result(&self) -> CassErrorResult {
unsafe { CassErrorResult::build(cass_future_get_error_result(self.0)) }
}
fn error_code(self) -> Result<()> { unsafe { cass_future_error_code(self.0).to_result(()).chain_err(|| "") } }
pub fn error_message(&mut self) -> String {
unsafe {
let message = mem::zeroed();
let message_length = mem::zeroed();
cass_future_error_message(self.0, message, message_length);
let slice: &[u8] = slice::from_raw_parts(message as *const u8, message_length as usize);
str::from_utf8(slice).expect("must be utf8").to_owned()
}
}
pub fn payload_item_count(&self) -> usize { unsafe { cass_future_custom_payload_item_count(self.0) } }
pub fn payload_item(&self, index: usize) -> Result<(String, String)> {
unsafe {
let name = mem::zeroed();
let name_length = mem::zeroed();
let value = mem::zeroed();
let value_length = mem::zeroed();
match cass_future_custom_payload_item(self.0, index, name, name_length, value, value_length) {
CASS_OK => {
Ok((str::from_utf8(slice::from_raw_parts(name as *const u8, name_length as usize))
.expect("must be utf8")
.to_owned(),
str::from_utf8(slice::from_raw_parts(value as *const u8, value_length as usize))
.expect("must be utf8")
.to_owned()))
}
err => Err(err.to_result("").unwrap().into()),
}
}
}
}
#[must_use]
#[derive(Debug)]
pub struct ResultFuture(*mut _Future);
impl Drop for ResultFuture {
fn drop(&mut self) { unsafe { cass_future_free(self.0) } }
}
impl ResultFuture {
pub fn wait(&mut self) -> Result<CassResult> {
unsafe {
cass_future_wait(self.0);
self.error_code()
}
}
pub fn error_code(&mut self) -> Result<CassResult> {
unsafe {
let x = self.get();
let error_code = cass_future_error_code(self.0);
match (x, error_code) {
(Some(x), _) => Ok(x),
(None, err) => match err.to_result(()) {
Ok(_) => unimplemented!(),
Err(e) => Err(Error::with_chain(e, ErrorKind::CassandraError)),
}
}
}
}
pub fn error_message(&mut self) -> String {
unsafe {
let message = mem::zeroed();
let message_length = mem::zeroed();
cass_future_error_message(self.0, message, message_length);
let slice = slice::from_raw_parts(message as *const u8, message_length as usize);
str::from_utf8(slice).expect("must be utf8").to_owned()
}
}
pub fn get(&mut self) -> Option<CassResult> {
unsafe {
let result = cass_future_get_result(self.0);
if result.is_null() {
None
} else {
Some((CassResult::build(result)))
}
}
}
}
#[derive(Debug)]
pub struct PreparedFuture(*mut _Future);
impl Drop for PreparedFuture {
fn drop(&mut self) { unsafe { cass_future_free(self.0) } }
}
impl PreparedFuture {
pub fn wait(&mut self) -> Result<PreparedStatement> {
unsafe {
cass_future_wait(self.0);
self.error_code()
}
}
pub fn error_code(&mut self) -> Result<PreparedStatement> {
unsafe { cass_future_error_code(self.0).to_result(self.get()).chain_err(|| "") }
}
pub fn get(&mut self) -> PreparedStatement { unsafe { PreparedStatement::build(cass_future_get_prepared(self.0)) } }
}
#[derive(Debug)]
pub struct ConnectFuture(*mut _Future);
impl Protected<*mut _Future> for ConnectFuture {
fn inner(&self) -> *mut _Future { self.0 }
fn build(inner: *mut _Future) -> Self { ConnectFuture(inner) }
}
impl Drop for ConnectFuture {
fn drop(&mut self) { unsafe { cass_future_free(self.0) } }
}
#[derive(Debug)]
pub struct SessionFuture(*mut _Future);
impl SessionFuture {
pub fn wait(&mut self) -> Result<()> {
unsafe {
cass_future_wait(self.0);
self.error_code()
}
}
pub fn error_code(&self) -> Result<()> { unsafe { cass_future_error_code(self.0).to_result(()).chain_err(|| "") } }
pub fn get(&self) -> Option<CassResult> {
unsafe {
let result = cass_future_get_result(self.0);
debug!("result is null: {}", result.is_null());
if result.is_null() {
None
} else {
Some(CassResult::build(result))
}
}
}
}
impl Drop for SessionFuture {
fn drop(&mut self) { unsafe { cass_future_free(self.0) } }
}
#[derive(Debug)]
pub struct CloseFuture(*mut _Future);
impl Protected<*mut _Future> for Future {
fn inner(&self) -> *mut _Future { self.0 }
fn build(inner: *mut _Future) -> Self { Future(inner) }
}
impl Protected<*mut _Future> for PreparedFuture {
fn inner(&self) -> *mut _Future { self.0 }
fn build(inner: *mut _Future) -> Self { PreparedFuture(inner) }
}
impl Protected<*mut _Future> for ResultFuture {
fn inner(&self) -> *mut _Future { self.0 }
fn build(inner: *mut _Future) -> Self { ResultFuture(inner) }
}
impl Protected<*mut _Future> for SessionFuture {
fn inner(&self) -> *mut _Future { self.0 }
fn build(inner: *mut _Future) -> Self { SessionFuture(inner) }
}
impl Protected<*mut _Future> for CloseFuture {
fn inner(&self) -> *mut _Future { self.0 }
fn build(inner: *mut _Future) -> Self { CloseFuture(inner) }
}
impl Drop for CloseFuture {
fn drop(&mut self) { unsafe { cass_future_free(self.0) } }
}
impl CloseFuture {
pub fn wait(&self) -> Result<()> {
unsafe {
cass_future_wait(self.0);
self.error_code()
}
}
pub fn error_code(&self) -> Result<()> { unsafe { cass_future_error_code(self.0).to_result(()).chain_err(|| "") } }
pub fn get(&self) -> PreparedStatement { unsafe { PreparedStatement::build(cass_future_get_prepared(self.0)) } }
}