@@ -26,17 +26,33 @@ use crate::storage_trait::{DbHashMap, DbKey, DbValue, PatriciaStorageResult, Sto
2626
2727#[ macro_export]
2828macro_rules! define_short_key_storage {
29- ( $( ( $sizes: ty, $names: ident ) ) ,+ $( , ) ?) => {
29+ ( $( ( $sizes: expr , $sizes_type : ty, $names: ident ) ) ,+ $( , ) ?) => {
3030 $(
31- $crate:: define_short_key_storage!( $sizes, $names) ;
31+ $crate:: define_short_key_storage!( $sizes, $sizes_type , $ names) ;
3232 ) +
33+
34+ /// Wrap an existing storage implementation in a boxed short key storage implementation.
35+ /// If no size is given, boxes and returns the original storage.
36+ /// Panics if the size is not within the allowed range.
37+ pub fn wrap_storage_or_panic<S : Storage + ' static >(
38+ size: Option <u8 >,
39+ storage: S ,
40+ ) -> Box <dyn Storage > {
41+ let Some ( size) = size else {
42+ return Box :: new( storage) ;
43+ } ;
44+ match size {
45+ $( $sizes => Box :: new( $names:: new( storage) ) , ) +
46+ size => panic!( "Invalid key size {size}." ) ,
47+ }
48+ }
3349 } ;
3450
35- ( $size: ty, $name: ident) => {
51+ ( $size: expr , $size_type : ty, $name: ident) => {
3652 /// A storage that hashes each key to a $size - byte key.
3753 pub struct $name<S : Storage > {
3854 storage: S ,
39- _bytes: PhantomData <$size >,
55+ _bytes: PhantomData <$size_type >,
4056 }
4157
4258 impl <S : Storage > $name<S > {
@@ -45,7 +61,7 @@ macro_rules! define_short_key_storage {
4561 }
4662
4763 pub fn small_key( key: & DbKey ) -> DbKey {
48- let mut hasher = Blake2s :: <$size >:: new( ) ;
64+ let mut hasher = Blake2s :: <$size_type >:: new( ) ;
4965 hasher. update( key. 0 . as_slice( ) ) ;
5066 let result = hasher. finalize( ) ;
5167 DbKey ( result. as_slice( ) . to_vec( ) )
@@ -81,26 +97,27 @@ macro_rules! define_short_key_storage {
8197 fn delete( & mut self , key: & DbKey ) -> PatriciaStorageResult <Option <DbValue >> {
8298 self . storage. delete( & Self :: small_key( key) )
8399 }
100+
84101 }
85102 } ;
86103}
87104
88105define_short_key_storage ! (
89- ( U16 , ShortKeyStorage16 ) ,
90- ( U17 , ShortKeyStorage17 ) ,
91- ( U18 , ShortKeyStorage18 ) ,
92- ( U19 , ShortKeyStorage19 ) ,
93- ( U20 , ShortKeyStorage20 ) ,
94- ( U21 , ShortKeyStorage21 ) ,
95- ( U22 , ShortKeyStorage22 ) ,
96- ( U23 , ShortKeyStorage23 ) ,
97- ( U24 , ShortKeyStorage24 ) ,
98- ( U25 , ShortKeyStorage25 ) ,
99- ( U26 , ShortKeyStorage26 ) ,
100- ( U27 , ShortKeyStorage27 ) ,
101- ( U28 , ShortKeyStorage28 ) ,
102- ( U29 , ShortKeyStorage29 ) ,
103- ( U30 , ShortKeyStorage30 ) ,
104- ( U31 , ShortKeyStorage31 ) ,
105- ( U32 , ShortKeyStorage32 )
106+ ( 16 , U16 , ShortKeyStorage16 ) ,
107+ ( 17 , U17 , ShortKeyStorage17 ) ,
108+ ( 18 , U18 , ShortKeyStorage18 ) ,
109+ ( 19 , U19 , ShortKeyStorage19 ) ,
110+ ( 20 , U20 , ShortKeyStorage20 ) ,
111+ ( 21 , U21 , ShortKeyStorage21 ) ,
112+ ( 22 , U22 , ShortKeyStorage22 ) ,
113+ ( 23 , U23 , ShortKeyStorage23 ) ,
114+ ( 24 , U24 , ShortKeyStorage24 ) ,
115+ ( 25 , U25 , ShortKeyStorage25 ) ,
116+ ( 26 , U26 , ShortKeyStorage26 ) ,
117+ ( 27 , U27 , ShortKeyStorage27 ) ,
118+ ( 28 , U28 , ShortKeyStorage28 ) ,
119+ ( 29 , U29 , ShortKeyStorage29 ) ,
120+ ( 30 , U30 , ShortKeyStorage30 ) ,
121+ ( 31 , U31 , ShortKeyStorage31 ) ,
122+ ( 32 , U32 , ShortKeyStorage32 )
106123) ;
0 commit comments