From 7af5861d7ce58f60b222cf27f8b3833c7fff02cb Mon Sep 17 00:00:00 2001 From: Ofek Shaltiel Date: Wed, 14 Aug 2024 17:26:49 +0300 Subject: [PATCH] enforce list's push key type and export it --- src/lists.rs | 12 ++++++++---- src/typed.rs | 19 ++++++++++--------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/lists.rs b/src/lists.rs index 4bbed36..2cd6d84 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -1123,16 +1123,20 @@ impl CandyStore { &self, list_key: &B1, val: &B2, - ) -> Result { + ) -> Result { 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, val: Vec) -> Result { - let uuid = Uuid::new_v4(); + pub fn owned_push_to_list_head( + &self, + list_key: Vec, + val: Vec, + ) -> Result { + let uuid = EncodableUuid::from(Uuid::new_v4()); let status = self._insert_to_list( list_key, - uuid.as_bytes().to_vec(), + uuid.to_bytes::(), val, InsertMode::GetOrCreate, InsertPosition::Head, diff --git a/src/typed.rs b/src/typed.rs index 85baf7a..b52b4f6 100644 --- a/src/typed.rs +++ b/src/typed.rs @@ -1,6 +1,5 @@ use anyhow::anyhow; use std::{borrow::Borrow, marker::PhantomData, sync::Arc}; -use uuid::Uuid; use crate::{ insertion::{ReplaceStatus, SetStatus}, @@ -449,14 +448,15 @@ where self.store.owned_discard_list(list_key) } - // only available if K == Uuid - fn _push_head( + /// Same as [CandyStore::push_to_list_head], but `list_key` is typed + pub fn push_head( &self, list_key: &Q1, val: &Q2, - ) -> Result + ) -> Result where L: Borrow, + EncodableUuid: From, V: Borrow, { let list_key = Self::make_list_key(list_key); @@ -464,14 +464,15 @@ where self.store.owned_push_to_list_head(list_key, val) } - // only available if K == Uuid - fn _push_tail( + /// Same as [CandyStore::push_to_list_tail], but `list_key` is typed + pub fn push_tail( &self, list_key: &Q1, val: &Q2, ) -> Result where L: Borrow, + EncodableUuid: From, V: Borrow, { let list_key = Self::make_list_key(list_key); @@ -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 { - pub list: CandyTypedList, + pub list: CandyTypedList, } impl Clone for CandyTypedDeque { @@ -564,7 +565,7 @@ where L: Borrow, V: Borrow, { - self.list._push_head(list_key, val)?; + self.list.push_head(list_key, val)?; Ok(()) } @@ -578,7 +579,7 @@ where L: Borrow, V: Borrow, { - self.list._push_tail(list_key, val)?; + self.list.push_tail(list_key, val)?; Ok(()) }