mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-23 00:39:20 +08:00
Reduce required lifetime
Extends #825 to - create_collation - commit_hook - rollback_hook - update_hook - table_filter
This commit is contained in:
parent
9c954b8cb5
commit
926977846f
@ -15,9 +15,9 @@ unsafe extern "C" fn free_boxed_value<T>(p: *mut c_void) {
|
||||
|
||||
impl Connection {
|
||||
/// `feature = "collation"` Add or modify a collation.
|
||||
pub fn create_collation<C>(&self, collation_name: &str, x_compare: C) -> Result<()>
|
||||
pub fn create_collation<'c, C>(&'c self, collation_name: &str, x_compare: C) -> Result<()>
|
||||
where
|
||||
C: Fn(&str, &str) -> Ordering + Send + UnwindSafe + 'static,
|
||||
C: Fn(&str, &str) -> Ordering + Send + UnwindSafe + 'c,
|
||||
{
|
||||
self.db
|
||||
.borrow_mut()
|
||||
@ -39,9 +39,9 @@ impl Connection {
|
||||
}
|
||||
|
||||
impl InnerConnection {
|
||||
fn create_collation<C>(&mut self, collation_name: &str, x_compare: C) -> Result<()>
|
||||
fn create_collation<'c, C>(&'c mut self, collation_name: &str, x_compare: C) -> Result<()>
|
||||
where
|
||||
C: Fn(&str, &str) -> Ordering + Send + UnwindSafe + 'static,
|
||||
C: Fn(&str, &str) -> Ordering + Send + UnwindSafe + 'c,
|
||||
{
|
||||
unsafe extern "C" fn call_boxed_closure<C>(
|
||||
arg1: *mut c_void,
|
||||
|
@ -334,15 +334,15 @@ impl Connection {
|
||||
/// # Failure
|
||||
///
|
||||
/// Will return Err if the function could not be attached to the connection.
|
||||
pub fn create_scalar_function<F, T>(
|
||||
&self,
|
||||
pub fn create_scalar_function<'c, F, T>(
|
||||
&'c self,
|
||||
fn_name: &str,
|
||||
n_arg: c_int,
|
||||
flags: FunctionFlags,
|
||||
x_func: F,
|
||||
) -> Result<()>
|
||||
where
|
||||
F: FnMut(&Context<'_>) -> Result<T> + Send + UnwindSafe + 'static,
|
||||
F: FnMut(&Context<'_>) -> Result<T> + Send + UnwindSafe + 'c,
|
||||
T: ToSql,
|
||||
{
|
||||
self.db
|
||||
@ -411,15 +411,15 @@ impl Connection {
|
||||
}
|
||||
|
||||
impl InnerConnection {
|
||||
fn create_scalar_function<F, T>(
|
||||
&mut self,
|
||||
fn create_scalar_function<'c, F, T>(
|
||||
&'c mut self,
|
||||
fn_name: &str,
|
||||
n_arg: c_int,
|
||||
flags: FunctionFlags,
|
||||
x_func: F,
|
||||
) -> Result<()>
|
||||
where
|
||||
F: FnMut(&Context<'_>) -> Result<T> + Send + UnwindSafe + 'static,
|
||||
F: FnMut(&Context<'_>) -> Result<T> + Send + UnwindSafe + 'c,
|
||||
T: ToSql,
|
||||
{
|
||||
unsafe extern "C" fn call_boxed_closure<F, T>(
|
||||
|
48
src/hooks.rs
48
src/hooks.rs
@ -40,9 +40,9 @@ impl Connection {
|
||||
/// a transaction is committed.
|
||||
///
|
||||
/// The callback returns `true` to rollback.
|
||||
pub fn commit_hook<F>(&self, hook: Option<F>)
|
||||
pub fn commit_hook<'c, F>(&'c self, hook: Option<F>)
|
||||
where
|
||||
F: FnMut() -> bool + Send + 'static,
|
||||
F: FnMut() -> bool + Send + 'c,
|
||||
{
|
||||
self.db.borrow_mut().commit_hook(hook);
|
||||
}
|
||||
@ -51,9 +51,9 @@ impl Connection {
|
||||
/// a transaction is committed.
|
||||
///
|
||||
/// The callback returns `true` to rollback.
|
||||
pub fn rollback_hook<F>(&self, hook: Option<F>)
|
||||
pub fn rollback_hook<'c, F>(&'c self, hook: Option<F>)
|
||||
where
|
||||
F: FnMut() + Send + 'static,
|
||||
F: FnMut() + Send + 'c,
|
||||
{
|
||||
self.db.borrow_mut().rollback_hook(hook);
|
||||
}
|
||||
@ -68,9 +68,9 @@ impl Connection {
|
||||
/// - the name of the database ("main", "temp", ...),
|
||||
/// - the name of the table that is updated,
|
||||
/// - the ROWID of the row that is updated.
|
||||
pub fn update_hook<F>(&self, hook: Option<F>)
|
||||
pub fn update_hook<'c, F>(&'c self, hook: Option<F>)
|
||||
where
|
||||
F: FnMut(Action, &str, &str, i64) + Send + 'static,
|
||||
F: FnMut(Action, &str, &str, i64) + Send + 'c,
|
||||
{
|
||||
self.db.borrow_mut().update_hook(hook);
|
||||
}
|
||||
@ -97,9 +97,9 @@ impl InnerConnection {
|
||||
self.progress_handler(0, None::<fn() -> bool>);
|
||||
}
|
||||
|
||||
fn commit_hook<F>(&mut self, hook: Option<F>)
|
||||
fn commit_hook<'c, F>(&'c mut self, hook: Option<F>)
|
||||
where
|
||||
F: FnMut() -> bool + Send + 'static,
|
||||
F: FnMut() -> bool + Send + 'c,
|
||||
{
|
||||
unsafe extern "C" fn call_boxed_closure<F>(p_arg: *mut c_void) -> c_int
|
||||
where
|
||||
@ -146,9 +146,9 @@ impl InnerConnection {
|
||||
self.free_commit_hook = free_commit_hook;
|
||||
}
|
||||
|
||||
fn rollback_hook<F>(&mut self, hook: Option<F>)
|
||||
fn rollback_hook<'c, F>(&'c mut self, hook: Option<F>)
|
||||
where
|
||||
F: FnMut() + Send + 'static,
|
||||
F: FnMut() + Send + 'c,
|
||||
{
|
||||
unsafe extern "C" fn call_boxed_closure<F>(p_arg: *mut c_void)
|
||||
where
|
||||
@ -187,9 +187,9 @@ impl InnerConnection {
|
||||
self.free_rollback_hook = free_rollback_hook;
|
||||
}
|
||||
|
||||
fn update_hook<F>(&mut self, hook: Option<F>)
|
||||
fn update_hook<'c, F>(&'c mut self, hook: Option<F>)
|
||||
where
|
||||
F: FnMut(Action, &str, &str, i64) + Send + 'static,
|
||||
F: FnMut(Action, &str, &str, i64) + Send + 'c,
|
||||
{
|
||||
unsafe extern "C" fn call_boxed_closure<F>(
|
||||
p_arg: *mut c_void,
|
||||
@ -306,16 +306,14 @@ mod test {
|
||||
fn test_commit_hook() {
|
||||
let db = Connection::open_in_memory().unwrap();
|
||||
|
||||
lazy_static! {
|
||||
static ref CALLED: AtomicBool = AtomicBool::new(false);
|
||||
}
|
||||
let mut called = false;
|
||||
db.commit_hook(Some(|| {
|
||||
CALLED.store(true, Ordering::Relaxed);
|
||||
called = true;
|
||||
false
|
||||
}));
|
||||
db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); COMMIT;")
|
||||
.unwrap();
|
||||
assert!(CALLED.load(Ordering::Relaxed));
|
||||
assert!(called);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -335,34 +333,30 @@ mod test {
|
||||
fn test_rollback_hook() {
|
||||
let db = Connection::open_in_memory().unwrap();
|
||||
|
||||
lazy_static! {
|
||||
static ref CALLED: AtomicBool = AtomicBool::new(false);
|
||||
}
|
||||
let mut called = false;
|
||||
db.rollback_hook(Some(|| {
|
||||
CALLED.store(true, Ordering::Relaxed);
|
||||
called = true;
|
||||
}));
|
||||
db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); ROLLBACK;")
|
||||
.unwrap();
|
||||
assert!(CALLED.load(Ordering::Relaxed));
|
||||
assert!(called);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_update_hook() {
|
||||
let db = Connection::open_in_memory().unwrap();
|
||||
|
||||
lazy_static! {
|
||||
static ref CALLED: AtomicBool = AtomicBool::new(false);
|
||||
}
|
||||
let mut called = false;
|
||||
db.update_hook(Some(|action, db: &str, tbl: &str, row_id| {
|
||||
assert_eq!(Action::SQLITE_INSERT, action);
|
||||
assert_eq!("main", db);
|
||||
assert_eq!("foo", tbl);
|
||||
assert_eq!(1, row_id);
|
||||
CALLED.store(true, Ordering::Relaxed);
|
||||
called = true;
|
||||
}));
|
||||
db.execute_batch("CREATE TABLE foo (t TEXT)").unwrap();
|
||||
db.execute_batch("INSERT INTO foo VALUES ('lisa')").unwrap();
|
||||
assert!(CALLED.load(Ordering::Relaxed));
|
||||
assert!(called);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -53,9 +53,9 @@ impl Session<'_> {
|
||||
}
|
||||
|
||||
/// Set a table filter
|
||||
pub fn table_filter<F>(&mut self, filter: Option<F>)
|
||||
pub fn table_filter<'s, F>(&'s mut self, filter: Option<F>)
|
||||
where
|
||||
F: Fn(&str) -> bool + Send + RefUnwindSafe + 'static,
|
||||
F: Fn(&str) -> bool + Send + RefUnwindSafe + 's,
|
||||
{
|
||||
unsafe extern "C" fn call_boxed_closure<F>(
|
||||
p_arg: *mut c_void,
|
||||
|
Loading…
Reference in New Issue
Block a user