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 {
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
// instead...
#[link(name = "rocksdb")]
......@@ -482,7 +495,8 @@ extern "C" {
mod test {
use super::*;
use std::ffi::{CStr, CString};
use libc::{self, c_void, c_char};
use std::ptr;
use libc::{self, c_void};
use tempdir::TempDir;
#[test]
......@@ -501,7 +515,7 @@ mod test {
.unwrap();
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);
assert!(err.is_null(), error_message(err));
......
......@@ -17,6 +17,7 @@ extern crate libc;
#[cfg(test)]
extern crate tempdir;
#[macro_use]
pub extern crate librocksdb_sys;
pub mod rocksdb;
......
This diff is collapsed.
......@@ -23,7 +23,7 @@ use merge_operator::MergeFn;
use rocksdb_ffi::{self, DBOptions, DBWriteOptions, DBBlockBasedTableOptions,
DBReadOptions, DBCompressionType, DBRecoveryMode,
DBSnapshot, DBInstance};
DBSnapshot, DBInstance, DBFlushOptions};
use std::ffi::CString;
use std::mem;
......@@ -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)]
mod tests {
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