Skip to content

Commit

Permalink
Merge pull request #4 from sweet-security/typed_peek
Browse files Browse the repository at this point in the history
enforce list's push key type and export it
  • Loading branch information
ofek-sha authored Aug 14, 2024
2 parents 4658c65 + 7af5861 commit 6156bf9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
12 changes: 8 additions & 4 deletions src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,16 +1123,20 @@ impl CandyStore {
&self,
list_key: &B1,
val: &B2,
) -> Result<Uuid> {
) -> Result<EncodableUuid> {
self.owned_push_to_list_head(list_key.as_ref().to_owned(), val.as_ref().to_owned())
}

/// Owned version of [Self::push_to_list_head]
pub fn owned_push_to_list_head(&self, list_key: Vec<u8>, val: Vec<u8>) -> Result<Uuid> {
let uuid = Uuid::new_v4();
pub fn owned_push_to_list_head(
&self,
list_key: Vec<u8>,
val: Vec<u8>,
) -> Result<EncodableUuid> {
let uuid = EncodableUuid::from(Uuid::new_v4());
let status = self._insert_to_list(
list_key,
uuid.as_bytes().to_vec(),
uuid.to_bytes::<LE>(),
val,
InsertMode::GetOrCreate,
InsertPosition::Head,
Expand Down
19 changes: 10 additions & 9 deletions src/typed.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use anyhow::anyhow;
use std::{borrow::Borrow, marker::PhantomData, sync::Arc};
use uuid::Uuid;

use crate::{
insertion::{ReplaceStatus, SetStatus},
Expand Down Expand Up @@ -449,29 +448,31 @@ where
self.store.owned_discard_list(list_key)
}

// only available if K == Uuid
fn _push_head<Q1: ?Sized + Encode, Q2: ?Sized + Encode>(
/// Same as [CandyStore::push_to_list_head], but `list_key` is typed
pub fn push_head<Q1: ?Sized + Encode, Q2: ?Sized + Encode>(
&self,
list_key: &Q1,
val: &Q2,
) -> Result<Uuid>
) -> Result<EncodableUuid>
where
L: Borrow<Q1>,
EncodableUuid: From<K>,
V: Borrow<Q2>,
{
let list_key = Self::make_list_key(list_key);
let val = val.to_bytes::<LE>();
self.store.owned_push_to_list_head(list_key, val)
}

// only available if K == Uuid
fn _push_tail<Q1: ?Sized + Encode, Q2: ?Sized + Encode>(
/// Same as [CandyStore::push_to_list_tail], but `list_key` is typed
pub fn push_tail<Q1: ?Sized + Encode, Q2: ?Sized + Encode>(
&self,
list_key: &Q1,
val: &Q2,
) -> Result<EncodableUuid>
where
L: Borrow<Q1>,
EncodableUuid: From<K>,
V: Borrow<Q2>,
{
let list_key = Self::make_list_key(list_key);
Expand Down Expand Up @@ -532,7 +533,7 @@ where
/// and popping from either the head or the tail. The keys are auto-generated internally and are not exposed to
/// the caller
pub struct CandyTypedDeque<L, V> {
pub list: CandyTypedList<L, uuid::Bytes, V>,
pub list: CandyTypedList<L, EncodableUuid, V>,
}

impl<L, V> Clone for CandyTypedDeque<L, V> {
Expand Down Expand Up @@ -564,7 +565,7 @@ where
L: Borrow<Q1>,
V: Borrow<Q2>,
{
self.list._push_head(list_key, val)?;
self.list.push_head(list_key, val)?;
Ok(())
}

Expand All @@ -578,7 +579,7 @@ where
L: Borrow<Q1>,
V: Borrow<Q2>,
{
self.list._push_tail(list_key, val)?;
self.list.push_tail(list_key, val)?;
Ok(())
}

Expand Down

0 comments on commit 6156bf9

Please sign in to comment.