From 0c1c7cadfa72c1dbdd9f9b30287ec932aebf0040 Mon Sep 17 00:00:00 2001 From: Dan Burkert Date: Sun, 22 Feb 2015 21:25:55 -0800 Subject: [PATCH] update to new CString API --- src/database.rs | 2 +- src/environment.rs | 6 +++++- src/error.rs | 13 +++++-------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/database.rs b/src/database.rs index dc61b99..92de0a4 100644 --- a/src/database.rs +++ b/src/database.rs @@ -25,7 +25,7 @@ impl Database { name: Option<&str>, flags: c_uint) -> LmdbResult { - let c_name = name.map(|n| CString::from_slice(n.as_bytes())); + let c_name = name.map(|n| CString::new(n).unwrap()); let name_ptr = if let Some(ref c_name) = c_name { c_name.as_ptr() } else { ptr::null() }; let mut dbi: ffi::MDB_dbi = 0; try!(lmdb_result(ffi::mdb_dbi_open(txn, name_ptr, flags, &mut dbi))); diff --git a/src/environment.rs b/src/environment.rs index b298175..466e47a 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -53,6 +53,8 @@ impl Environment { /// /// This function will fail with `LmdbError::BadRslot` if called by a thread which has an ongoing /// transaction. + /// + /// The database name may not contain the null character. pub fn open_db<'env>(&'env self, name: Option<&str>) -> LmdbResult { let mutex = self.dbi_open_mutex.lock(); let txn = try!(self.begin_ro_txn()); @@ -162,6 +164,8 @@ pub struct EnvironmentBuilder { impl EnvironmentBuilder { /// Open an environment. + /// + /// The path may not contain the null character. pub fn open(&self, path: &Path, mode: FilePermission) -> LmdbResult { let mut env: *mut ffi::MDB_env = ptr::null_mut(); unsafe { @@ -179,7 +183,7 @@ impl EnvironmentBuilder { ffi::mdb_env_close(env)) } lmdb_try_with_cleanup!(ffi::mdb_env_open(env, - CString::from_slice(path.as_os_str().as_bytes()).as_ptr(), + CString::new(path.as_os_str().as_bytes()).unwrap().as_ptr(), self.flags.bits(), mode.bits() as mode_t), ffi::mdb_env_close(env)); diff --git a/src/error.rs b/src/error.rs index 93f7456..6c16266 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,9 +1,7 @@ use libc::c_int; -use std; -use std::mem; use std::error::Error; -use std::fmt; -use std::str; +use std::ffi::CStr; +use std::{fmt, str}; use ffi; @@ -115,11 +113,10 @@ impl fmt::Display for LmdbError { impl Error for LmdbError { fn description(&self) -> &str { - unsafe { + unsafe { // This is safe since the error messages returned from mdb_strerror are static. - let err: &'static *const i8 = - mem::transmute(&(ffi::mdb_strerror(self.to_err_code()) as *const i8)); - str::from_utf8_unchecked(std::ffi::c_str_to_bytes(err)) + let err: *const i8 = ffi::mdb_strerror(self.to_err_code()) as *const i8; + str::from_utf8_unchecked(CStr::from_ptr(err).to_bytes()) } } }