From 49a016b53d4a4e6342ac7f0974cd3fc812a28c66 Mon Sep 17 00:00:00 2001 From: mpostma Date: Tue, 22 Dec 2020 11:23:25 +0100 Subject: [PATCH] create update handler trait fix type inference error --- http-ui/src/main.rs | 3 ++- src/search/facet/facet_condition.rs | 1 + src/update_store.rs | 29 ++++++++++++++++++++--------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/http-ui/src/main.rs b/http-ui/src/main.rs index 80c65716d..5299c0e13 100644 --- a/http-ui/src/main.rs +++ b/http-ui/src/main.rs @@ -306,7 +306,8 @@ async fn main() -> anyhow::Result<()> { let update_store = UpdateStore::open( update_store_options, update_store_path, - move |update_id, meta, content| { + // the type hint is necessary: https://github.com/rust-lang/rust/issues/32600 + move |update_id, meta, content:&_| { // We prepare the update by using the update builder. let mut update_builder = UpdateBuilder::new(); if let Some(max_nb_chunks) = indexer_opt_cloned.max_nb_chunks { diff --git a/src/search/facet/facet_condition.rs b/src/search/facet/facet_condition.rs index 77e1de5ad..08e056549 100644 --- a/src/search/facet/facet_condition.rs +++ b/src/search/facet/facet_condition.rs @@ -61,6 +61,7 @@ impl FacetStringOperator { FacetStringOperator::Equal(s.to_lowercase()) } + #[allow(dead_code)] fn not_equal(s: &str) -> Self { FacetStringOperator::equal(s).negate() } diff --git a/src/update_store.rs b/src/update_store.rs index 9ffa2a7bc..7211a6293 100644 --- a/src/update_store.rs +++ b/src/update_store.rs @@ -18,15 +18,26 @@ pub struct UpdateStore { notification_sender: Sender<()>, } +pub trait UpdateHandler { + fn handle_update(&mut self, update_id: u64, meta: M, content: &[u8]) -> heed::Result; +} + +impl UpdateHandler for F +where F: FnMut(u64, M, &[u8]) -> heed::Result + Send + 'static { + fn handle_update(&mut self, update_id: u64, meta: M, content: &[u8]) -> heed::Result { + self(update_id, meta, content) + } +} + impl UpdateStore { - pub fn open( + pub fn open( mut options: EnvOpenOptions, path: P, - mut update_function: F, + mut update_handler: U, ) -> heed::Result>> where P: AsRef, - F: FnMut(u64, M, &[u8]) -> heed::Result + Send + 'static, + U: UpdateHandler + Send + 'static, M: for<'a> Deserialize<'a>, N: Serialize, { @@ -55,7 +66,7 @@ impl UpdateStore { // Block and wait for something to process. for () in notification_receiver { loop { - match update_store_cloned.process_pending_update(&mut update_function) { + match update_store_cloned.process_pending_update(&mut update_handler) { Ok(Some(_)) => (), Ok(None) => break, Err(e) => eprintln!("error while processing update: {}", e), @@ -125,9 +136,9 @@ impl UpdateStore { /// Executes the user provided function on the next pending update (the one with the lowest id). /// This is asynchronous as it let the user process the update with a read-only txn and /// only writing the result meta to the processed-meta store *after* it has been processed. - fn process_pending_update(&self, mut f: F) -> heed::Result> + fn process_pending_update(&self, handler: &mut U) -> heed::Result> where - F: FnMut(u64, M, &[u8]) -> heed::Result, + U: UpdateHandler, M: for<'a> Deserialize<'a>, N: Serialize, { @@ -144,7 +155,7 @@ impl UpdateStore { .expect("associated update content"); // Process the pending update using the provided user function. - let new_meta = (f)(first_id.get(), first_meta, first_content)?; + let new_meta = handler.handle_update(first_id.get(), first_meta, first_content)?; drop(rtxn); // Once the pending update have been successfully processed @@ -298,7 +309,7 @@ mod tests { fn simple() { let dir = tempfile::tempdir().unwrap(); let options = EnvOpenOptions::new(); - let update_store = UpdateStore::open(options, dir, |_id, meta: String, _content| { + let update_store = UpdateStore::open(options, dir, |_id, meta: String, _content:&_| { Ok(meta + " processed") }).unwrap(); @@ -316,7 +327,7 @@ mod tests { fn long_running_update() { let dir = tempfile::tempdir().unwrap(); let options = EnvOpenOptions::new(); - let update_store = UpdateStore::open(options, dir, |_id, meta: String, _content| { + let update_store = UpdateStore::open(options, dir, |_id, meta: String, _content:&_| { thread::sleep(Duration::from_millis(400)); Ok(meta + " processed") }).unwrap();