Commit 41468f31 authored by Jay's avatar Jay Committed by GitHub

add ffi_try to simplify error handle (#46)

parent a09bd206
...@@ -86,6 +86,19 @@ pub fn error_message(ptr: *mut c_char) -> String { ...@@ -86,6 +86,19 @@ pub fn error_message(ptr: *mut c_char) -> String {
s s
} }
#[macro_export]
macro_rules! ffi_try {
($func:ident($($arg:expr),*)) => ({
use std::ptr;
let mut err = ptr::null_mut();
let res = $crate::$func($($arg),*, &mut err);
if !err.is_null() {
return Err($crate::error_message(err));
}
res
})
}
// TODO audit the use of boolean arguments, b/c I think they need to be u8 // TODO audit the use of boolean arguments, b/c I think they need to be u8
// instead... // instead...
#[link(name = "rocksdb")] #[link(name = "rocksdb")]
...@@ -482,7 +495,8 @@ extern "C" { ...@@ -482,7 +495,8 @@ extern "C" {
mod test { mod test {
use super::*; use super::*;
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
use libc::{self, c_void, c_char}; use std::ptr;
use libc::{self, c_void};
use tempdir::TempDir; use tempdir::TempDir;
#[test] #[test]
...@@ -501,7 +515,7 @@ mod test { ...@@ -501,7 +515,7 @@ mod test {
.unwrap(); .unwrap();
let cpath_ptr = cpath.as_ptr(); let cpath_ptr = cpath.as_ptr();
let mut err = 0 as *mut c_char; let mut err = ptr::null_mut();
let db = rocksdb_open(opts, cpath_ptr, &mut err); let db = rocksdb_open(opts, cpath_ptr, &mut err);
assert!(err.is_null(), error_message(err)); assert!(err.is_null(), error_message(err));
......
...@@ -17,6 +17,7 @@ extern crate libc; ...@@ -17,6 +17,7 @@ extern crate libc;
#[cfg(test)] #[cfg(test)]
extern crate tempdir; extern crate tempdir;
#[macro_use]
pub extern crate librocksdb_sys; pub extern crate librocksdb_sys;
pub mod rocksdb; pub mod rocksdb;
......
This diff is collapsed.
...@@ -23,7 +23,7 @@ use merge_operator::MergeFn; ...@@ -23,7 +23,7 @@ use merge_operator::MergeFn;
use rocksdb_ffi::{self, DBOptions, DBWriteOptions, DBBlockBasedTableOptions, use rocksdb_ffi::{self, DBOptions, DBWriteOptions, DBBlockBasedTableOptions,
DBReadOptions, DBCompressionType, DBRecoveryMode, DBReadOptions, DBCompressionType, DBRecoveryMode,
DBSnapshot, DBInstance}; DBSnapshot, DBInstance, DBFlushOptions};
use std::ffi::CString; use std::ffi::CString;
use std::mem; use std::mem;
...@@ -565,6 +565,30 @@ impl Options { ...@@ -565,6 +565,30 @@ impl Options {
} }
} }
pub struct FlushOptions {
pub inner: *mut DBFlushOptions,
}
impl FlushOptions {
pub fn new() -> FlushOptions {
unsafe {
FlushOptions {
inner: rocksdb_ffi::rocksdb_flushoptions_create(),
}
}
}
pub fn set_wait(&mut self, wait: bool) {
unsafe {rocksdb_ffi::rocksdb_flushoptions_set_wait(self.inner, wait);}
}
}
impl Drop for FlushOptions {
fn drop(&mut self) {
unsafe {rocksdb_ffi::rocksdb_flushoptions_destroy(self.inner);}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::Options; use super::Options;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment