From d17b371c4d4b98898d287c20e52cc79b9fbc7f74 Mon Sep 17 00:00:00 2001 From: Ivan Ukhov Date: Fri, 29 May 2015 08:58:19 -0400 Subject: [PATCH] Make Database::execute accept None --- src/lib.rs | 10 +++++----- tests/lib.rs | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3575774..ec73299 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -98,7 +98,7 @@ pub struct Database<'d> { _phantom: PhantomData<&'d raw::sqlite3>, } -struct ExecuteCallback<'d>(Box) -> bool + 'd>); +pub type ExecuteCallback<'c> = FnMut(Vec<(String, String)>) -> bool + 'c; impl<'d> Database<'d> { /// Open a database. @@ -111,13 +111,13 @@ impl<'d> Database<'d> { } /// Execute an SQL statement. - pub fn execute(&mut self, sql: &str, callback: Option) -> Result<()> - where F: FnMut(Vec<(String, String)>) -> bool { + pub fn execute<'c>(&mut self, sql: &str, + callback: Option<&mut ExecuteCallback<'c>>) -> Result<()> { unsafe { match callback { Some(callback) => { - let mut callback = ExecuteCallback(Box::new(callback)); + let mut callback = Box::new(callback); success!(raw::sqlite3_exec(self.db, str_to_c_str!(sql), Some(execute_callback), &mut callback as *mut _ as *mut _, 0 as *mut _)); }, @@ -166,7 +166,7 @@ extern fn execute_callback(callback: *mut c_void, count: c_int, values: *mut *mu pairs.push((column, value)); } - let ExecuteCallback(ref mut callback) = *(callback as *mut _); + let ref mut callback = *(callback as *mut Box<&mut ExecuteCallback>); if callback(pairs) { 0 } else { 1 } } } diff --git a/tests/lib.rs b/tests/lib.rs index 18755f4..9d8ddc9 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -18,14 +18,14 @@ fn execute() { let mut database = ok!(sqlite::open(&path)); let sql = r#"CREATE TABLE `users` (id INTEGER, name VARCHAR(255), age REAL);"#; - ok!(database.execute(sql, Some(|_| -> bool { true }))); + ok!(database.execute(sql, None)); let sql = r#"INSERT INTO `users` (id, name, age) VALUES (1, "Alice", 20.99);"#; - ok!(database.execute(sql, Some(|_| -> bool { true }))); + ok!(database.execute(sql, None)); let mut done = false; let sql = r#"SELECT * FROM `users`;"#; - ok!(database.execute(sql, Some(|pairs: Vec<(String, String)>| -> bool { + ok!(database.execute(sql, Some(&mut |pairs: Vec<(String, String)>| -> bool { assert!(pairs.len() == 3); assert!(pairs[0] == pair!("id", "1")); assert!(pairs[1] == pair!("name", "Alice"));