rocksdb/
ffi_util.rs

1// Copyright 2016 Alex Regueiro
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15
16use crate::{ffi, Error};
17use libc::{self, c_char, c_void, size_t};
18use std::ffi::{CStr, CString};
19use std::path::Path;
20use std::ptr;
21
22pub(crate) unsafe fn from_cstr(ptr: *const c_char) -> String {
23    let cstr = CStr::from_ptr(ptr as *const _);
24    String::from_utf8_lossy(cstr.to_bytes()).into_owned()
25}
26
27pub(crate) unsafe fn raw_data(ptr: *const c_char, size: usize) -> Option<Vec<u8>> {
28    if ptr.is_null() {
29        None
30    } else {
31        let mut dst = vec![0; size];
32        ptr::copy_nonoverlapping(ptr as *const u8, dst.as_mut_ptr(), size);
33
34        Some(dst)
35    }
36}
37
38pub fn error_message(ptr: *const c_char) -> String {
39    unsafe {
40        let s = from_cstr(ptr);
41        ffi::rocksdb_free(ptr as *mut c_void);
42        s
43    }
44}
45
46pub fn opt_bytes_to_ptr<T: AsRef<[u8]>>(opt: Option<T>) -> *const c_char {
47    match opt {
48        Some(v) => v.as_ref().as_ptr() as *const c_char,
49        None => ptr::null(),
50    }
51}
52
53pub(crate) fn to_cpath<P: AsRef<Path>>(path: P) -> Result<CString, Error> {
54    match CString::new(path.as_ref().to_string_lossy().as_bytes()) {
55        Ok(c) => Ok(c),
56        Err(e) => Err(Error::new(format!(
57            "Failed to convert path to CString: {e}"
58        ))),
59    }
60}
61
62macro_rules! ffi_try {
63    ( $($function:ident)::*() ) => {
64        ffi_try_impl!($($function)::*())
65    };
66
67    ( $($function:ident)::*( $arg1:expr $(, $arg:expr)* $(,)? ) ) => {
68        ffi_try_impl!($($function)::*($arg1 $(, $arg)* ,))
69    };
70}
71
72macro_rules! ffi_try_impl {
73    ( $($function:ident)::*( $($arg:expr,)*) ) => {{
74        let mut err: *mut ::libc::c_char = ::std::ptr::null_mut();
75        let result = $($function)::*($($arg,)* &mut err);
76        if !err.is_null() {
77            return Err(Error::new($crate::ffi_util::error_message(err)));
78        }
79        result
80    }};
81}
82
83/// Value which can be converted into a C string.
84///
85/// The trait is used as argument to functions which wish to accept either
86/// [`&str`] or [`&CStr`](CStr) arguments while internally need to interact with
87/// C APIs.  Accepting [`&str`] may be more convenient for users but requires
88/// conversion into [`CString`] internally which requires allocation.  With this
89/// trait, latency-conscious users may choose to prepare [`CStr`] in advance and
90/// then pass it directly without having to incur the conversion cost.
91///
92/// To use the trait, function should accept `impl CStrLike` and after baking
93/// the argument (with [`CStrLike::bake`] method) it can use it as a [`&CStr`](CStr)
94/// (since the baked result dereferences into [`CStr`]).
95///
96/// # Example
97///
98/// ```
99/// use std::ffi::{CStr, CString};
100/// use rocksdb::CStrLike;
101///
102/// fn strlen(arg: impl CStrLike) -> std::result::Result<usize, String> {
103///     let baked = arg.bake().map_err(|err| err.to_string())?;
104///     Ok(unsafe { libc::strlen(baked.as_ptr()) })
105/// }
106///
107/// const FOO: &str = "foo";
108/// const BAR: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"bar\0") };
109///
110/// assert_eq!(Ok(3), strlen(FOO));
111/// assert_eq!(Ok(3), strlen(BAR));
112/// ```
113pub trait CStrLike {
114    type Baked: std::ops::Deref<Target = CStr>;
115    type Error: std::fmt::Debug + std::fmt::Display;
116
117    /// Bakes self into value which can be freely converted into [`&CStr`](CStr).
118    ///
119    /// This may require allocation and may fail if `self` has invalid value.
120    fn bake(self) -> Result<Self::Baked, Self::Error>;
121
122    /// Consumers and converts value into an owned [`CString`].
123    ///
124    /// If `Self` is already a `CString` simply returns it; if it’s a reference
125    /// to a `CString` then the value is cloned.  In other cases this may
126    /// require allocation and may fail if `self` has invalid value.
127    fn into_c_string(self) -> Result<CString, Self::Error>;
128}
129
130impl CStrLike for &str {
131    type Baked = CString;
132    type Error = std::ffi::NulError;
133
134    fn bake(self) -> Result<Self::Baked, Self::Error> {
135        CString::new(self)
136    }
137    fn into_c_string(self) -> Result<CString, Self::Error> {
138        CString::new(self)
139    }
140}
141
142// This is redundant for the most part and exists so that `foo(&string)` (where
143// `string: String` works just as if `foo` took `arg: &str` argument.
144impl CStrLike for &String {
145    type Baked = CString;
146    type Error = std::ffi::NulError;
147
148    fn bake(self) -> Result<Self::Baked, Self::Error> {
149        CString::new(self.as_bytes())
150    }
151    fn into_c_string(self) -> Result<CString, Self::Error> {
152        CString::new(self.as_bytes())
153    }
154}
155
156impl CStrLike for &CStr {
157    type Baked = Self;
158    type Error = std::convert::Infallible;
159
160    fn bake(self) -> Result<Self::Baked, Self::Error> {
161        Ok(self)
162    }
163    fn into_c_string(self) -> Result<CString, Self::Error> {
164        Ok(self.to_owned())
165    }
166}
167
168// This exists so that if caller constructs a `CString` they can pass it into
169// the function accepting `CStrLike` argument.  Some of such functions may take
170// the argument whereas otherwise they would need to allocated a new owned
171// object.
172impl CStrLike for CString {
173    type Baked = CString;
174    type Error = std::convert::Infallible;
175
176    fn bake(self) -> Result<Self::Baked, Self::Error> {
177        Ok(self)
178    }
179    fn into_c_string(self) -> Result<CString, Self::Error> {
180        Ok(self)
181    }
182}
183
184// This is redundant for the most part and exists so that `foo(&cstring)` (where
185// `string: CString` works just as if `foo` took `arg: &CStr` argument.
186impl<'a> CStrLike for &'a CString {
187    type Baked = &'a CStr;
188    type Error = std::convert::Infallible;
189
190    fn bake(self) -> Result<Self::Baked, Self::Error> {
191        Ok(self)
192    }
193    fn into_c_string(self) -> Result<CString, Self::Error> {
194        Ok(self.clone())
195    }
196}
197
198/// Owned malloc-allocated memory slice.
199/// Do not derive `Clone` for this because it will cause double-free.
200pub struct CSlice {
201    data: *const c_char,
202    len: size_t,
203}
204
205impl CSlice {
206    /// Constructing such a slice may be unsafe.
207    ///
208    /// # Safety
209    /// The caller must ensure that the pointer and length are valid.
210    /// Moreover, `CSlice` takes the ownership of the memory and will free it
211    /// using `rocksdb_free`. The caller must ensure that the memory is
212    /// allocated by `malloc` in RocksDB and will not be freed by any other
213    /// means.
214    pub(crate) unsafe fn from_raw_parts(data: *const c_char, len: size_t) -> Self {
215        Self { data, len }
216    }
217}
218
219impl AsRef<[u8]> for CSlice {
220    fn as_ref(&self) -> &[u8] {
221        unsafe { std::slice::from_raw_parts(self.data as *const u8, self.len) }
222    }
223}
224
225impl Drop for CSlice {
226    fn drop(&mut self) {
227        unsafe {
228            ffi::rocksdb_free(self.data as *mut c_void);
229        }
230    }
231}
232
233#[test]
234fn test_c_str_like_bake() {
235    fn test<S: CStrLike>(value: S) -> Result<usize, S::Error> {
236        value
237            .bake()
238            .map(|value| unsafe { libc::strlen(value.as_ptr()) })
239    }
240
241    assert_eq!(Ok(3), test("foo")); // &str
242    assert_eq!(Ok(3), test(&String::from("foo"))); // String
243    assert_eq!(Ok(3), test(CString::new("foo").unwrap().as_ref())); // &CStr
244    assert_eq!(Ok(3), test(&CString::new("foo").unwrap())); // &CString
245    assert_eq!(Ok(3), test(CString::new("foo").unwrap())); // CString
246
247    assert_eq!(3, test("foo\0bar").err().unwrap().nul_position());
248}
249
250#[test]
251fn test_c_str_like_into() {
252    fn test<S: CStrLike>(value: S) -> Result<CString, S::Error> {
253        value.into_c_string()
254    }
255
256    let want = CString::new("foo").unwrap();
257
258    assert_eq!(Ok(want.clone()), test("foo")); // &str
259    assert_eq!(Ok(want.clone()), test(&String::from("foo"))); // &String
260    assert_eq!(
261        Ok(want.clone()),
262        test(CString::new("foo").unwrap().as_ref())
263    ); // &CStr
264    assert_eq!(Ok(want.clone()), test(&CString::new("foo").unwrap())); // &CString
265    assert_eq!(Ok(want), test(CString::new("foo").unwrap())); // CString
266
267    assert_eq!(3, test("foo\0bar").err().unwrap().nul_position());
268}