Use associated types instead of generics

This commit is contained in:
gwenn 2018-05-06 17:19:19 +02:00
parent 10ba0514e6
commit 8e6ea05efa
4 changed files with 24 additions and 10 deletions

View File

@ -89,7 +89,9 @@ impl CSVTab {
} }
} }
impl VTab<CSVTabCursor> for CSVTab { impl VTab for CSVTab {
type Cursor = CSVTabCursor;
unsafe fn connect(db: *mut ffi::sqlite3, _aux: *mut c_void, args: &[&[u8]]) -> Result<CSVTab> { unsafe fn connect(db: *mut ffi::sqlite3, _aux: *mut c_void, args: &[&[u8]]) -> Result<CSVTab> {
if args.len() < 4 { if args.len() < 4 {
return Err(Error::ModuleError("no CSV file specified".to_owned())); return Err(Error::ModuleError("no CSV file specified".to_owned()));
@ -246,7 +248,9 @@ impl CSVTabCursor {
} }
} }
impl VTabCursor<CSVTab> for CSVTabCursor { impl VTabCursor for CSVTabCursor {
type Table = CSVTab;
fn vtab(&self) -> &CSVTab { fn vtab(&self) -> &CSVTab {
unsafe { & *(self.base.pVtab as *const CSVTab) } unsafe { & *(self.base.pVtab as *const CSVTab) }
} }

View File

@ -59,7 +59,9 @@ struct IntArrayVTab {
array: *const Rc<RefCell<Vec<i64>>>, array: *const Rc<RefCell<Vec<i64>>>,
} }
impl VTab<IntArrayVTabCursor> for IntArrayVTab { impl VTab for IntArrayVTab {
type Cursor = IntArrayVTabCursor;
unsafe fn connect(db: *mut ffi::sqlite3, unsafe fn connect(db: *mut ffi::sqlite3,
aux: *mut c_void, aux: *mut c_void,
_args: &[&[u8]]) _args: &[&[u8]])
@ -100,7 +102,9 @@ impl IntArrayVTabCursor {
} }
} }
impl VTabCursor<IntArrayVTab> for IntArrayVTabCursor { impl VTabCursor for IntArrayVTabCursor {
type Table = IntArrayVTab;
fn vtab(&self) -> &IntArrayVTab { fn vtab(&self) -> &IntArrayVTab {
unsafe { & *(self.base.pVtab as *const IntArrayVTab) } unsafe { & *(self.base.pVtab as *const IntArrayVTab) }
} }

View File

@ -41,7 +41,8 @@ use types::{FromSql, FromSqlError, ToSql, ValueRef};
// //
/// Virtual table instance trait. /// Virtual table instance trait.
pub trait VTab<C: VTabCursor<Self>>: Sized { pub trait VTab: Sized {
type Cursor: VTabCursor;
/// Create a new instance of a virtual table in response to a CREATE VIRTUAL TABLE statement. /// 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 `db` parameter is a pointer to the SQLite database connection that is executing
/// the CREATE VIRTUAL TABLE statement. /// the CREATE VIRTUAL TABLE statement.
@ -54,7 +55,7 @@ pub trait VTab<C: VTabCursor<Self>>: Sized {
/// Determine the best way to access the virtual table. /// Determine the best way to access the virtual table.
fn best_index(&self, info: &mut IndexInfo) -> Result<()>; fn best_index(&self, info: &mut IndexInfo) -> Result<()>;
/// Create a new cursor used for accessing a virtual table. /// Create a new cursor used for accessing a virtual table.
fn open(&self) -> Result<C>; fn open(&self) -> Result<Self::Cursor>;
} }
bitflags! { bitflags! {
@ -180,9 +181,10 @@ impl<'a> IndexConstraintUsage<'a> {
} }
/// Virtual table cursor trait. /// Virtual table cursor trait.
pub trait VTabCursor<V: VTab<Self>>: Sized { pub trait VTabCursor: Sized {
type Table: VTab;
/// Accessor to the associated virtual table. /// Accessor to the associated virtual table.
fn vtab(&self) -> &V; fn vtab(&self) -> &Self::Table;
/// Begin a search of a virtual table. /// Begin a search of a virtual table.
fn filter(&mut self, idx_num: c_int, idx_str: Option<&str>, args: &Values) -> Result<()>; fn filter(&mut self, idx_num: c_int, idx_str: Option<&str>, args: &Values) -> Result<()>;
/// Advance cursor to the next row of a result set initiated by `filter`. /// Advance cursor to the next row of a result set initiated by `filter`.

View File

@ -60,7 +60,9 @@ struct SeriesTab {
} }
impl VTab<SeriesTabCursor> for SeriesTab { impl VTab for SeriesTab {
type Cursor = SeriesTabCursor;
unsafe fn connect(db: *mut ffi::sqlite3, unsafe fn connect(db: *mut ffi::sqlite3,
_aux: *mut c_void, _aux: *mut c_void,
_args: &[&[u8]]) _args: &[&[u8]])
@ -171,7 +173,9 @@ impl SeriesTabCursor {
Default::default() Default::default()
} }
} }
impl VTabCursor<SeriesTab> for SeriesTabCursor { impl VTabCursor for SeriesTabCursor {
type Table = SeriesTab;
fn vtab(&self) -> &SeriesTab { fn vtab(&self) -> &SeriesTab {
unsafe { & *(self.base.pVtab as *const SeriesTab) } unsafe { & *(self.base.pVtab as *const SeriesTab) }
} }