Skip to content

Commit 7bdaeae

Browse files
authored
Merge pull request #77 from nrc/deref
Remove `Deref` impls for Keys and Values
2 parents 2ea6ecc + eaff491 commit 7bdaeae

File tree

3 files changed

+22
-54
lines changed

3 files changed

+22
-54
lines changed

src/kv.rs

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use derive_new::new;
44
use std::cmp::{Eq, PartialEq};
55
use std::convert::TryFrom;
6-
use std::ops::{Bound, Deref, DerefMut, Range, RangeFrom, RangeInclusive};
6+
use std::ops::{Bound, Range, RangeFrom, RangeInclusive};
77
use std::{fmt, str, u8};
88

99
use 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> {
6058
pub struct Key(Vec<u8>);
6159

6260
impl 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-
136115
impl 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-
156127
impl 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)]
200169
pub 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+
202178
impl 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 {
335303
impl 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
}

src/rpc/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ impl RpcClient {
296296
}
297297

298298
fn get_region(&self, key: &Key) -> impl Future<Output = Result<Region>> {
299-
self.pd.get_region(key)
299+
self.pd.get_region(key.into())
300300
}
301301

302302
fn kv_client(&self, context: &RegionContext) -> Result<Arc<KvClient>> {

src/rpc/pd/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl Region {
3838
}
3939

4040
pub fn contains(&self, key: &Key) -> bool {
41-
let key: &[u8] = key.as_ref();
41+
let key: &[u8] = key.into();
4242
let start_key = self.region.get_start_key();
4343
let end_key = self.region.get_end_key();
4444
start_key <= key && (end_key > key || end_key.is_empty())

0 commit comments

Comments
 (0)