diff --git a/tools/kv_typed/src/backend.rs b/tools/kv_typed/src/backend.rs index 5e91cc21d5e569bf2314c533f25e4bc56861b7b5..a1f0f7d1642233b609f6d07267b4705d2ba163fb 100644 --- a/tools/kv_typed/src/backend.rs +++ b/tools/kv_typed/src/backend.rs @@ -49,7 +49,7 @@ pub trait BackendCol: 'static + Clone + Debug + Send + Sync { k: &K, f: F, ) -> KvResult<Option<D>>; - fn get_ref_slice<K: Key, V: ValueSliceZc, D, F: Fn(&[V::Elem]) -> KvResult<D>>( + fn get_ref_slice<K: Key, V: ValueSliceZc, D, F: Fn(&[u8]) -> KvResult<D>>( &self, k: &K, f: F, diff --git a/tools/kv_typed/src/backend/leveldb.rs b/tools/kv_typed/src/backend/leveldb.rs index 22692c7ad8f2472d42b3a30d40ef5a917e92c073..e2f4a45262980f1f77925818e58c506e74049be0 100644 --- a/tools/kv_typed/src/backend/leveldb.rs +++ b/tools/kv_typed/src/backend/leveldb.rs @@ -162,7 +162,7 @@ impl BackendCol for LevelDbCol { }) } #[inline(always)] - fn get_ref_slice<K: Key, V: ValueSliceZc, D, F: Fn(&[V::Elem]) -> KvResult<D>>( + fn get_ref_slice<K: Key, V: ValueSliceZc, D, F: Fn(&[u8]) -> KvResult<D>>( &self, k: &K, f: F, @@ -170,21 +170,7 @@ impl BackendCol for LevelDbCol { k.as_bytes(|k_bytes| { self.0 .get(ReadOptions::new(), k_bytes)? - .map(|bytes| { - if bytes.is_empty() { - f(&[]) - } else if let Some(layout_verified) = - zerocopy::LayoutVerified::<_, [V::Elem]>::new_slice( - &bytes[V::prefix_len()..], - ) - { - f(&layout_verified) - } else { - Err(KvError::DeserError( - "Bytes are invalid length or alignment.".into(), - )) - } - }) + .map(|bytes| f(&bytes)) .transpose() }) } diff --git a/tools/kv_typed/src/backend/lmdb.rs b/tools/kv_typed/src/backend/lmdb.rs index e30532868e2aadad117e5404438c5aa1f660d444..bf329ffbeeb5ff6ad0a21e8437eb629062412ade 100644 --- a/tools/kv_typed/src/backend/lmdb.rs +++ b/tools/kv_typed/src/backend/lmdb.rs @@ -312,7 +312,7 @@ impl BackendCol for LmdbCol { }) } - fn get_ref_slice<K: Key, V: ValueSliceZc, D, F: Fn(&[V::Elem]) -> KvResult<D>>( + fn get_ref_slice<K: Key, V: ValueSliceZc, D, F: Fn(&[u8]) -> KvResult<D>>( &self, k: &K, f: F, @@ -323,21 +323,7 @@ impl BackendCol for LmdbCol { access .get::<_, [u8]>(&self.inner.tree, k_bytes) .to_opt()? - .map(|bytes| { - if bytes.is_empty() { - f(&[]) - } else if let Some(layout_verified) = - zerocopy::LayoutVerified::<_, [V::Elem]>::new_slice( - &bytes[V::prefix_len()..], - ) - { - f(&layout_verified) - } else { - Err(KvError::DeserError( - "Bytes are invalid length or alignment.".into(), - )) - } - }) + .map(|bytes| f(bytes)) .transpose() }) } diff --git a/tools/kv_typed/src/backend/memory.rs b/tools/kv_typed/src/backend/memory.rs index 0d50e7857043483c19abb07bbfc1f9a96d272adc..ed325389b2b938601f3eb2f7f0218d2055696965 100644 --- a/tools/kv_typed/src/backend/memory.rs +++ b/tools/kv_typed/src/backend/memory.rs @@ -129,31 +129,12 @@ impl BackendCol for MemCol { }) } #[inline(always)] - fn get_ref_slice<K: Key, V: ValueSliceZc, D, F: Fn(&[V::Elem]) -> KvResult<D>>( + fn get_ref_slice<K: Key, V: ValueSliceZc, D, F: Fn(&[u8]) -> KvResult<D>>( &self, k: &K, f: F, ) -> KvResult<Option<D>> { - k.as_bytes(|k_bytes| { - self.tree - .get(k_bytes) - .map(|bytes| { - if bytes.is_empty() { - f(&[]) - } else if let Some(layout_verified) = - zerocopy::LayoutVerified::<_, [V::Elem]>::new_slice( - &bytes[V::prefix_len()..], - ) - { - f(&layout_verified) - } else { - Err(KvError::DeserError( - "Bytes are invalid length or alignment.".into(), - )) - } - }) - .transpose() - }) + k.as_bytes(|k_bytes| self.tree.get(k_bytes).map(|bytes| f(bytes)).transpose()) } #[inline(always)] fn delete<K: Key>(&mut self, k: &K) -> KvResult<()> { diff --git a/tools/kv_typed/src/backend/memory_singleton.rs b/tools/kv_typed/src/backend/memory_singleton.rs index 786ca4e69c002ce0c8e0410e8ac55921d2c11513..8a9afc56e01c269b44901ff1c192f7f1b27371c6 100644 --- a/tools/kv_typed/src/backend/memory_singleton.rs +++ b/tools/kv_typed/src/backend/memory_singleton.rs @@ -113,27 +113,12 @@ impl BackendCol for MemCol { .transpose() } #[inline(always)] - fn get_ref_slice<K: Key, V: ValueSliceZc, D, F: Fn(&[V::Elem]) -> KvResult<D>>( + fn get_ref_slice<K: Key, V: ValueSliceZc, D, F: Fn(&[u8]) -> KvResult<D>>( &self, _k: &K, f: F, ) -> KvResult<Option<D>> { - self.0 - .as_ref() - .map(|bytes| { - if bytes.is_empty() { - f(&[]) - } else if let Some(layout_verified) = - zerocopy::LayoutVerified::<_, [V::Elem]>::new_slice(&bytes[V::prefix_len()..]) - { - f(&layout_verified) - } else { - Err(KvError::DeserError( - "Bytes are invalid length or alignment.".into(), - )) - } - }) - .transpose() + self.0.as_ref().map(|bytes| f(bytes)).transpose() } #[inline(always)] fn delete<K: Key>(&mut self, _k: &K) -> KvResult<()> { diff --git a/tools/kv_typed/src/backend/sled.rs b/tools/kv_typed/src/backend/sled.rs index 9d1358026612b72ada811c057520d3a8dfba1c7f..43bab2a7e127f603867ade4a8e965d813c8d508b 100644 --- a/tools/kv_typed/src/backend/sled.rs +++ b/tools/kv_typed/src/backend/sled.rs @@ -112,31 +112,12 @@ impl BackendCol for SledCol { }) } #[inline(always)] - fn get_ref_slice<K: Key, V: ValueSliceZc, D, F: Fn(&[V::Elem]) -> KvResult<D>>( + fn get_ref_slice<K: Key, V: ValueSliceZc, D, F: Fn(&[u8]) -> KvResult<D>>( &self, k: &K, f: F, ) -> KvResult<Option<D>> { - k.as_bytes(|k_bytes| { - self.0 - .get(k_bytes)? - .map(|bytes| { - if bytes.is_empty() { - f(&[]) - } else if let Some(layout_verified) = - zerocopy::LayoutVerified::<_, [V::Elem]>::new_slice( - &bytes[V::prefix_len()..], - ) - { - f(&layout_verified) - } else { - Err(KvError::DeserError( - "Bytes are invalid length or alignment.".into(), - )) - } - }) - .transpose() - }) + k.as_bytes(|k_bytes| self.0.get(k_bytes)?.map(|bytes| f(&bytes)).transpose()) } #[inline(always)] fn delete<K: Key>(&mut self, k: &K) -> KvResult<()> { diff --git a/tools/kv_typed/src/collection_ro.rs b/tools/kv_typed/src/collection_ro.rs index 1ab5360100f86ddbb25da8e20c57ba886c42f735..3a1aa0c3e740d8e7a6323d7a4a1dc53ef5abe53a 100644 --- a/tools/kv_typed/src/collection_ro.rs +++ b/tools/kv_typed/src/collection_ro.rs @@ -192,7 +192,19 @@ impl<V: ValueSliceZc, BC: BackendCol, E: EventTrait<V = V>> DbCollectionRoGetRef f: F, ) -> KvResult<Option<D>> { let r = self.inner.read(); - r.backend_col.get_ref_slice::<E::K, V, D, F>(k, f) + r.backend_col.get_ref_slice::<E::K, V, D, _>(k, |bytes| { + if bytes.is_empty() { + f(&[]) + } else if let Some(layout_verified) = + zerocopy::LayoutVerified::<_, [V::Elem]>::new_slice(&bytes[V::prefix_len()..]) + { + f(&layout_verified) + } else { + Err(KvError::DeserError( + "Bytes are invalid length or alignment.".into(), + )) + } + }) } } diff --git a/tools/kv_typed/src/transactional_read.rs b/tools/kv_typed/src/transactional_read.rs index 647861f9b3c8d7c6518e3063e8fdcaf4639b70f8..75df9be30dbbc97be405298fb2e039604249bd57 100644 --- a/tools/kv_typed/src/transactional_read.rs +++ b/tools/kv_typed/src/transactional_read.rs @@ -137,7 +137,19 @@ impl<'tx, V: ValueSliceZc, BC: BackendCol, E: EventTrait<V = V>> TxColRo<'tx, BC ) -> KvResult<Option<D>> { self.col_reader .backend_col - .get_ref_slice::<E::K, V, D, F>(k, f) + .get_ref_slice::<E::K, V, D, _>(k, |bytes| { + if bytes.is_empty() { + f(&[]) + } else if let Some(layout_verified) = + zerocopy::LayoutVerified::<_, [V::Elem]>::new_slice(&bytes[V::prefix_len()..]) + { + f(&layout_verified) + } else { + Err(KvError::DeserError( + "Bytes are invalid length or alignment.".into(), + )) + } + }) } } diff --git a/tools/kv_typed/src/transactional_write.rs b/tools/kv_typed/src/transactional_write.rs index 2595e630250a999f61ca548bda105b63741b3513..54d109d34cd45627cdc4e4e9de63618fb8a29ed1 100644 --- a/tools/kv_typed/src/transactional_write.rs +++ b/tools/kv_typed/src/transactional_write.rs @@ -61,7 +61,19 @@ impl<'tx, V: ValueSliceZc, BC: BackendCol, E: EventTrait<V = V>> TxColRw<'tx, BC ) -> KvResult<Option<D>> { self.col_reader .backend_col - .get_ref_slice::<E::K, V, D, F>(k, f) + .get_ref_slice::<E::K, V, D, _>(k, |bytes| { + if bytes.is_empty() { + f(&[]) + } else if let Some(layout_verified) = + zerocopy::LayoutVerified::<_, [V::Elem]>::new_slice(&bytes[V::prefix_len()..]) + { + f(&layout_verified) + } else { + Err(KvError::DeserError( + "Bytes are invalid length or alignment.".into(), + )) + } + }) } }