From 58b8b4c95d7e05fc525d1485061ed45b4290f706 Mon Sep 17 00:00:00 2001 From: gwenn Date: Sun, 6 May 2018 18:05:02 +0200 Subject: [PATCH] Introduce Aux associated type --- src/vtab/csvtab.rs | 4 +++- src/vtab/int_array.rs | 4 +++- src/vtab/mod.rs | 19 ++++++++++++++----- src/vtab/series.rs | 4 +++- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/vtab/csvtab.rs b/src/vtab/csvtab.rs index 1e5d137..89aae7d 100644 --- a/src/vtab/csvtab.rs +++ b/src/vtab/csvtab.rs @@ -34,6 +34,7 @@ pub fn load_module(conn: &Connection) -> Result<()> { init_module!( CSV_MODULE, CSVTab, + (), CSVTabCursor, csv_create, csv_connect, @@ -96,9 +97,10 @@ impl CSVTab { } impl VTab for CSVTab { + type Aux = (); type Cursor = CSVTabCursor; - unsafe fn connect(db: *mut ffi::sqlite3, _aux: *mut c_void, args: &[&[u8]]) -> Result { + unsafe fn connect(db: *mut ffi::sqlite3, _aux: *mut (), args: &[&[u8]]) -> Result { if args.len() < 4 { return Err(Error::ModuleError("no CSV file specified".to_owned())); } diff --git a/src/vtab/int_array.rs b/src/vtab/int_array.rs index 21eb54b..f786f6a 100644 --- a/src/vtab/int_array.rs +++ b/src/vtab/int_array.rs @@ -43,6 +43,7 @@ pub fn drop_int_array(conn: &Connection, name: &str) -> Result<()> { eponymous_module!( INT_ARRAY_MODULE, IntArrayVTab, + Rc>>, IntArrayVTabCursor, Some(int_array_connect), int_array_connect, @@ -66,11 +67,12 @@ struct IntArrayVTab { } impl VTab for IntArrayVTab { + type Aux = Rc>>; type Cursor = IntArrayVTabCursor; unsafe fn connect( db: *mut ffi::sqlite3, - aux: *mut c_void, + aux: *mut Rc>>, _args: &[&[u8]], ) -> Result { let array = aux as *const Rc>>; diff --git a/src/vtab/mod.rs b/src/vtab/mod.rs index 7a8575d..8db0dba 100644 --- a/src/vtab/mod.rs +++ b/src/vtab/mod.rs @@ -42,16 +42,18 @@ use {str_to_cstring, Connection, Error, InnerConnection, Result}; /// Virtual table instance trait. pub trait VTab: Sized { + type Aux; type Cursor: VTabCursor; + /// Create a new instance of a virtual table in response to a CREATE VIRTUAL TABLE statement. /// The `db` parameter is a pointer to the SQLite database connection that is executing /// the CREATE VIRTUAL TABLE statement. - unsafe fn create(db: *mut ffi::sqlite3, aux: *mut c_void, args: &[&[u8]]) -> Result { + unsafe fn create(db: *mut ffi::sqlite3, aux: *mut Self::Aux, args: &[&[u8]]) -> Result { Self::connect(db, aux, args) } /// Similar to `create`. The difference is that `connect` is called to establish a new connection /// to an _existing_ virtual table whereas `create` is called to create a new virtual table from scratch. - unsafe fn connect(db: *mut ffi::sqlite3, aux: *mut c_void, args: &[&[u8]]) -> Result; + unsafe fn connect(db: *mut ffi::sqlite3, aux: *mut Self::Aux, args: &[&[u8]]) -> Result; /// Determine the best way to access the virtual table. fn best_index(&self, info: &mut IndexInfo) -> Result<()>; /// Create a new cursor used for accessing a virtual table. @@ -385,6 +387,7 @@ macro_rules! init_module { ( $module_name:ident, $vtab:ident, + $aux:ty, $cursor:ty, $create:ident, $connect:ident, @@ -427,9 +430,10 @@ macro_rules! init_module { // The xConnect and xCreate methods do the same thing, but they must be // different so that the virtual table is not an eponymous virtual table. - create_or_connect!($vtab, $create, create); + create_or_connect!($vtab, $aux, $create, create); common_decl!( $vtab, + $aux, $cursor, $connect, $best_index, @@ -451,6 +455,7 @@ macro_rules! eponymous_module { ( $module_name:ident, $vtab:ident, + $aux:ty, $cursor:ty, $create:expr, $connect:ident, @@ -494,6 +499,7 @@ macro_rules! eponymous_module { common_decl!( $vtab, + $aux, $cursor, $connect, $best_index, @@ -511,7 +517,7 @@ macro_rules! eponymous_module { } // eponymous_module macro end macro_rules! create_or_connect { - ($vtab:ident, $create_or_connect:ident, $vtab_func:ident) => { + ($vtab:ident, $aux:ty, $create_or_connect:ident, $vtab_func:ident) => { unsafe extern "C" fn $create_or_connect( db: *mut ffi::sqlite3, aux: *mut c_void, @@ -524,6 +530,8 @@ macro_rules! create_or_connect { use std::ffi::CStr; use std::slice; use vtab::mprintf; + + let aux = aux as *mut $aux; let args = slice::from_raw_parts(argv, argc as usize); let vec = args.iter() .map(|&cs| CStr::from_ptr(cs).to_bytes()) @@ -552,6 +560,7 @@ macro_rules! create_or_connect { macro_rules! common_decl { ( $vtab:ident, + $aux:ty, $cursor:ty, $connect:ident, $best_index:ident, @@ -565,7 +574,7 @@ macro_rules! common_decl { $column:ident, $rowid:ident ) => { - create_or_connect!($vtab, $connect, connect); + create_or_connect!($vtab, $aux, $connect, connect); unsafe extern "C" fn $best_index( vtab: *mut ffi::sqlite3_vtab, info: *mut ffi::sqlite3_index_info, diff --git a/src/vtab/series.rs b/src/vtab/series.rs index 1feb8dd..1fecb4c 100644 --- a/src/vtab/series.rs +++ b/src/vtab/series.rs @@ -16,6 +16,7 @@ pub fn load_module(conn: &Connection) -> Result<()> { eponymous_module!( SERIES_MODULE, SeriesTab, + (), SeriesTabCursor, None, series_connect, @@ -61,11 +62,12 @@ struct SeriesTab { } impl VTab for SeriesTab { + type Aux = (); type Cursor = SeriesTabCursor; unsafe fn connect( db: *mut ffi::sqlite3, - _aux: *mut c_void, + _aux: *mut (), _args: &[&[u8]], ) -> Result { let vtab = SeriesTab {