update to new CString API

without.crypto
Dan Burkert 10 years ago
parent dee8811fd0
commit 0c1c7cadfa
  1. 2
      src/database.rs
  2. 6
      src/environment.rs
  3. 11
      src/error.rs

@ -25,7 +25,7 @@ impl Database {
name: Option<&str>, name: Option<&str>,
flags: c_uint) flags: c_uint)
-> LmdbResult<Database> { -> LmdbResult<Database> {
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 name_ptr = if let Some(ref c_name) = c_name { c_name.as_ptr() } else { ptr::null() };
let mut dbi: ffi::MDB_dbi = 0; let mut dbi: ffi::MDB_dbi = 0;
try!(lmdb_result(ffi::mdb_dbi_open(txn, name_ptr, flags, &mut dbi))); try!(lmdb_result(ffi::mdb_dbi_open(txn, name_ptr, flags, &mut dbi)));

@ -53,6 +53,8 @@ impl Environment {
/// ///
/// This function will fail with `LmdbError::BadRslot` if called by a thread which has an ongoing /// This function will fail with `LmdbError::BadRslot` if called by a thread which has an ongoing
/// transaction. /// transaction.
///
/// The database name may not contain the null character.
pub fn open_db<'env>(&'env self, name: Option<&str>) -> LmdbResult<Database> { pub fn open_db<'env>(&'env self, name: Option<&str>) -> LmdbResult<Database> {
let mutex = self.dbi_open_mutex.lock(); let mutex = self.dbi_open_mutex.lock();
let txn = try!(self.begin_ro_txn()); let txn = try!(self.begin_ro_txn());
@ -162,6 +164,8 @@ pub struct EnvironmentBuilder {
impl EnvironmentBuilder { impl EnvironmentBuilder {
/// Open an environment. /// Open an environment.
///
/// The path may not contain the null character.
pub fn open(&self, path: &Path, mode: FilePermission) -> LmdbResult<Environment> { pub fn open(&self, path: &Path, mode: FilePermission) -> LmdbResult<Environment> {
let mut env: *mut ffi::MDB_env = ptr::null_mut(); let mut env: *mut ffi::MDB_env = ptr::null_mut();
unsafe { unsafe {
@ -179,7 +183,7 @@ impl EnvironmentBuilder {
ffi::mdb_env_close(env)) ffi::mdb_env_close(env))
} }
lmdb_try_with_cleanup!(ffi::mdb_env_open(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(), self.flags.bits(),
mode.bits() as mode_t), mode.bits() as mode_t),
ffi::mdb_env_close(env)); ffi::mdb_env_close(env));

@ -1,9 +1,7 @@
use libc::c_int; use libc::c_int;
use std;
use std::mem;
use std::error::Error; use std::error::Error;
use std::fmt; use std::ffi::CStr;
use std::str; use std::{fmt, str};
use ffi; use ffi;
@ -117,9 +115,8 @@ impl Error for LmdbError {
fn description(&self) -> &str { fn description(&self) -> &str {
unsafe { unsafe {
// This is safe since the error messages returned from mdb_strerror are static. // This is safe since the error messages returned from mdb_strerror are static.
let err: &'static *const i8 = let err: *const i8 = ffi::mdb_strerror(self.to_err_code()) as *const i8;
mem::transmute(&(ffi::mdb_strerror(self.to_err_code()) as *const i8)); str::from_utf8_unchecked(CStr::from_ptr(err).to_bytes())
str::from_utf8_unchecked(std::ffi::c_str_to_bytes(err))
} }
} }
} }

Loading…
Cancel
Save