use crate::{Error, ffi};
use libc::{self, c_char, c_void, size_t};
use std::ffi::{CStr, CString};
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
use std::path::Path;
use std::ptr;
pub(crate) unsafe fn from_cstr_without_free(ptr: *const c_char) -> String {
let cstr = unsafe { CStr::from_ptr(ptr as *const _) };
String::from_utf8_lossy(cstr.to_bytes()).into_owned()
}
pub(crate) unsafe fn from_cstr_and_free(ptr: *const c_char) -> String {
let cstr = unsafe { CStr::from_ptr(ptr as *const _) };
let s = String::from_utf8_lossy(cstr.to_bytes()).into_owned();
unsafe { ffi::rocksdb_free(ptr as *mut c_void) };
s
}
pub(crate) unsafe fn raw_data(ptr: *const c_char, size: usize) -> Option<Vec<u8>> {
if ptr.is_null() {
None
} else {
Some(unsafe { std::slice::from_raw_parts(ptr as *const u8, size) }.to_vec())
}
}
pub fn convert_rocksdb_error(rocksdb_err: *const c_char) -> Error {
let rocksdb_err_str = unsafe { from_cstr_and_free(rocksdb_err) };
Error::new(rocksdb_err_str)
}
pub fn opt_bytes_to_ptr<T: AsRef<[u8]> + ?Sized>(opt: Option<&T>) -> *const c_char {
match opt {
Some(v) => v.as_ref().as_ptr() as *const c_char,
None => ptr::null(),
}
}
#[cfg(unix)]
pub(crate) fn to_cpath<P: AsRef<Path>>(path: P) -> Result<CString, Error> {
CString::new(path.as_ref().as_os_str().as_bytes())
.map_err(|e| Error::new(format!("Failed to convert path to CString: {e}")))
}
#[cfg(not(unix))]
pub(crate) fn to_cpath<P: AsRef<Path>>(path: P) -> Result<CString, Error> {
match CString::new(path.as_ref().to_string_lossy().as_bytes()) {
Ok(c) => Ok(c),
Err(e) => Err(Error::new(format!(
"Failed to convert path to CString: {e}"
))),
}
}
macro_rules! ffi_try {
( $($function:ident)::*() ) => {
ffi_try_impl!($($function)::*())
};
( $($function:ident)::*( $arg1:expr $(, $arg:expr)* $(,)? ) ) => {
ffi_try_impl!($($function)::*($arg1 $(, $arg)* ,))
};
}
macro_rules! ffi_try_impl {
( $($function:ident)::*( $($arg:expr,)*) ) => {{
let mut err: *mut ::libc::c_char = ::std::ptr::null_mut();
let result = $($function)::*($($arg,)* &mut err);
if !err.is_null() {
return Err($crate::ffi_util::convert_rocksdb_error(err));
}
result
}};
}
pub trait CStrLike {
type Baked: std::ops::Deref<Target = CStr>;
type Error: std::fmt::Debug + std::fmt::Display;
fn bake(self) -> Result<Self::Baked, Self::Error>;
fn into_c_string(self) -> Result<CString, Self::Error>;
}
impl CStrLike for &str {
type Baked = CString;
type Error = std::ffi::NulError;
fn bake(self) -> Result<Self::Baked, Self::Error> {
CString::new(self)
}
fn into_c_string(self) -> Result<CString, Self::Error> {
CString::new(self)
}
}
impl CStrLike for &String {
type Baked = CString;
type Error = std::ffi::NulError;
fn bake(self) -> Result<Self::Baked, Self::Error> {
CString::new(self.as_bytes())
}
fn into_c_string(self) -> Result<CString, Self::Error> {
CString::new(self.as_bytes())
}
}
impl CStrLike for &CStr {
type Baked = Self;
type Error = std::convert::Infallible;
fn bake(self) -> Result<Self::Baked, Self::Error> {
Ok(self)
}
fn into_c_string(self) -> Result<CString, Self::Error> {
Ok(self.to_owned())
}
}
impl CStrLike for CString {
type Baked = CString;
type Error = std::convert::Infallible;
fn bake(self) -> Result<Self::Baked, Self::Error> {
Ok(self)
}
fn into_c_string(self) -> Result<CString, Self::Error> {
Ok(self)
}
}
impl<'a> CStrLike for &'a CString {
type Baked = &'a CStr;
type Error = std::convert::Infallible;
fn bake(self) -> Result<Self::Baked, Self::Error> {
Ok(self)
}
fn into_c_string(self) -> Result<CString, Self::Error> {
Ok(self.clone())
}
}
pub struct CSlice {
data: *const c_char,
len: size_t,
}
impl CSlice {
pub(crate) unsafe fn from_raw_parts(data: *const c_char, len: size_t) -> Self {
Self { data, len }
}
}
impl AsRef<[u8]> for CSlice {
fn as_ref(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.data as *const u8, self.len) }
}
}
impl Drop for CSlice {
fn drop(&mut self) {
unsafe {
ffi::rocksdb_free(self.data as *mut c_void);
}
}
}
#[test]
fn test_c_str_like_bake() {
fn test<S: CStrLike>(value: S) -> Result<usize, S::Error> {
value
.bake()
.map(|value| unsafe { libc::strlen(value.as_ptr()) })
}
assert_eq!(Ok(3), test("foo")); assert_eq!(Ok(3), test(&String::from("foo"))); assert_eq!(Ok(3), test(CString::new("foo").unwrap().as_ref())); assert_eq!(Ok(3), test(&CString::new("foo").unwrap())); assert_eq!(Ok(3), test(CString::new("foo").unwrap()));
assert_eq!(3, test("foo\0bar").err().unwrap().nul_position());
}
#[test]
fn test_c_str_like_into() {
fn test<S: CStrLike>(value: S) -> Result<CString, S::Error> {
value.into_c_string()
}
let want = CString::new("foo").unwrap();
assert_eq!(Ok(want.clone()), test("foo")); assert_eq!(Ok(want.clone()), test(&String::from("foo"))); assert_eq!(
Ok(want.clone()),
test(CString::new("foo").unwrap().as_ref())
); assert_eq!(Ok(want.clone()), test(&CString::new("foo").unwrap())); assert_eq!(Ok(want), test(CString::new("foo").unwrap()));
assert_eq!(3, test("foo\0bar").err().unwrap().nul_position());
}