From 5ed104348817faf899309aba79473d751febbbbe Mon Sep 17 00:00:00 2001 From: Victor Farazdagi Date: Thu, 19 Jan 2023 19:09:24 +0300 Subject: [PATCH] Allow to pass in owned variable for Decision::Change variant in compaction filter (#725) --- src/compaction_filter.rs | 7 ++++--- tests/test_compationfilter.rs | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/compaction_filter.rs b/src/compaction_filter.rs index fc52068..cd2a7d2 100644 --- a/src/compaction_filter.rs +++ b/src/compaction_filter.rs @@ -28,7 +28,7 @@ pub enum Decision { /// Remove the object from the database Remove, /// Change the value for the key - Change(&'static [u8]), + Change(Vec), } /// CompactionFilter allows an application to modify/delete a key-value at @@ -73,6 +73,7 @@ pub trait CompactionFilter { /// /// [set_compaction_filter]: ../struct.Options.html#method.set_compaction_filter pub trait CompactionFilterFn: FnMut(u32, &[u8], &[u8]) -> Decision {} + impl CompactionFilterFn for F where F: FnMut(u32, &[u8], &[u8]) -> Decision + Send + 'static {} pub struct CompactionFilterCallback @@ -135,8 +136,8 @@ where Keep => 0, Remove => 1, Change(newval) => { - *new_value = newval.as_ptr() as *mut c_char; *new_value_length = newval.len() as size_t; + *new_value = Box::into_raw(newval.into_boxed_slice()) as *mut c_char; *value_changed = 1_u8; 0 } @@ -149,7 +150,7 @@ fn test_filter(level: u32, key: &[u8], value: &[u8]) -> Decision { use self::Decision::{Change, Keep, Remove}; match key.first() { Some(&b'_') => Remove, - Some(&b'%') => Change(b"secret"), + Some(&b'%') => Change(b"secret".to_vec()), _ => Keep, } } diff --git a/tests/test_compationfilter.rs b/tests/test_compationfilter.rs index f5e206d..3562366 100644 --- a/tests/test_compationfilter.rs +++ b/tests/test_compationfilter.rs @@ -25,7 +25,7 @@ fn test_filter(level: u32, key: &[u8], value: &[u8]) -> CompactionDecision { use self::CompactionDecision::*; match key.first() { Some(&b'_') => Remove, - Some(&b'%') => Change(b"secret"), + Some(&b'%') => Change(b"secret".to_vec()), _ => Keep, } }