@@ -37,7 +37,7 @@ static MAP_VIEW_HASH_RUNTIME: LazyLock<HistogramVec> = LazyLock::new(|| {
3737} ) ;
3838
3939use std:: {
40- borrow:: Borrow ,
40+ borrow:: { Borrow , Cow } ,
4141 collections:: { btree_map:: Entry , BTreeMap } ,
4242 marker:: PhantomData ,
4343 mem,
@@ -67,7 +67,7 @@ pub struct ByteMapView<C, V> {
6767}
6868
6969/// Whether we have a value or its serialization.
70- pub enum ValueOrBytes < ' a , T > {
70+ enum ValueOrBytes < ' a , T > {
7171 /// The value itself.
7272 Value ( & ' a T ) ,
7373 /// The serialization.
@@ -78,11 +78,11 @@ impl<'a, T> ValueOrBytes<'a, T>
7878where
7979 T : Clone + DeserializeOwned ,
8080{
81- /// Convert to a value .
82- pub fn to_value ( self ) -> Result < T , ViewError > {
81+ /// Convert to a Cow .
82+ fn to_value ( & self ) -> Result < Cow < ' a , T > , ViewError > {
8383 match self {
84- ValueOrBytes :: Value ( value) => Ok ( value . clone ( ) ) ,
85- ValueOrBytes :: Bytes ( bytes) => Ok ( bcs:: from_bytes ( & bytes) ?) ,
84+ ValueOrBytes :: Value ( value) => Ok ( Cow :: Borrowed ( value ) ) ,
85+ ValueOrBytes :: Bytes ( bytes) => Ok ( Cow :: Owned ( bcs:: from_bytes ( bytes) ?) ) ,
8686 }
8787 }
8888}
9292 T : Serialize ,
9393{
9494 /// Convert to bytes.
95- pub fn to_bytes ( self ) -> Result < Vec < u8 > , ViewError > {
95+ pub fn into_bytes ( self ) -> Result < Vec < u8 > , ViewError > {
9696 match self {
9797 ValueOrBytes :: Value ( value) => Ok ( bcs:: to_bytes ( value) ?) ,
9898 ValueOrBytes :: Bytes ( bytes) => Ok ( bytes) ,
@@ -706,13 +706,13 @@ where
706706 /// assert_eq!(part_keys.len(), 2);
707707 /// # })
708708 /// ```
709- pub async fn for_each_key_value_while < F > (
710- & self ,
709+ pub async fn for_each_key_value_while < ' a , F > (
710+ & ' a self ,
711711 mut f : F ,
712712 prefix : Vec < u8 > ,
713713 ) -> Result < ( ) , ViewError >
714714 where
715- F : FnMut ( & [ u8 ] , V ) -> Result < bool , ViewError > + Send ,
715+ F : FnMut ( & [ u8 ] , Cow < ' a , V > ) -> Result < bool , ViewError > + Send ,
716716 {
717717 self . for_each_key_value_or_bytes_while (
718718 |key, value| {
@@ -772,9 +772,13 @@ where
772772 /// assert_eq!(count, 1);
773773 /// # })
774774 /// ```
775- pub async fn for_each_key_value < F > ( & self , mut f : F , prefix : Vec < u8 > ) -> Result < ( ) , ViewError >
775+ pub async fn for_each_key_value < ' a , F > (
776+ & ' a self ,
777+ mut f : F ,
778+ prefix : Vec < u8 > ,
779+ ) -> Result < ( ) , ViewError >
776780 where
777- F : FnMut ( & [ u8 ] , V ) -> Result < ( ) , ViewError > + Send ,
781+ F : FnMut ( & [ u8 ] , Cow < ' a , V > ) -> Result < ( ) , ViewError > + Send ,
778782 {
779783 self . for_each_key_value_while (
780784 |key, value| {
@@ -820,6 +824,7 @@ where
820824 |key, value| {
821825 let mut big_key = prefix. clone ( ) ;
822826 big_key. extend ( key) ;
827+ let value = value. into_owned ( ) ;
823828 key_values. push ( ( big_key, value) ) ;
824829 Ok ( ( ) )
825830 } ,
@@ -917,13 +922,13 @@ where
917922 #[ cfg( with_metrics) ]
918923 let _hash_latency = MAP_VIEW_HASH_RUNTIME . measure_latency ( ) ;
919924 let mut hasher = sha3:: Sha3_256 :: default ( ) ;
920- let mut count = 0 ;
925+ let mut count = 0u32 ;
921926 let prefix = Vec :: new ( ) ;
922927 self . for_each_key_value_or_bytes (
923928 |index, value| {
924929 count += 1 ;
925930 hasher. update_with_bytes ( index) ?;
926- let bytes = value. to_bytes ( ) ?;
931+ let bytes = value. into_bytes ( ) ?;
927932 hasher. update_with_bytes ( & bytes) ?;
928933 Ok ( ( ) )
929934 } ,
@@ -1275,9 +1280,9 @@ where
12751280 /// assert_eq!(values.len(), 2);
12761281 /// # })
12771282 /// ```
1278- pub async fn for_each_index_value_while < F > ( & self , mut f : F ) -> Result < ( ) , ViewError >
1283+ pub async fn for_each_index_value_while < ' a , F > ( & ' a self , mut f : F ) -> Result < ( ) , ViewError >
12791284 where
1280- F : FnMut ( I , V ) -> Result < bool , ViewError > + Send ,
1285+ F : FnMut ( I , Cow < ' a , V > ) -> Result < bool , ViewError > + Send ,
12811286 {
12821287 let prefix = Vec :: new ( ) ;
12831288 self . map
@@ -1312,9 +1317,9 @@ where
13121317 /// assert_eq!(count, 1);
13131318 /// # })
13141319 /// ```
1315- pub async fn for_each_index_value < F > ( & self , mut f : F ) -> Result < ( ) , ViewError >
1320+ pub async fn for_each_index_value < ' a , F > ( & ' a self , mut f : F ) -> Result < ( ) , ViewError >
13161321 where
1317- F : FnMut ( I , V ) -> Result < ( ) , ViewError > + Send ,
1322+ F : FnMut ( I , Cow < ' a , V > ) -> Result < ( ) , ViewError > + Send ,
13181323 {
13191324 let prefix = Vec :: new ( ) ;
13201325 self . map
@@ -1356,6 +1361,7 @@ where
13561361 pub async fn index_values ( & self ) -> Result < Vec < ( I , V ) > , ViewError > {
13571362 let mut key_values = Vec :: new ( ) ;
13581363 self . for_each_index_value ( |index, value| {
1364+ let value = value. into_owned ( ) ;
13591365 key_values. push ( ( index, value) ) ;
13601366 Ok ( ( ) )
13611367 } )
@@ -1771,9 +1777,9 @@ where
17711777 /// assert_eq!(values.len(), 2);
17721778 /// # })
17731779 /// ```
1774- pub async fn for_each_index_value_while < F > ( & self , mut f : F ) -> Result < ( ) , ViewError >
1780+ pub async fn for_each_index_value_while < ' a , F > ( & ' a self , mut f : F ) -> Result < ( ) , ViewError >
17751781 where
1776- F : FnMut ( I , V ) -> Result < bool , ViewError > + Send ,
1782+ F : FnMut ( I , Cow < ' a , V > ) -> Result < bool , ViewError > + Send ,
17771783 {
17781784 let prefix = Vec :: new ( ) ;
17791785 self . map
@@ -1809,9 +1815,9 @@ where
18091815 /// assert_eq!(indices, vec![34, 37]);
18101816 /// # })
18111817 /// ```
1812- pub async fn for_each_index_value < F > ( & self , mut f : F ) -> Result < ( ) , ViewError >
1818+ pub async fn for_each_index_value < ' a , F > ( & ' a self , mut f : F ) -> Result < ( ) , ViewError >
18131819 where
1814- F : FnMut ( I , V ) -> Result < ( ) , ViewError > + Send ,
1820+ F : FnMut ( I , Cow < ' a , V > ) -> Result < ( ) , ViewError > + Send ,
18151821 {
18161822 let prefix = Vec :: new ( ) ;
18171823 self . map
@@ -1853,6 +1859,7 @@ where
18531859 pub async fn index_values ( & self ) -> Result < Vec < ( I , V ) > , ViewError > {
18541860 let mut key_values = Vec :: new ( ) ;
18551861 self . for_each_index_value ( |index, value| {
1862+ let value = value. into_owned ( ) ;
18561863 key_values. push ( ( index, value) ) ;
18571864 Ok ( ( ) )
18581865 } )
0 commit comments