Skip to content

Commit

Permalink
Remove unnecessary unsafe in skipdb-core (#14)
Browse files Browse the repository at this point in the history
- Adds a test that proves `Values: Send` without the unsafe trait impl.
- Adds an extra constraint to functions that require `Values<V>: Send`.
- Adds `#![forbid(unsafe_code)]` to skipdb-core.
  • Loading branch information
parasyte committed Apr 29, 2024
1 parent 716be2c commit 3362606
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions async-skipdb/src/optimistic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use skipdb_core::types::Values;
use std::{collections::hash_map::RandomState, hash::Hash};

use super::*;
Expand Down Expand Up @@ -134,6 +135,7 @@ impl<K, V, SP, S> OptimisticDb<K, V, SP, S>
where
K: Ord + Eq + Hash + Send + 'static,
V: Send + 'static,
Values<V>: Send,
SP: AsyncSpawner,
{
/// Compact the database.
Expand Down
2 changes: 2 additions & 0 deletions async-skipdb/src/serializable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use async_txn::{AsyncSpawner, BTreeCm};
pub use cheap_clone::CheapClone;
use skipdb_core::types::Values;

use super::*;

Expand Down Expand Up @@ -136,6 +137,7 @@ impl<K, V, S> SerializableDb<K, V, S>
where
K: CheapClone + Ord + Send + 'static,
V: Send + 'static,
Values<V>: Send,
S: AsyncSpawner,
{
/// Compact the database.
Expand Down
2 changes: 2 additions & 0 deletions skipdb-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![deny(warnings)]
#![forbid(unsafe_code)]
#![allow(clippy::type_complexity)]

extern crate alloc;
Expand Down Expand Up @@ -182,6 +183,7 @@ impl<K, V> SkipCore<K, V>
where
K: Ord + Send + 'static,
V: Send + 'static,
Values<V>: Send,
{
pub fn compact(&self, new_discard_version: u64) {
match self
Expand Down
15 changes: 13 additions & 2 deletions skipdb-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ pub struct Values<V> {
values: SkipMap<u64, Option<V>>,
}

unsafe impl<V: Send> Send for Values<V> {}

impl<V> Values<V> {
pub(crate) fn new() -> Self {
Self {
Expand Down Expand Up @@ -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: Send>(_t: T) {}

let values = Values::<()>::new();
takes_send(values);
}
}
2 changes: 2 additions & 0 deletions skipdb/src/optimistic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use skipdb_core::types::Values;
use std::{collections::hash_map::RandomState, hash::Hash};

mod write;
Expand Down Expand Up @@ -124,6 +125,7 @@ impl<K, V, S> OptimisticDb<K, V, S>
where
K: Ord + Eq + Hash + Send + 'static,
V: Send + 'static,
Values<V>: Send,
S: BuildHasher + Clone,
{
/// Compact the database.
Expand Down
2 changes: 2 additions & 0 deletions skipdb/src/serializable.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub use cheap_clone::CheapClone;
use skipdb_core::types::Values;
use txn::BTreeCm;

use super::*;
Expand Down Expand Up @@ -126,6 +127,7 @@ impl<K, V> SerializableDb<K, V>
where
K: CheapClone + Ord + Send + 'static,
V: Send + 'static,
Values<V>: Send,
{
/// Compact the database.
#[inline]
Expand Down

0 comments on commit 3362606

Please sign in to comment.