Rename raw to ffi

This commit is contained in:
Ivan Ukhov 2015-06-12 14:23:18 -04:00
parent f776e1daf6
commit 40e08d5b97
4 changed files with 37 additions and 37 deletions

View File

@ -1,5 +1,5 @@
use ffi;
use libc::{c_char, c_int, c_void};
use raw;
use std::marker::PhantomData;
use std::path::Path;
@ -7,9 +7,9 @@ use {Result, Statement};
/// A connection to a database.
pub struct Database<'l> {
raw: *mut raw::sqlite3,
raw: *mut ffi::sqlite3,
busy_callback: Option<Box<FnMut(usize) -> bool + 'l>>,
phantom: PhantomData<raw::sqlite3>,
phantom: PhantomData<ffi::sqlite3>,
}
impl<'l> Database<'l> {
@ -17,7 +17,7 @@ impl<'l> Database<'l> {
pub fn open(path: &Path) -> Result<Database<'l>> {
let mut raw = 0 as *mut _;
unsafe {
success!(raw::sqlite3_open(path_to_c_str!(path), &mut raw));
success!(ffi::sqlite3_open(path_to_c_str!(path), &mut raw));
}
Ok(Database {
raw: raw,
@ -30,7 +30,7 @@ impl<'l> Database<'l> {
#[inline]
pub fn execute(&self, sql: &str) -> Result<()> {
unsafe {
success!(self.raw, raw::sqlite3_exec(self.raw, str_to_c_str!(sql), None, 0 as *mut _,
success!(self.raw, ffi::sqlite3_exec(self.raw, str_to_c_str!(sql), None, 0 as *mut _,
0 as *mut _));
}
Ok(())
@ -46,7 +46,7 @@ impl<'l> Database<'l> {
{
unsafe {
let callback = Box::new(callback);
success!(self.raw, raw::sqlite3_exec(self.raw, str_to_c_str!(sql),
success!(self.raw, ffi::sqlite3_exec(self.raw, str_to_c_str!(sql),
Some(process_callback::<F>),
&*callback as *const F as *mut F as *mut _,
0 as *mut _));
@ -71,7 +71,7 @@ impl<'l> Database<'l> {
try!(self.remove_busy_handler());
unsafe {
let callback = Box::new(callback);
let result = raw::sqlite3_busy_handler(self.raw, Some(busy_callback::<F>),
let result = ffi::sqlite3_busy_handler(self.raw, Some(busy_callback::<F>),
&*callback as *const F as *mut F as *mut _);
self.busy_callback = Some(callback);
success!(self.raw, result);
@ -83,7 +83,7 @@ impl<'l> Database<'l> {
/// rejected operations until a timeout expires.
#[inline]
pub fn set_busy_timeout(&mut self, milliseconds: usize) -> Result<()> {
unsafe { success!(self.raw, raw::sqlite3_busy_timeout(self.raw, milliseconds as c_int)) };
unsafe { success!(self.raw, ffi::sqlite3_busy_timeout(self.raw, milliseconds as c_int)) };
Ok(())
}
@ -91,7 +91,7 @@ impl<'l> Database<'l> {
#[inline]
pub fn remove_busy_handler(&mut self) -> Result<()> {
::std::mem::replace(&mut self.busy_callback, None);
unsafe { success!(self.raw, raw::sqlite3_busy_handler(self.raw, None, 0 as *mut _)) };
unsafe { success!(self.raw, ffi::sqlite3_busy_handler(self.raw, None, 0 as *mut _)) };
Ok(())
}
}
@ -101,7 +101,7 @@ impl<'l> Drop for Database<'l> {
#[allow(unused_must_use)]
fn drop(&mut self) {
self.remove_busy_handler();
unsafe { raw::sqlite3_close(self.raw) };
unsafe { ffi::sqlite3_close(self.raw) };
}
}

View File

@ -1,4 +1,4 @@
use raw;
use ffi;
use std::convert::{From, Into};
use std::fmt::{self, Display, Formatter};
@ -14,14 +14,14 @@ macro_rules! declare(
/// An error kind.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ErrorKind {
$($left = raw::$right as isize,)*
$($left = ffi::$right as isize,)*
Unknown,
}
impl From<isize> for ErrorKind {
fn from(code: isize) -> ErrorKind {
match code as ::libc::c_int {
$(raw::$right => ErrorKind::$left,)*
$(ffi::$right => ErrorKind::$left,)*
_ => ErrorKind::Unknown,
}
}
@ -95,13 +95,13 @@ impl Display for ErrorKind {
}
}
pub fn last(raw: *mut raw::sqlite3) -> Option<Error> {
pub fn last(raw: *mut ffi::sqlite3) -> Option<Error> {
unsafe {
let code = raw::sqlite3_errcode(raw);
if code == raw::SQLITE_OK {
let code = ffi::sqlite3_errcode(raw);
if code == ffi::SQLITE_OK {
return None;
}
let message = raw::sqlite3_errmsg(raw);
let message = ffi::sqlite3_errmsg(raw);
if message.is_null() {
return None;
}

View File

@ -1,7 +1,7 @@
#![allow(unused_unsafe)]
extern crate libc;
extern crate sqlite3_sys as raw;
extern crate sqlite3_sys as ffi;
#[cfg(test)]
extern crate temporary;
@ -22,13 +22,13 @@ macro_rules! failure(
macro_rules! success(
($database:expr, $result:expr) => (
match $result {
::raw::SQLITE_OK => {},
::ffi::SQLITE_OK => {},
code => failure!($database, code),
}
);
($result:expr) => (
match $result {
::raw::SQLITE_OK => {},
::ffi::SQLITE_OK => {},
code => return Err(::Error::from(::ErrorKind::from(code as isize))),
}
);

View File

@ -1,13 +1,13 @@
use ffi;
use libc::{c_double, c_int};
use raw;
use std::marker::PhantomData;
use Result;
/// A prepared statement.
pub struct Statement<'l> {
raw: (*mut raw::sqlite3_stmt, *mut raw::sqlite3),
phantom: PhantomData<(raw::sqlite3_stmt, &'l raw::sqlite3)>,
raw: (*mut ffi::sqlite3_stmt, *mut ffi::sqlite3),
phantom: PhantomData<(ffi::sqlite3_stmt, &'l ffi::sqlite3)>,
}
/// A binding of a parameter of a prepared statement.
@ -39,17 +39,17 @@ impl<'l> Statement<'l> {
match *binding {
Binding::Float(i, value) => unsafe {
debug_assert!(i > 0, "the indexing starts from 1");
success!(self.raw.1, raw::sqlite3_bind_double(self.raw.0, i as c_int,
success!(self.raw.1, ffi::sqlite3_bind_double(self.raw.0, i as c_int,
value as c_double));
},
Binding::Integer(i, value) => unsafe {
debug_assert!(i > 0, "the indexing starts from 1");
success!(self.raw.1, raw::sqlite3_bind_int64(self.raw.0, i as c_int,
value as raw::sqlite3_int64));
success!(self.raw.1, ffi::sqlite3_bind_int64(self.raw.0, i as c_int,
value as ffi::sqlite3_int64));
},
Binding::Text(i, value) => unsafe {
debug_assert!(i > 0, "the indexing starts from 1");
success!(self.raw.1, raw::sqlite3_bind_text(self.raw.0, i as c_int,
success!(self.raw.1, ffi::sqlite3_bind_text(self.raw.0, i as c_int,
str_to_c_str!(value), -1, None));
},
}
@ -67,9 +67,9 @@ impl<'l> Statement<'l> {
/// Evaluate the statement.
pub fn step(&mut self) -> Result<State> {
match unsafe { raw::sqlite3_step(self.raw.0) } {
raw::SQLITE_DONE => Ok(State::Done),
raw::SQLITE_ROW => Ok(State::Row),
match unsafe { ffi::sqlite3_step(self.raw.0) } {
ffi::SQLITE_DONE => Ok(State::Done),
ffi::SQLITE_ROW => Ok(State::Row),
code => failure!(self.raw.1, code),
}
}
@ -77,7 +77,7 @@ impl<'l> Statement<'l> {
/// Reset the statement.
#[inline]
pub fn reset(&mut self) -> Result<()> {
unsafe { success!(self.raw.1, raw::sqlite3_reset(self.raw.0)) };
unsafe { success!(self.raw.1, ffi::sqlite3_reset(self.raw.0)) };
Ok(())
}
}
@ -85,26 +85,26 @@ impl<'l> Statement<'l> {
impl<'l> Drop for Statement<'l> {
#[inline]
fn drop(&mut self) {
unsafe { raw::sqlite3_finalize(self.raw.0) };
unsafe { ffi::sqlite3_finalize(self.raw.0) };
}
}
impl Value for f64 {
fn read(statement: &Statement, i: usize) -> Result<f64> {
Ok(unsafe { raw::sqlite3_column_double(statement.raw.0, i as c_int) as f64 })
Ok(unsafe { ffi::sqlite3_column_double(statement.raw.0, i as c_int) as f64 })
}
}
impl Value for i64 {
fn read(statement: &Statement, i: usize) -> Result<i64> {
Ok(unsafe { raw::sqlite3_column_int64(statement.raw.0, i as c_int) as i64 })
Ok(unsafe { ffi::sqlite3_column_int64(statement.raw.0, i as c_int) as i64 })
}
}
impl Value for String {
fn read(statement: &Statement, i: usize) -> Result<String> {
unsafe {
let pointer = raw::sqlite3_column_text(statement.raw.0, i as c_int);
let pointer = ffi::sqlite3_column_text(statement.raw.0, i as c_int);
if pointer.is_null() {
raise!("cannot read a TEXT column");
}
@ -114,10 +114,10 @@ impl Value for String {
}
#[inline]
pub fn new<'l>(raw1: *mut raw::sqlite3, sql: &str) -> Result<Statement<'l>> {
pub fn new<'l>(raw1: *mut ffi::sqlite3, sql: &str) -> Result<Statement<'l>> {
let mut raw0 = 0 as *mut _;
unsafe {
success!(raw1, raw::sqlite3_prepare(raw1, str_to_c_str!(sql), -1, &mut raw0, 0 as *mut _));
success!(raw1, ffi::sqlite3_prepare(raw1, str_to_c_str!(sql), -1, &mut raw0, 0 as *mut _));
}
Ok(Statement { raw: (raw0, raw1), phantom: PhantomData })
}