Merge pull request #867 from phiresky/function-context-db-handle

This commit is contained in:
Thom Chiovoloni 2020-12-18 20:50:52 -08:00 committed by GitHub
commit 4657ddc8e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -53,6 +53,8 @@
//! } //! }
//! ``` //! ```
use std::any::Any; use std::any::Any;
use std::marker::PhantomData;
use std::ops::Deref;
use std::os::raw::{c_int, c_void}; use std::os::raw::{c_int, c_void};
use std::panic::{catch_unwind, RefUnwindSafe, UnwindSafe}; use std::panic::{catch_unwind, RefUnwindSafe, UnwindSafe};
use std::ptr; use std::ptr;
@ -220,6 +222,37 @@ impl Context<'_> {
.map_err(|_| Error::GetAuxWrongType) .map_err(|_| Error::GetAuxWrongType)
} }
} }
/// Get the db connection handle via [sqlite3_context_db_handle](https://www.sqlite.org/c3ref/context_db_handle.html)
///
/// # Safety
///
/// This function is marked unsafe because there is a potential for other
/// references to the connection to be sent across threads, [see this comment](https://github.com/rusqlite/rusqlite/issues/643#issuecomment-640181213).
pub unsafe fn get_connection(&self) -> Result<ConnectionRef<'_>> {
let handle = ffi::sqlite3_context_db_handle(self.ctx);
Ok(ConnectionRef {
conn: Connection::from_handle(handle)?,
phantom: PhantomData,
})
}
}
/// A reference to a connection handle with a lifetime bound to something.
pub struct ConnectionRef<'ctx> {
// comes from Connection::from_handle(sqlite3_context_db_handle(...))
// and is non-owning
conn: Connection,
phantom: PhantomData<&'ctx Context<'ctx>>,
}
impl Deref for ConnectionRef<'_> {
type Target = Connection;
#[inline]
fn deref(&self) -> &Connection {
&self.conn
}
} }
type AuxInner = Arc<dyn Any + Send + Sync + 'static>; type AuxInner = Arc<dyn Any + Send + Sync + 'static>;