@@ -25,23 +25,34 @@ pub type KeyStrType<'a> = &'a str;
2525#[ derive( Debug , Default , Clone , PartialEq , Eq , Hash ) ]
2626pub struct ObjectAsVec < ' ctx > ( pub ( crate ) Vec < ( KeyStrType < ' ctx > , Value < ' ctx > ) > ) ;
2727
28- #[ cfg( feature = "cowkeys" ) ]
29- impl < ' ctx > From < Vec < ( & ' ctx str , Value < ' ctx > ) > > for ObjectAsVec < ' ctx > {
30- fn from ( vec : Vec < ( & ' ctx str , Value < ' ctx > ) > ) -> Self {
28+ impl < ' ctx , K , V > From < Vec < ( K , V ) > > for ObjectAsVec < ' ctx >
29+ where
30+ K : Into < KeyStrType < ' ctx > > ,
31+ V : Into < Value < ' ctx > > ,
32+ {
33+ fn from ( vec : Vec < ( K , V ) > ) -> Self {
3134 Self :: from_iter ( vec)
3235 }
3336}
3437
35- #[ cfg( not( feature = "cowkeys" ) ) ]
36- impl < ' ctx > From < Vec < ( & ' ctx str , Value < ' ctx > ) > > for ObjectAsVec < ' ctx > {
37- fn from ( vec : Vec < ( & ' ctx str , Value < ' ctx > ) > ) -> Self {
38- Self ( vec)
38+ impl < ' ctx , K , V , const N : usize > From < [ ( K , V ) ; N ] > for ObjectAsVec < ' ctx >
39+ where
40+ K : Into < KeyStrType < ' ctx > > ,
41+ V : Into < Value < ' ctx > >
42+ {
43+ #[ inline]
44+ fn from ( value : [ ( K , V ) ; N ] ) -> Self {
45+ Self :: from_iter ( value)
3946 }
4047}
4148
42- impl < ' ctx > FromIterator < ( & ' ctx str , Value < ' ctx > ) > for ObjectAsVec < ' ctx > {
43- fn from_iter < T : IntoIterator < Item = ( & ' ctx str , Value < ' ctx > ) > > ( iter : T ) -> Self {
44- Self ( iter. into_iter ( ) . map ( |( k, v) | ( k. into ( ) , v) ) . collect ( ) )
49+ impl < ' ctx , K , V > FromIterator < ( K , V ) > for ObjectAsVec < ' ctx >
50+ where
51+ K : Into < KeyStrType < ' ctx > > ,
52+ V : Into < Value < ' ctx > >
53+ {
54+ fn from_iter < T : IntoIterator < Item = ( K , V ) > > ( iter : T ) -> Self {
55+ Self ( iter. into_iter ( ) . map ( |( k, v) | ( k. into ( ) , v. into ( ) ) ) . collect ( ) )
4556 }
4657}
4758
@@ -212,14 +223,20 @@ impl<'ctx> ObjectAsVec<'ctx> {
212223 /// This operation is linear in the size of the Vec because it potentially requires iterating
213224 /// through all elements to find a matching key.
214225 #[ inline]
215- pub fn insert ( & mut self , key : & ' ctx str , value : Value < ' ctx > ) -> Option < Value < ' ctx > > {
226+ pub fn insert < K , V > ( & mut self , key : K , value : V ) -> Option < Value < ' ctx > >
227+ where
228+ K : Into < KeyStrType < ' ctx > > ,
229+ V : Into < Value < ' ctx > > ,
230+ {
231+ let key = key. into ( ) ;
232+ let value = value. into ( ) ;
216233 for ( k, v) in & mut self . 0 {
217234 if * k == key {
218235 return Some ( std:: mem:: replace ( v, value) ) ;
219236 }
220237 }
221238 // If the key is not found, push the new key-value pair to the end of the Vec
222- self . 0 . push ( ( key. into ( ) , value) ) ;
239+ self . 0 . push ( ( key, value) ) ;
223240 None
224241 }
225242
@@ -306,12 +323,26 @@ mod tests {
306323 assert_eq ! ( obj. len( ) , 0 ) ;
307324 }
308325
326+ #[ test]
327+ fn test_initialization_from_array ( ) {
328+ let obj = ObjectAsVec :: from ( [
329+ ( "a" , 0u64 ) ,
330+ ( "b" , 1u64 ) ,
331+ ( "c" , 2u64 ) ,
332+ ] ) ;
333+
334+ assert_eq ! ( obj. len( ) , 3 ) ;
335+ assert_eq ! ( obj. get( "a" ) , Some ( & Value :: Number ( 0u64 . into( ) ) ) ) ;
336+ assert_eq ! ( obj. get( "b" ) , Some ( & Value :: Number ( 1u64 . into( ) ) ) ) ;
337+ assert_eq ! ( obj. get( "c" ) , Some ( & Value :: Number ( 2u64 . into( ) ) ) ) ;
338+ }
339+
309340 #[ test]
310341 fn test_initialization_from_vec ( ) {
311342 let obj = ObjectAsVec :: from ( vec ! [
312- ( "a" , Value :: Number ( 0u64 . into ( ) ) ) ,
313- ( "b" , Value :: Number ( 1u64 . into ( ) ) ) ,
314- ( "c" , Value :: Number ( 2u64 . into ( ) ) ) ,
343+ ( "a" , 0u64 ) ,
344+ ( "b" , 1u64 ) ,
345+ ( "c" , 2u64 ) ,
315346 ] ) ;
316347
317348 assert_eq ! ( obj. len( ) , 3 ) ;
@@ -441,7 +472,7 @@ mod tests {
441472 fn test_insert_multiple_types ( ) {
442473 let mut obj = ObjectAsVec :: default ( ) ;
443474 obj. insert ( "boolean" , Value :: Bool ( true ) ) ;
444- obj. insert ( "number" , Value :: Number ( 3.14 . into ( ) ) ) ;
475+ obj. insert ( "number" , Value :: Number ( 1.23 . into ( ) ) ) ;
445476 obj. insert ( "string" , Value :: Str ( Cow :: Borrowed ( "Hello" ) ) ) ;
446477 obj. insert ( "null" , Value :: Null ) ;
447478
@@ -454,7 +485,7 @@ mod tests {
454485 ) ;
455486 assert_eq ! ( obj. len( ) , 5 ) ;
456487 assert_eq ! ( obj. get( "boolean" ) , Some ( & Value :: Bool ( true ) ) ) ;
457- assert_eq ! ( obj. get( "number" ) , Some ( & Value :: Number ( 3.14 . into( ) ) ) ) ;
488+ assert_eq ! ( obj. get( "number" ) , Some ( & Value :: Number ( 1.23 . into( ) ) ) ) ;
458489 assert_eq ! ( obj. get( "string" ) , Some ( & Value :: Str ( Cow :: Borrowed ( "Hello" ) ) ) ) ;
459490 assert_eq ! ( obj. get( "null" ) , Some ( & Value :: Null ) ) ;
460491 assert_eq ! (
@@ -516,7 +547,7 @@ mod tests {
516547 . unwrap ( ) ;
517548
518549 // ensure that the found object matches the searched for object
519- let ( key, value ) = obj. get_key_value_at ( idx) . unwrap ( ) ;
550+ let ( key, _ ) = obj. get_key_value_at ( idx) . unwrap ( ) ;
520551 assert_eq ! ( key, "city" ) ;
521552 }
522553}
0 commit comments