Skip to content

Commit

Permalink
Redesign transfer trait
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Dec 25, 2024
1 parent b081c3d commit 8d0a98e
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 176 deletions.
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.22.15"
version = "0.22.16"
edition = "2021"
rust-version = "1.81.0"
repository = "https://github.com/al8n/skl"
Expand Down
45 changes: 13 additions & 32 deletions src/dynamic/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
ref_counter::RefCounter,
traits::Constructable,
types::{internal::ValuePointer as ValuePointerType, Height, KeyBuilder, ValueBuilder},
FindResult, Header, Inserter, MaybeTombstone, Splice, State, Transformable, Version,
FindResult, Header, Inserter, MaybeTombstone, Splice, Transfer, Version,
};

mod entry;
Expand Down Expand Up @@ -318,8 +318,7 @@ where
contains_key: impl Fn(&[u8]) -> bool,
) -> Option<EntryRef<'a, S, C, A, R>>
where
S: State,
S::Data<'a, &'a [u8]>: Transformable<Input = Option<&'a [u8]>>,
S: Transfer<'a, &'a [u8]>,
{
loop {
unsafe {
Expand All @@ -337,13 +336,8 @@ where
let pointer = nd.get_value_pointer::<A>();
let value =
nd.get_value_by_value_offset(&self.arena, pointer.value_offset, pointer.value_len);
let ent = EntryRef::from_node_with_pointer(
version,
*nd,
self,
Some(nk),
<S::Data<'a, &'a [u8]> as Transformable>::from_input(value),
);
let ent =
EntryRef::from_node_with_pointer(version, *nd, self, Some(nk), S::from_input(value));
return Some(ent);
}

Expand All @@ -359,8 +353,7 @@ where
contains_key: impl Fn(&[u8]) -> bool,
) -> Option<EntryRef<'a, S, C, A, R>>
where
S: State,
S::Data<'a, &'a [u8]>: Transformable<Input = Option<&'a [u8]>>,
S: Transfer<'a, &'a [u8]>,
{
loop {
unsafe {
Expand Down Expand Up @@ -390,7 +383,7 @@ where
*nd,
self,
Some(nk),
<S::Data<'a, &'a [u8]> as Transformable>::from_input(value),
S::from_input(value),
);
return Some(ent);
}
Expand All @@ -411,13 +404,8 @@ where
let pointer = nd.get_value_pointer::<A>();
let value =
nd.get_value_by_value_offset(&self.arena, pointer.value_offset, pointer.value_len);
let ent = EntryRef::from_node_with_pointer(
version,
*nd,
self,
Some(nk),
<S::Data<'a, &'a [u8]> as Transformable>::from_input(value),
);
let ent =
EntryRef::from_node_with_pointer(version, *nd, self, Some(nk), S::from_input(value));
return Some(ent);
}

Expand All @@ -433,8 +421,7 @@ where
contains_key: impl Fn(&[u8]) -> bool,
) -> Option<EntryRef<'a, S, C, A, R>>
where
S: State,
S::Data<'a, &'a [u8]>: Transformable<Input = Option<&'a [u8]>>,
S: Transfer<'a, &'a [u8]>,
{
loop {
unsafe {
Expand All @@ -452,13 +439,8 @@ where
let pointer = nd.get_value_pointer::<A>();
let value =
nd.get_value_by_value_offset(&self.arena, pointer.value_offset, pointer.value_len);
let ent = EntryRef::from_node_with_pointer(
version,
*nd,
self,
Some(nk),
<S::Data<'a, &'a [u8]> as Transformable>::from_input(value),
);
let ent =
EntryRef::from_node_with_pointer(version, *nd, self, Some(nk), S::from_input(value));
return Some(ent);
}

Expand All @@ -474,8 +456,7 @@ where
contains_key: impl Fn(&[u8]) -> bool,
) -> Option<EntryRef<'a, S, C, A, R>>
where
S: State,
S::Data<'a, &'a [u8]>: Sized + Transformable<Input = Option<&'a [u8]>>,
S: Transfer<'a, &'a [u8]>,
{
loop {
unsafe {
Expand Down Expand Up @@ -522,7 +503,7 @@ where
*nd,
self,
Some(nk),
<S::Data<'a, &'a [u8]> as Transformable>::from_input(value),
<S as crate::sealed::Sealed<'_, &[u8]>>::from_input(value),
);
return Some(ent);
}
Expand Down
13 changes: 6 additions & 7 deletions src/dynamic/list/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
allocator::{Allocator, Node, NodePointer, WithVersion},
dynamic::list::SkipList,
ref_counter::RefCounter,
Active, MaybeTombstone, State, Transformable, Version,
Active, MaybeTombstone, State, Transfer, Version,
};
use dbutils::equivalentor::BytesComparator;

Expand Down Expand Up @@ -104,18 +104,18 @@ where

/// Returns the reference to the value, `None` means the entry is removed.
#[inline]
pub fn value(&self) -> <S::Data<'a, &'a [u8]> as Transformable>::Output
pub fn value(&self) -> S::Data<'a, S::To>
where
S::Data<'a, &'a [u8]>: Transformable,
S: Transfer<'a, &'a [u8]>,
{
self.value.transform()
S::transfer(&self.value)
}

/// Returns `true` if the entry is marked as removed
#[inline]
pub fn tombstone(&self) -> bool
where
S::Data<'a, &'a [u8]>: Transformable,
S: Transfer<'a, &'a [u8]>,
{
!S::validate_data(&self.value)
}
Expand All @@ -126,8 +126,7 @@ where
C: BytesComparator,
A: Allocator,
R: RefCounter,
S: State,
S::Data<'a, &'a [u8]>: Sized + Transformable<Input = Option<&'a [u8]>>,
S: Transfer<'a, &'a [u8]>,
{
/// Returns the next entry in the map.
#[inline]
Expand Down
18 changes: 9 additions & 9 deletions src/dynamic/list/iterator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{Allocator, EntryRef, NodePointer, RefCounter, SkipList, Version};
use crate::{allocator::Node, State, Transformable};
use crate::{allocator::Node, State, Transfer};
use core::{
borrow::Borrow,
ops::{Bound, RangeBounds},
Expand Down Expand Up @@ -155,8 +155,8 @@ where
RC: RefCounter,
Q: ?Sized + Borrow<[u8]>,
R: RangeBounds<Q>,
S: State,
S::Data<'a, &'a [u8]>: Copy + Transformable<Input = Option<&'a [u8]>>,
S: Transfer<'a, &'a [u8]>,
S::Data<'a, &'a [u8]>: Copy,
{
/// Advances to the next position. Returns the key and value if the
/// iterator is pointing at a valid entry, and `None` otherwise.
Expand Down Expand Up @@ -384,8 +384,8 @@ where
RC: RefCounter,
Q: ?Sized + Borrow<[u8]>,
R: RangeBounds<Q>,
S: State,
S::Data<'a, &'a [u8]>: Copy + Transformable<Input = Option<&'a [u8]>>,
S: Transfer<'a, &'a [u8]>,
S::Data<'a, &'a [u8]>: Copy,
{
/// Moves the iterator to the highest element whose key is below the given bound.
/// If no such element is found then `None` is returned.
Expand Down Expand Up @@ -604,8 +604,8 @@ where
RC: RefCounter,
Q: ?Sized + Borrow<[u8]>,
R: RangeBounds<Q>,
S: State,
S::Data<'a, &'a [u8]>: Copy + Transformable<Input = Option<&'a [u8]>>,
S: Transfer<'a, &'a [u8]>,
S::Data<'a, &'a [u8]>: Copy,
{
type Item = EntryRef<'a, S, C, A, RC>;

Expand Down Expand Up @@ -652,8 +652,8 @@ where
RC: RefCounter,
Q: ?Sized + Borrow<[u8]>,
R: RangeBounds<Q>,
S: State,
S::Data<'a, &'a [u8]>: Copy + Transformable<Input = Option<&'a [u8]>>,
S: Transfer<'a, &'a [u8]>,
S::Data<'a, &'a [u8]>: Copy,
{
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
Expand Down
16 changes: 6 additions & 10 deletions src/generic/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ use crate::{
allocator::{Allocator, Deallocator, Meta, Node, NodePointer, Pointer, ValuePointer},
encode_key_size_and_height,
error::Error,
generic::{MaybeTombstone, State},
generic::MaybeTombstone,
internal::RefMeta,
options::CompressionPolicy,
random_height,
ref_counter::RefCounter,
traits::Constructable,
ty_ref,
types::{internal::ValuePointer as ValuePointerType, Height, KeyBuilder, ValueBuilder},
FindResult, Header, Inserter, Splice, Transformable, Version,
FindResult, Header, Inserter, Splice, Transfer, Version,
};

mod entry;
Expand Down Expand Up @@ -357,8 +357,7 @@ where
contains_key: impl Fn(&K::Ref<'a>) -> bool,
) -> Option<EntryRef<'a, K, V, S, C, A, R>>
where
S: State,
S::Data<'a, LazyRef<'a, V>>: Transformable<Input = Option<&'a [u8]>>,
S: Transfer<'a, LazyRef<'a, V>>,
{
loop {
unsafe {
Expand Down Expand Up @@ -392,8 +391,7 @@ where
contains_key: impl Fn(&K::Ref<'a>) -> bool,
) -> Option<EntryRef<'a, K, V, S, C, A, R>>
where
S: State,
S::Data<'a, LazyRef<'a, V>>: Transformable<Input = Option<&'a [u8]>>,
S: Transfer<'a, LazyRef<'a, V>>,
C: TypeRefEquivalentor<'a, K>,
{
loop {
Expand Down Expand Up @@ -461,8 +459,7 @@ where
contains_key: impl Fn(&K::Ref<'a>) -> bool,
) -> Option<EntryRef<'a, K, V, S, C, A, R>>
where
S: State,
S::Data<'a, LazyRef<'a, V>>: Transformable<Input = Option<&'a [u8]>>,
S: Transfer<'a, LazyRef<'a, V>>,
{
loop {
unsafe {
Expand Down Expand Up @@ -497,8 +494,7 @@ where
contains_key: impl Fn(&K::Ref<'a>) -> bool,
) -> Option<EntryRef<'a, K, V, S, C, A, R>>
where
S: State,
S::Data<'a, LazyRef<'a, V>>: Transformable<Input = Option<&'a [u8]>>,
S: Transfer<'a, LazyRef<'a, V>>,
C: TypeRefEquivalentor<'a, K>,
{
loop {
Expand Down
31 changes: 14 additions & 17 deletions src/generic/list/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
allocator::{Allocator, Node, NodePointer, WithVersion},
generic::{Active, MaybeTombstone, State},
types::internal::ValuePointer,
Transformable, Version,
Transfer, Version,
};

/// An entry reference of the `SkipMap`.
Expand All @@ -35,9 +35,8 @@ where
V: ?Sized + Type,
A: Allocator,
R: RefCounter,
S: State,
S::Data<'a, LazyRef<'a, V>>: Transformable,
<S::Data<'a, LazyRef<'a, V>> as Transformable>::Output: core::fmt::Debug,
S: Transfer<'a, LazyRef<'a, V>>,
S::Data<'a, S::To>: core::fmt::Debug,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("EntryRef")
Expand Down Expand Up @@ -119,27 +118,27 @@ where

/// Returns the reference to the value, `None` means the entry is removed.
#[inline]
pub fn value(&self) -> <S::Data<'a, LazyRef<'a, V>> as Transformable>::Output
pub fn value(&self) -> S::Data<'a, S::To>
where
S::Data<'a, LazyRef<'a, V>>: Transformable,
S: Transfer<'a, LazyRef<'a, V>>,
{
self.value.transform()
S::transfer(&self.value)
}

/// Returns the value in raw bytes
#[inline]
pub fn raw_value(&self) -> <S::Data<'a, LazyRef<'a, V>> as Transformable>::Input
pub fn raw_value(&self) -> S::Data<'a, &'a [u8]>
where
S::Data<'a, LazyRef<'a, V>>: Transformable,
S: Transfer<'a, LazyRef<'a, V>>,
{
self.value.input()
S::input(&self.value)
}

/// Returns `true` if the entry is marked as removed
#[inline]
pub fn tombstone(&self) -> bool
where
S::Data<'a, LazyRef<'a, V>>: Transformable,
S: Transfer<'a, LazyRef<'a, V>>,
{
!S::validate_data(&self.value)
}
Expand All @@ -149,8 +148,7 @@ impl<'a, K, V, S, C, A, R> EntryRef<'a, K, V, S, C, A, R>
where
K: ?Sized + Type,
V: ?Sized + Type,
S: State,
S::Data<'a, LazyRef<'a, V>>: Sized + Transformable<Input = Option<&'a [u8]>>,
S: Transfer<'a, LazyRef<'a, V>>,
A: Allocator,
R: RefCounter,
C: TypeRefComparator<'a, K>,
Expand Down Expand Up @@ -226,8 +224,7 @@ impl<'a, K, V, S, C, A, R> EntryRef<'a, K, V, S, C, A, R>
where
K: ?Sized + Type,
V: ?Sized + Type,
S: State,
S::Data<'a, LazyRef<'a, V>>: Transformable<Input = Option<&'a [u8]>>,
S: Transfer<'a, LazyRef<'a, V>>,
A: Allocator,
R: RefCounter,
{
Expand Down Expand Up @@ -258,7 +255,7 @@ where
Self {
list,
key,
value: <S::Data<'a, LazyRef<'a, V>> as Transformable>::from_input(raw_value),
value: S::from_input(raw_value),
value_part_pointer: vp,
version: node.version(),
query_version,
Expand Down Expand Up @@ -297,7 +294,7 @@ where
Self {
list,
key,
value: <S::Data<'a, LazyRef<'a, V>> as Transformable>::from_input(raw_value),
value: S::from_input(raw_value),
value_part_pointer: pointer,
version: node.version(),
query_version,
Expand Down
Loading

0 comments on commit 8d0a98e

Please sign in to comment.