3
3
use derive_new:: new;
4
4
use std:: cmp:: { Eq , PartialEq } ;
5
5
use std:: convert:: TryFrom ;
6
- use std:: ops:: { Bound , Deref , DerefMut , Range , RangeFrom , RangeInclusive } ;
6
+ use std:: ops:: { Bound , Range , RangeFrom , RangeInclusive } ;
7
7
use std:: { fmt, str, u8} ;
8
8
9
9
use crate :: { Error , Result } ;
@@ -25,8 +25,6 @@ impl<'a> fmt::Display for HexRepr<'a> {
25
25
/// valid `UTF-8` is not required. This means that the user is permitted to store any data they wish,
26
26
/// as long as it can be represented by bytes. (Which is to say, pretty much anything!)
27
27
///
28
- /// This is a *wrapper type* that implements `Deref<Target=[u8]>` so it can be used like one transparently.
29
- ///
30
28
/// This type also implements `From` for many types. With one exception, these are all done without
31
29
/// reallocation. Using a `&'static str`, like many examples do for simplicity, has an internal
32
30
/// allocation cost.
@@ -60,6 +58,11 @@ impl<'a> fmt::Display for HexRepr<'a> {
60
58
pub struct Key ( Vec < u8 > ) ;
61
59
62
60
impl Key {
61
+ #[ inline]
62
+ pub fn is_empty ( & self ) -> bool {
63
+ self . 0 . is_empty ( )
64
+ }
65
+
63
66
#[ inline]
64
67
fn zero_terminated ( & self ) -> bool {
65
68
self . 0 . last ( ) . map ( |i| * i == 0 ) . unwrap_or ( false )
@@ -109,50 +112,18 @@ impl From<&'static str> for Key {
109
112
}
110
113
}
111
114
112
- impl AsRef < Key > for Key {
113
- fn as_ref ( & self ) -> & Key {
114
- self
115
- }
116
- }
117
-
118
- impl AsMut < Key > for Key {
119
- fn as_mut ( & mut self ) -> & mut Key {
120
- self
121
- }
122
- }
123
-
124
- impl AsRef < [ u8 ] > for Key {
125
- fn as_ref ( & self ) -> & [ u8 ] {
126
- & self . 0
127
- }
128
- }
129
-
130
- impl AsMut < [ u8 ] > for Key {
131
- fn as_mut ( & mut self ) -> & mut [ u8 ] {
132
- & mut self . 0
133
- }
134
- }
135
-
136
115
impl Into < Vec < u8 > > for Key {
137
116
fn into ( self ) -> Vec < u8 > {
138
117
self . 0
139
118
}
140
119
}
141
120
142
- impl Deref for Key {
143
- type Target = [ u8 ] ;
144
-
145
- fn deref ( & self ) -> & Self :: Target {
121
+ impl < ' a > Into < & ' a [ u8 ] > for & ' a Key {
122
+ fn into ( self ) -> & ' a [ u8 ] {
146
123
& self . 0
147
124
}
148
125
}
149
126
150
- impl DerefMut for Key {
151
- fn deref_mut ( & mut self ) -> & mut [ u8 ] {
152
- & mut self . 0
153
- }
154
- }
155
-
156
127
impl fmt:: Debug for Key {
157
128
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
158
129
write ! ( f, "Key({})" , HexRepr ( & self . 0 ) )
@@ -165,8 +136,6 @@ impl fmt::Debug for Key {
165
136
/// as valid `UTF-8` is not required. This means that the user is permitted to store any data they wish,
166
137
/// as long as it can be represented by bytes. (Which is to say, pretty much anything!)
167
138
///
168
- /// This is a *wrapper type* that implements `Deref<Target=[u8]>` so it can be used like one transparently.
169
- ///
170
139
/// This type also implements `From` for many types. With one exception, these are all done without
171
140
/// reallocation. Using a `&'static str`, like many examples do for simplicity, has an internal
172
141
/// allocation cost.
@@ -199,6 +168,13 @@ impl fmt::Debug for Key {
199
168
#[ derive( new, Default , Clone , Eq , PartialEq , Hash ) ]
200
169
pub struct Value ( Vec < u8 > ) ;
201
170
171
+ impl Value {
172
+ #[ inline]
173
+ pub fn is_empty ( & self ) -> bool {
174
+ self . 0 . is_empty ( )
175
+ }
176
+ }
177
+
202
178
impl From < Vec < u8 > > for Value {
203
179
fn from ( v : Vec < u8 > ) -> Self {
204
180
Value ( v)
@@ -223,16 +199,8 @@ impl Into<Vec<u8>> for Value {
223
199
}
224
200
}
225
201
226
- impl Deref for Value {
227
- type Target = [ u8 ] ;
228
-
229
- fn deref ( & self ) -> & Self :: Target {
230
- & self . 0
231
- }
232
- }
233
-
234
- impl AsRef < [ u8 ] > for Value {
235
- fn as_ref ( & self ) -> & [ u8 ] {
202
+ impl < ' a > Into < & ' a [ u8 ] > for & ' a Value {
203
+ fn into ( self ) -> & ' a [ u8 ] {
236
204
& self . 0
237
205
}
238
206
}
@@ -335,9 +303,9 @@ impl Into<(Key, Value)> for KvPair {
335
303
impl fmt:: Debug for KvPair {
336
304
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
337
305
let KvPair ( key, value) = self ;
338
- match str:: from_utf8 ( & value) {
339
- Ok ( s) => write ! ( f, "KvPair({}, {:?})" , HexRepr ( & key) , s) ,
340
- Err ( _) => write ! ( f, "KvPair({}, {})" , HexRepr ( & key) , HexRepr ( & value) ) ,
306
+ match str:: from_utf8 ( & value. 0 ) {
307
+ Ok ( s) => write ! ( f, "KvPair({}, {:?})" , HexRepr ( & key. 0 ) , s) ,
308
+ Err ( _) => write ! ( f, "KvPair({}, {})" , HexRepr ( & key. 0 ) , HexRepr ( & value. 0 ) ) ,
341
309
}
342
310
}
343
311
}
0 commit comments