Make VTab / VTabCursor unsafe trait as implmenting them on the wrong type is unsound

This commit is contained in:
Thom Chiovoloni 2020-04-14 07:39:41 -07:00 committed by Thom Chiovoloni
parent 3c6b57fe1b
commit c9ef5bd63c
4 changed files with 13 additions and 9 deletions

View File

@ -71,7 +71,7 @@ struct ArrayTab {
base: ffi::sqlite3_vtab, base: ffi::sqlite3_vtab,
} }
impl VTab for ArrayTab { unsafe impl VTab for ArrayTab {
type Aux = (); type Aux = ();
type Cursor = ArrayTabCursor; type Cursor = ArrayTabCursor;
@ -149,7 +149,7 @@ impl ArrayTabCursor {
} }
} }
} }
impl VTabCursor for ArrayTabCursor { unsafe impl VTabCursor for ArrayTabCursor {
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<()> {
if idx_num > 0 { if idx_num > 0 {
self.ptr = args.get_array(0)?; self.ptr = args.get_array(0)?;

View File

@ -95,7 +95,7 @@ impl CSVTab {
} }
} }
impl VTab for CSVTab { unsafe impl VTab for CSVTab {
type Aux = (); type Aux = ();
type Cursor = CSVTabCursor; type Cursor = CSVTabCursor;
@ -296,7 +296,7 @@ impl CSVTabCursor {
} }
} }
impl VTabCursor for CSVTabCursor { unsafe impl VTabCursor for CSVTabCursor {
// Only a full table scan is supported. So `filter` simply rewinds to // Only a full table scan is supported. So `filter` simply rewinds to
// the beginning. // the beginning.
fn filter( fn filter(

View File

@ -189,7 +189,11 @@ impl VTabConnection {
/// `feature = "vtab"` Virtual table instance trait. /// `feature = "vtab"` Virtual table instance trait.
/// ///
/// Implementations must be like: /// # Safety
///
/// The first item in a struct implementing VTab must be
/// `rusqlite::sqlite3_vtab`, and the struct must be `#[repr(C)]`.
///
/// ```rust,ignore /// ```rust,ignore
/// #[repr(C)] /// #[repr(C)]
/// struct MyTab { /// struct MyTab {
@ -200,7 +204,7 @@ impl VTabConnection {
/// ``` /// ```
/// ///
/// (See [SQLite doc](https://sqlite.org/c3ref/vtab.html)) /// (See [SQLite doc](https://sqlite.org/c3ref/vtab.html))
pub trait VTab: Sized { pub unsafe trait VTab: Sized {
type Aux; type Aux;
type Cursor: VTabCursor; type Cursor: VTabCursor;
@ -465,7 +469,7 @@ impl OrderBy<'_> {
/// ``` /// ```
/// ///
/// (See [SQLite doc](https://sqlite.org/c3ref/vtab_cursor.html)) /// (See [SQLite doc](https://sqlite.org/c3ref/vtab_cursor.html))
pub trait VTabCursor: Sized { pub unsafe trait VTabCursor: Sized {
/// Begin a search of a virtual table. /// Begin a search of a virtual table.
/// (See [SQLite doc](https://sqlite.org/vtab.html#the_xfilter_method)) /// (See [SQLite doc](https://sqlite.org/vtab.html#the_xfilter_method))
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<()>;

View File

@ -49,7 +49,7 @@ struct SeriesTab {
base: ffi::sqlite3_vtab, base: ffi::sqlite3_vtab,
} }
impl VTab for SeriesTab { unsafe impl VTab for SeriesTab {
type Aux = (); type Aux = ();
type Cursor = SeriesTabCursor; type Cursor = SeriesTabCursor;
@ -181,7 +181,7 @@ impl SeriesTabCursor {
SeriesTabCursor::default() SeriesTabCursor::default()
} }
} }
impl VTabCursor for SeriesTabCursor { unsafe impl VTabCursor for SeriesTabCursor {
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<()> {
let idx_num = QueryPlanFlags::from_bits_truncate(idx_num); let idx_num = QueryPlanFlags::from_bits_truncate(idx_num);
let mut i = 0; let mut i = 0;