Skip to content

Commit

Permalink
Use impl Fn instead of impl FnOnce + Copy
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Jul 9, 2024
1 parent 8d21265 commit 211d2ec
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 29 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 0.12.0

- Bump `rarena-allocator`'s version
- Change value callback from `impl FnOnce + Copy` to `impl Fn`

## 0.11.0

- Refactor and extract lock-free ARENA allocator implementation to [`rarena-allocator`](https://github.com/al8n/rarena) crate.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "skl"
version = "0.11.4"
version = "0.12.0"
edition = "2021"
rust-version = "1.56.0"
repository = "https://github.com/al8n/skl"
Expand Down
21 changes: 11 additions & 10 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ impl<T> Node<T> {
arena: &'a Arena,
trailer: T,
value_size: u32,
f: impl FnOnce(&mut VacantBuffer<'a>) -> Result<(), E>,
f: &impl Fn(&mut VacantBuffer<'a>) -> Result<(), E>,
) -> Result<(), Either<E, Error>> {
let mut bytes = arena
.alloc_aligned_bytes::<T>(value_size)
Expand Down Expand Up @@ -1151,7 +1151,7 @@ impl<T: Trailer, C> SkipMap<T, C> {
key: &Key<'a, 'b>,
trailer: T,
value_size: u32,
f: impl FnOnce(&mut VacantBuffer<'a>) -> Result<(), E>,
f: &impl Fn(&mut VacantBuffer<'a>) -> Result<(), E>,
) -> Result<(NodePtr<T>, u32, Deallocator), Either<E, Error>> {
let height = super::random_height(self.opts.max_height().into());
let (nd, deallocator) = match key {
Expand Down Expand Up @@ -1732,7 +1732,7 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
trailer: T,
key: Key<'a, 'b>,
value_size: u32,
f: impl FnOnce(&mut VacantBuffer<'a>) -> Result<(), E> + Copy,
f: impl Fn(&mut VacantBuffer<'a>) -> Result<(), E>,
success: Ordering,
failure: Ordering,
ins: &mut Inserter<T>,
Expand All @@ -1751,7 +1751,7 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {

if upsert {
return self.upsert(
old, node_ptr, &key, trailer, value_size, f, success, failure,
old, node_ptr, &key, trailer, value_size, &f, success, failure,
);
}

Expand Down Expand Up @@ -1792,10 +1792,11 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
}
};

let (nd, height, mut deallocator) = self.new_node(&k, trailer, value_size, f).map_err(|e| {
k.on_fail(&self.arena);
e
})?;
let (nd, height, mut deallocator) =
self.new_node(&k, trailer, value_size, &f).map_err(|e| {
k.on_fail(&self.arena);
e
})?;

// We always insert from the base level and up. After you add a node in base
// level, we cannot create a node in the level above because it would have
Expand Down Expand Up @@ -1913,7 +1914,7 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {

if upsert {
deallocator.dealloc(&self.arena);
return self.upsert(old, node_ptr, &k, trailer, value_size, f, success, failure);
return self.upsert(old, node_ptr, &k, trailer, value_size, &f, success, failure);
}

deallocator.dealloc(&self.arena);
Expand Down Expand Up @@ -1973,7 +1974,7 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
key: &Key<'a, 'b>,
trailer: T,
value_size: u32,
f: impl FnOnce(&mut VacantBuffer<'a>) -> Result<(), E>,
f: &impl Fn(&mut VacantBuffer<'a>) -> Result<(), E>,
success: Ordering,
failure: Ordering,
) -> Result<UpdateOk<'a, 'b, T, C>, Either<E, Error>> {
Expand Down
8 changes: 4 additions & 4 deletions src/map/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
trailer: T,
key: &'b [u8],
value_size: u32,
f: impl FnOnce(&mut VacantBuffer<'a>) -> Result<(), E> + Copy,
f: impl Fn(&mut VacantBuffer<'a>) -> Result<(), E>,
) -> Result<Option<EntryRef<'a, T, C>>, Either<E, Error>> {
if self.arena.read_only() {
return Err(Either::Right(Error::read_only()));
Expand Down Expand Up @@ -660,7 +660,7 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
trailer: T,
key: &'b [u8],
value_size: u32,
f: impl FnOnce(&mut VacantBuffer<'a>) -> Result<(), E> + Copy,
f: impl Fn(&mut VacantBuffer<'a>) -> Result<(), E>,
) -> Result<Option<EntryRef<'a, T, C>>, Either<E, Error>> {
if self.arena.read_only() {
return Err(Either::Right(Error::read_only()));
Expand Down Expand Up @@ -742,7 +742,7 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
key_size: u27,
key: impl FnOnce(&mut VacantBuffer<'a>) -> Result<(), E>,
val_size: u32,
val: impl FnOnce(&mut VacantBuffer<'a>) -> Result<(), E> + Copy,
val: impl Fn(&mut VacantBuffer<'a>) -> Result<(), E>,
) -> Result<Option<EntryRef<'a, T, C>>, Either<E, Error>> {
let vk = self.fetch_vacant_key(u32::from(key_size), key)?;

Expand Down Expand Up @@ -820,7 +820,7 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
key_size: u27,
key: impl FnOnce(&mut VacantBuffer<'a>) -> Result<(), E>,
val_size: u32,
val: impl FnOnce(&mut VacantBuffer<'a>) -> Result<(), E> + Copy,
val: impl Fn(&mut VacantBuffer<'a>) -> Result<(), E>,
) -> Result<Option<EntryRef<'a, T, C>>, Either<E, Error>> {
let vk = self.fetch_vacant_key(u32::from(key_size), key)?;

Expand Down
16 changes: 2 additions & 14 deletions src/map/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,29 +154,18 @@ fn full_in(l: impl FnOnce(usize) -> SkipMap) {
let l = l(1000);
let mut found_arena_full = false;

let mut full_at = 0;
for i in 0..100 {
if let Err(e) = l.get_or_insert(0, &make_int_key(i), &make_value(i)) {
assert!(matches!(
e,
Error::Arena(ArenaError::InsufficientSpace { .. })
));
found_arena_full = true;
full_at = i;
break;
}
}

assert!(found_arena_full);

let e = l
.get_or_insert(0, &make_int_key(full_at + 1), &make_value(full_at + 1))
.unwrap_err();

assert!(matches!(
e,
Error::Arena(ArenaError::InsufficientSpace { .. })
));
}

#[test]
Expand Down Expand Up @@ -1502,8 +1491,7 @@ fn test_concurrent_one_key_map_mut() {
let open_options = OpenOptions::default()
.create(Some(ARENA_SIZE as u32))
.read(true)
.write(true)
.shrink_on_drop(true);
.write(true);
let map_options = MmapOptions::default();
concurrent_one_key(Arc::new(
SkipMap::map_mut(p, open_options, map_options)
Expand Down Expand Up @@ -2301,7 +2289,7 @@ fn test_reopen_mmap() {
l.flush().unwrap();
}

let open_options = OpenOptions::default().read(true).shrink_on_drop(true);
let open_options = OpenOptions::default().read(true);
let map_options = MmapOptions::default();
let l = SkipMap::<u64>::map(&p, open_options, map_options, 0).unwrap();
assert_eq!(1000, l.len());
Expand Down

0 comments on commit 211d2ec

Please sign in to comment.