From e4ebb6d9e13e47b0878dc09dd40e4d6c77437773 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Tue, 4 Mar 2025 11:10:52 +0200 Subject: [PATCH] core/vdbe: Add ProgramState::get_vtab_cursor() helper --- core/vdbe/mod.rs | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/core/vdbe/mod.rs b/core/vdbe/mod.rs index 709129381..9cef73520 100644 --- a/core/vdbe/mod.rs +++ b/core/vdbe/mod.rs @@ -229,19 +229,6 @@ fn get_cursor_as_sorter_mut<'long, 'short>( cursor } -fn get_cursor_as_virtual_mut<'long, 'short>( - cursors: &'short mut RefMut<'long, Vec>>, - cursor_id: CursorID, -) -> &'short mut VTabOpaqueCursor { - let cursor = cursors - .get_mut(cursor_id) - .expect("cursor id out of bounds") - .as_mut() - .expect("cursor not allocated") - .as_virtual_mut(); - cursor -} - struct Bitfield([u64; N]); impl Bitfield { @@ -352,6 +339,17 @@ impl ProgramState { self.interrupted = false; self.parameters.clear(); } + + pub fn get_vtab_cursor<'a>(&'a self, cursor_id: CursorID) -> &'a mut VTabOpaqueCursor { + let mut cursors = self.cursors.borrow_mut(); + let cursor = cursors + .get_mut(cursor_id) + .expect("cursor id out of bounds") + .as_mut() + .expect("cursor not allocated") + .as_virtual_mut(); + unsafe { std::mem::transmute(cursor) } + } } macro_rules! must_be_btree_cursor { @@ -893,8 +891,7 @@ impl Program { let CursorType::VirtualTable(virtual_table) = cursor_type else { panic!("VFilter on non-virtual table cursor"); }; - let mut cursors = state.cursors.borrow_mut(); - let cursor = get_cursor_as_virtual_mut(&mut cursors, *cursor_id); + let cursor = state.get_vtab_cursor(*cursor_id); let mut args = Vec::new(); for i in 0..*arg_count { args.push(state.registers[args_reg + i].clone()); @@ -915,8 +912,7 @@ impl Program { let CursorType::VirtualTable(virtual_table) = cursor_type else { panic!("VColumn on non-virtual table cursor"); }; - let mut cursors = state.cursors.borrow_mut(); - let cursor = get_cursor_as_virtual_mut(&mut cursors, *cursor_id); + let cursor = state.get_vtab_cursor(*cursor_id); state.registers[*dest] = virtual_table.column(cursor, *column)?; state.pc += 1; } @@ -981,8 +977,7 @@ impl Program { let CursorType::VirtualTable(virtual_table) = cursor_type else { panic!("VNextAsync on non-virtual table cursor"); }; - let mut cursors = state.cursors.borrow_mut(); - let cursor = get_cursor_as_virtual_mut(&mut cursors, *cursor_id); + let cursor = state.get_vtab_cursor(*cursor_id); let has_more = virtual_table.next(cursor)?; if has_more { state.pc = pc_if_next.to_offset_int();