From 926977846fd0c4ec8e77fcaa9a6c39e9021a4663 Mon Sep 17 00:00:00 2001 From: gwenn Date: Wed, 28 Oct 2020 19:20:05 +0100 Subject: [PATCH] Reduce required lifetime Extends #825 to - create_collation - commit_hook - rollback_hook - update_hook - table_filter --- src/collation.rs | 8 ++++---- src/functions.rs | 12 ++++++------ src/hooks.rs | 48 +++++++++++++++++++++--------------------------- src/session.rs | 4 ++-- 4 files changed, 33 insertions(+), 39 deletions(-) diff --git a/src/collation.rs b/src/collation.rs index 1168b75..d88d662 100644 --- a/src/collation.rs +++ b/src/collation.rs @@ -15,9 +15,9 @@ unsafe extern "C" fn free_boxed_value(p: *mut c_void) { impl Connection { /// `feature = "collation"` Add or modify a collation. - pub fn create_collation(&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(&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( arg1: *mut c_void, diff --git a/src/functions.rs b/src/functions.rs index 3531391..80b51eb 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -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( - &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 + Send + UnwindSafe + 'static, + F: FnMut(&Context<'_>) -> Result + Send + UnwindSafe + 'c, T: ToSql, { self.db @@ -411,15 +411,15 @@ impl Connection { } impl InnerConnection { - fn create_scalar_function( - &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 + Send + UnwindSafe + 'static, + F: FnMut(&Context<'_>) -> Result + Send + UnwindSafe + 'c, T: ToSql, { unsafe extern "C" fn call_boxed_closure( diff --git a/src/hooks.rs b/src/hooks.rs index d9b2ac4..9a94578 100644 --- a/src/hooks.rs +++ b/src/hooks.rs @@ -40,9 +40,9 @@ impl Connection { /// a transaction is committed. /// /// The callback returns `true` to rollback. - pub fn commit_hook(&self, hook: Option) + pub fn commit_hook<'c, F>(&'c self, hook: Option) 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(&self, hook: Option) + pub fn rollback_hook<'c, F>(&'c self, hook: Option) 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(&self, hook: Option) + pub fn update_hook<'c, F>(&'c self, hook: Option) 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:: bool>); } - fn commit_hook(&mut self, hook: Option) + fn commit_hook<'c, F>(&'c mut self, hook: Option) where - F: FnMut() -> bool + Send + 'static, + F: FnMut() -> bool + Send + 'c, { unsafe extern "C" fn call_boxed_closure(p_arg: *mut c_void) -> c_int where @@ -146,9 +146,9 @@ impl InnerConnection { self.free_commit_hook = free_commit_hook; } - fn rollback_hook(&mut self, hook: Option) + fn rollback_hook<'c, F>(&'c mut self, hook: Option) where - F: FnMut() + Send + 'static, + F: FnMut() + Send + 'c, { unsafe extern "C" fn call_boxed_closure(p_arg: *mut c_void) where @@ -187,9 +187,9 @@ impl InnerConnection { self.free_rollback_hook = free_rollback_hook; } - fn update_hook(&mut self, hook: Option) + fn update_hook<'c, F>(&'c mut self, hook: Option) where - F: FnMut(Action, &str, &str, i64) + Send + 'static, + F: FnMut(Action, &str, &str, i64) + Send + 'c, { unsafe extern "C" fn call_boxed_closure( 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] diff --git a/src/session.rs b/src/session.rs index 97ae3a5..2dc6235 100644 --- a/src/session.rs +++ b/src/session.rs @@ -53,9 +53,9 @@ impl Session<'_> { } /// Set a table filter - pub fn table_filter(&mut self, filter: Option) + pub fn table_filter<'s, F>(&'s mut self, filter: Option) where - F: Fn(&str) -> bool + Send + RefUnwindSafe + 'static, + F: Fn(&str) -> bool + Send + RefUnwindSafe + 's, { unsafe extern "C" fn call_boxed_closure( p_arg: *mut c_void,