33use derive_new:: new;
44use std:: cmp:: { Eq , PartialEq } ;
55use std:: convert:: TryFrom ;
6- use std:: ops:: { Bound , Deref , DerefMut , Range , RangeFrom , RangeInclusive } ;
6+ use std:: ops:: { Bound , Range , RangeFrom , RangeInclusive } ;
77use std:: { fmt, str, u8} ;
88
99use crate :: { Error , Result } ;
@@ -25,8 +25,6 @@ impl<'a> fmt::Display for HexRepr<'a> {
2525/// valid `UTF-8` is not required. This means that the user is permitted to store any data they wish,
2626/// as long as it can be represented by bytes. (Which is to say, pretty much anything!)
2727///
28- /// This is a *wrapper type* that implements `Deref<Target=[u8]>` so it can be used like one transparently.
29- ///
3028/// This type also implements `From` for many types. With one exception, these are all done without
3129/// reallocation. Using a `&'static str`, like many examples do for simplicity, has an internal
3230/// allocation cost.
@@ -60,6 +58,11 @@ impl<'a> fmt::Display for HexRepr<'a> {
6058pub struct Key ( Vec < u8 > ) ;
6159
6260impl Key {
61+ #[ inline]
62+ pub fn is_empty ( & self ) -> bool {
63+ self . 0 . is_empty ( )
64+ }
65+
6366 #[ inline]
6467 fn zero_terminated ( & self ) -> bool {
6568 self . 0 . last ( ) . map ( |i| * i == 0 ) . unwrap_or ( false )
@@ -109,50 +112,18 @@ impl From<&'static str> for Key {
109112 }
110113}
111114
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-
136115impl Into < Vec < u8 > > for Key {
137116 fn into ( self ) -> Vec < u8 > {
138117 self . 0
139118 }
140119}
141120
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 ] {
146123 & self . 0
147124 }
148125}
149126
150- impl DerefMut for Key {
151- fn deref_mut ( & mut self ) -> & mut [ u8 ] {
152- & mut self . 0
153- }
154- }
155-
156127impl fmt:: Debug for Key {
157128 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
158129 write ! ( f, "Key({})" , HexRepr ( & self . 0 ) )
@@ -165,8 +136,6 @@ impl fmt::Debug for Key {
165136/// as valid `UTF-8` is not required. This means that the user is permitted to store any data they wish,
166137/// as long as it can be represented by bytes. (Which is to say, pretty much anything!)
167138///
168- /// This is a *wrapper type* that implements `Deref<Target=[u8]>` so it can be used like one transparently.
169- ///
170139/// This type also implements `From` for many types. With one exception, these are all done without
171140/// reallocation. Using a `&'static str`, like many examples do for simplicity, has an internal
172141/// allocation cost.
@@ -199,6 +168,13 @@ impl fmt::Debug for Key {
199168#[ derive( new, Default , Clone , Eq , PartialEq , Hash ) ]
200169pub struct Value ( Vec < u8 > ) ;
201170
171+ impl Value {
172+ #[ inline]
173+ pub fn is_empty ( & self ) -> bool {
174+ self . 0 . is_empty ( )
175+ }
176+ }
177+
202178impl From < Vec < u8 > > for Value {
203179 fn from ( v : Vec < u8 > ) -> Self {
204180 Value ( v)
@@ -223,16 +199,8 @@ impl Into<Vec<u8>> for Value {
223199 }
224200}
225201
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 ] {
236204 & self . 0
237205 }
238206}
@@ -335,9 +303,9 @@ impl Into<(Key, Value)> for KvPair {
335303impl fmt:: Debug for KvPair {
336304 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
337305 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 ) ) ,
341309 }
342310 }
343311}
0 commit comments