@@ -17,97 +17,3 @@ macro_rules! concat_keys {
1717 buf
1818 } ) ;
1919}
20-
21- /// Implements `BinaryKey` trait for any type that implements `BinaryValue`.
22- #[ macro_export]
23- macro_rules! impl_binary_key_for_binary_value {
24- ( $type: ty) => {
25- impl matterdb:: BinaryKey for $type {
26- fn size( & self ) -> usize {
27- matterdb:: BinaryValue :: to_bytes( self ) . len( )
28- }
29-
30- fn write( & self , buffer: & mut [ u8 ] ) -> usize {
31- let mut bytes = matterdb:: BinaryValue :: to_bytes( self ) ;
32- buffer. swap_with_slice( & mut bytes) ;
33- bytes. len( )
34- }
35-
36- fn read( buffer: & [ u8 ] ) -> Self :: Owned {
37- // `unwrap` is safe because only this code uses for
38- // serialize and deserialize these keys.
39- <Self as matterdb:: BinaryValue >:: from_bytes( buffer. into( ) ) . unwrap( )
40- }
41- }
42- } ;
43- }
44-
45- /// Hex conversions for the given `BinaryValue`.
46- ///
47- /// Implements `hex::FromHex` and `hex::ToHex` conversions for the given `BinaryValue` and uses them in
48- /// the implementation of the following traits:
49- ///
50- /// `FromStr`, `Display`, `Serialize`, `Deserialize`.
51- ///
52- /// Pay attention that macro uses `serde_str` under the hood.
53- #[ macro_export]
54- macro_rules! impl_serde_hex_for_binary_value {
55- ( $name: ident) => {
56- impl hex:: ToHex for $name {
57- fn encode_hex<T : std:: iter:: FromIterator <char >>( & self ) -> T {
58- use $crate:: BinaryValue ;
59- BinaryValue :: to_bytes( self ) . encode_hex( )
60- }
61-
62- fn encode_hex_upper<T : std:: iter:: FromIterator <char >>( & self ) -> T {
63- use $crate:: BinaryValue ;
64- BinaryValue :: to_bytes( self ) . encode_hex_upper( )
65- }
66- }
67-
68- impl hex:: FromHex for $name {
69- type Error = $crate:: _reexports:: Error ;
70-
71- fn from_hex<T : AsRef <[ u8 ] >>( v: T ) -> Result <Self , Self :: Error > {
72- use $crate:: BinaryValue ;
73-
74- let bytes = Vec :: <u8 >:: from_hex( v) ?;
75- <Self as BinaryValue >:: from_bytes( bytes. into( ) ) . map_err( From :: from)
76- }
77- }
78-
79- impl std:: fmt:: Display for $name {
80- fn fmt( & self , f: & mut std:: fmt:: Formatter <' _>) -> std:: fmt:: Result {
81- use hex:: ToHex ;
82- write!( f, "{}" , <Self as ToHex >:: encode_hex:: <String >( self ) )
83- }
84- }
85-
86- impl std:: str :: FromStr for $name {
87- type Err = $crate:: _reexports:: Error ;
88-
89- fn from_str( s: & str ) -> Result <Self , Self :: Err > {
90- use hex:: FromHex ;
91- <Self as FromHex >:: from_hex( s)
92- }
93- }
94-
95- impl <' de> serde:: Deserialize <' de> for $name {
96- fn deserialize<D >( deserializer: D ) -> Result <Self , D :: Error >
97- where
98- D : serde:: de:: Deserializer <' de>,
99- {
100- serde_str:: deserialize( deserializer)
101- }
102- }
103-
104- impl serde:: Serialize for $name {
105- fn serialize<S >( & self , serializer: S ) -> Result <S :: Ok , S :: Error >
106- where
107- S : serde:: Serializer ,
108- {
109- serde_str:: serialize( self , serializer)
110- }
111- }
112- } ;
113- }
0 commit comments