Skip to content

Commit 5236eba

Browse files
KerollmopsHoverbear
authored andcommitted
AsRef/Mut and From traits implementation on Key and Value (#26)
* Remove static lifetime restrictions on Key/Value From impl Signed-off-by: Clément Renault <[email protected]> * Add AsMut impl to Value and Key Signed-off-by: Clément Renault <[email protected]> * Allow constructing Key and Value from static str Signed-off-by: Clément Renault <[email protected]>
1 parent 4e9a42f commit 5236eba

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/lib.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,20 @@ impl From<String> for Key {
220220
}
221221
}
222222

223-
impl<'a> From<&'static str> for Key {
223+
impl From<&'static str> for Key {
224224
fn from(v: &'static str) -> Key {
225-
let mut vec = Vec::new();
226-
vec.extend_from_slice(v.as_bytes());
227-
Key(vec)
225+
Key(v.as_bytes().to_vec())
228226
}
229227
}
230228

231229
impl AsRef<Key> for Key {
232-
fn as_ref(&self) -> &Self {
230+
fn as_ref(&self) -> &Key {
231+
self
232+
}
233+
}
234+
235+
impl AsMut<Key> for Key {
236+
fn as_mut(&mut self) -> &mut Key {
233237
self
234238
}
235239
}
@@ -240,6 +244,12 @@ impl AsRef<[u8]> for Key {
240244
}
241245
}
242246

247+
impl AsMut<[u8]> for Key {
248+
fn as_mut(&mut self) -> &mut [u8] {
249+
&mut self.0
250+
}
251+
}
252+
243253
impl Deref for Key {
244254
type Target = [u8];
245255

@@ -326,9 +336,7 @@ impl From<String> for Value {
326336

327337
impl From<&'static str> for Value {
328338
fn from(v: &'static str) -> Value {
329-
let mut vec = Vec::new();
330-
vec.extend_from_slice(v.as_bytes());
331-
Value(vec)
339+
Value(v.as_bytes().to_vec())
332340
}
333341
}
334342

@@ -340,12 +348,19 @@ impl Deref for Value {
340348
}
341349
}
342350

351+
impl AsRef<[u8]> for Value {
352+
fn as_ref(&self) -> &[u8] {
353+
&self.0
354+
}
355+
}
356+
343357
impl fmt::Debug for Value {
344358
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
345359
match str::from_utf8(&self.0) {
346360
Ok(s) => write!(f, "Value({:?})", s),
347361
Err(_) => write!(f, "Value({})", HexRepr(&self.0)),
348362
}
363+
349364
}
350365
}
351366

0 commit comments

Comments
 (0)