VTabCursor lifetime should be bound to VTab lifetime

```c
struct sqlite3_vtab_cursor {
  sqlite3_vtab *pVtab;      /* Virtual table of this cursor */
```
It seems that we need to introduce a lifetime on `VTab` trait
to express such constraint:
https://users.rust-lang.org/t/associated-type-with-lifetime-bound/7256
But there may be another/new way to do it.
This commit is contained in:
gwenn
2020-06-01 09:48:49 +02:00
parent 48a15857fb
commit 2af75d1f13
5 changed files with 74 additions and 56 deletions

View File

@@ -27,6 +27,7 @@
//! ```
use std::default::Default;
use std::marker::PhantomData;
use std::os::raw::{c_char, c_int, c_void};
use std::rc::Rc;
@@ -72,9 +73,9 @@ struct ArrayTab {
base: ffi::sqlite3_vtab,
}
unsafe impl VTab for ArrayTab {
unsafe impl<'vtab> VTab<'vtab> for ArrayTab {
type Aux = ();
type Cursor = ArrayTabCursor;
type Cursor = ArrayTabCursor<'vtab>;
fn connect(
_: &mut VTabConnection,
@@ -118,28 +119,30 @@ unsafe impl VTab for ArrayTab {
Ok(())
}
fn open(&self) -> Result<ArrayTabCursor> {
fn open(&'vtab self) -> Result<ArrayTabCursor<'vtab>> {
Ok(ArrayTabCursor::new())
}
}
/// A cursor for the Array virtual table
#[repr(C)]
struct ArrayTabCursor {
struct ArrayTabCursor<'vtab> {
/// Base class. Must be first
base: ffi::sqlite3_vtab_cursor,
/// The rowid
row_id: i64,
/// Pointer to the array of values ("pointer")
ptr: Option<Array>,
phantom: PhantomData<&'vtab ArrayTab>,
}
impl ArrayTabCursor {
fn new() -> ArrayTabCursor {
impl ArrayTabCursor<'_> {
fn new<'vtab>() -> ArrayTabCursor<'vtab> {
ArrayTabCursor {
base: ffi::sqlite3_vtab_cursor::default(),
row_id: 0,
ptr: None,
phantom: PhantomData,
}
}
@@ -150,7 +153,7 @@ impl ArrayTabCursor {
}
}
}
unsafe impl VTabCursor for ArrayTabCursor {
unsafe impl VTabCursor for ArrayTabCursor<'_> {
fn filter(&mut self, idx_num: c_int, _idx_str: Option<&str>, args: &Values<'_>) -> Result<()> {
if idx_num > 0 {
self.ptr = args.get_array(0)?;