From 336260696db14043a4e376db63b45e09e59dd826 Mon Sep 17 00:00:00 2001 From: Jay Oster Date: Mon, 29 Apr 2024 00:52:35 -0500 Subject: [PATCH] Remove unnecessary `unsafe` in skipdb-core (#14) - Adds a test that proves `Values: Send` without the unsafe trait impl. - Adds an extra constraint to functions that require `Values: Send`. - Adds `#![forbid(unsafe_code)]` to skipdb-core. --- async-skipdb/src/optimistic.rs | 2 ++ async-skipdb/src/serializable.rs | 2 ++ skipdb-core/src/lib.rs | 2 ++ skipdb-core/src/types.rs | 15 +++++++++++++-- skipdb/src/optimistic.rs | 2 ++ skipdb/src/serializable.rs | 2 ++ 6 files changed, 23 insertions(+), 2 deletions(-) diff --git a/async-skipdb/src/optimistic.rs b/async-skipdb/src/optimistic.rs index da38b35..6285db3 100644 --- a/async-skipdb/src/optimistic.rs +++ b/async-skipdb/src/optimistic.rs @@ -1,3 +1,4 @@ +use skipdb_core::types::Values; use std::{collections::hash_map::RandomState, hash::Hash}; use super::*; @@ -134,6 +135,7 @@ impl OptimisticDb where K: Ord + Eq + Hash + Send + 'static, V: Send + 'static, + Values: Send, SP: AsyncSpawner, { /// Compact the database. diff --git a/async-skipdb/src/serializable.rs b/async-skipdb/src/serializable.rs index 2483718..450b034 100644 --- a/async-skipdb/src/serializable.rs +++ b/async-skipdb/src/serializable.rs @@ -1,5 +1,6 @@ use async_txn::{AsyncSpawner, BTreeCm}; pub use cheap_clone::CheapClone; +use skipdb_core::types::Values; use super::*; @@ -136,6 +137,7 @@ impl SerializableDb where K: CheapClone + Ord + Send + 'static, V: Send + 'static, + Values: Send, S: AsyncSpawner, { /// Compact the database. diff --git a/skipdb-core/src/lib.rs b/skipdb-core/src/lib.rs index ea22448..4961e6b 100644 --- a/skipdb-core/src/lib.rs +++ b/skipdb-core/src/lib.rs @@ -1,5 +1,6 @@ #![cfg_attr(not(feature = "std"), no_std)] #![deny(warnings)] +#![forbid(unsafe_code)] #![allow(clippy::type_complexity)] extern crate alloc; @@ -182,6 +183,7 @@ impl SkipCore where K: Ord + Send + 'static, V: Send + 'static, + Values: Send, { pub fn compact(&self, new_discard_version: u64) { match self diff --git a/skipdb-core/src/types.rs b/skipdb-core/src/types.rs index 26ba4b7..33f48ae 100644 --- a/skipdb-core/src/types.rs +++ b/skipdb-core/src/types.rs @@ -18,8 +18,6 @@ pub struct Values { values: SkipMap>, } -unsafe impl Send for Values {} - impl Values { pub(crate) fn new() -> Self { Self { @@ -172,3 +170,16 @@ where core::ops::Deref::deref(self).eq(other) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_values_send() { + fn takes_send(_t: T) {} + + let values = Values::<()>::new(); + takes_send(values); + } +} diff --git a/skipdb/src/optimistic.rs b/skipdb/src/optimistic.rs index c9ebcd0..6b77461 100644 --- a/skipdb/src/optimistic.rs +++ b/skipdb/src/optimistic.rs @@ -1,4 +1,5 @@ use super::*; +use skipdb_core::types::Values; use std::{collections::hash_map::RandomState, hash::Hash}; mod write; @@ -124,6 +125,7 @@ impl OptimisticDb where K: Ord + Eq + Hash + Send + 'static, V: Send + 'static, + Values: Send, S: BuildHasher + Clone, { /// Compact the database. diff --git a/skipdb/src/serializable.rs b/skipdb/src/serializable.rs index 4afdcf0..157672f 100644 --- a/skipdb/src/serializable.rs +++ b/skipdb/src/serializable.rs @@ -1,4 +1,5 @@ pub use cheap_clone::CheapClone; +use skipdb_core::types::Values; use txn::BTreeCm; use super::*; @@ -126,6 +127,7 @@ impl SerializableDb where K: CheapClone + Ord + Send + 'static, V: Send + 'static, + Values: Send, { /// Compact the database. #[inline]