Skip to content

Implement AsRef<[u8]>, Borrow<[u8]>, and Deref<Target=[u8]> for Key #494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/kv/key.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

use std::borrow::Borrow;
use std::fmt;
use std::ops::Bound;
use std::ops::{Bound, Deref};

#[allow(unused_imports)]
#[cfg(test)]
Expand Down Expand Up @@ -156,6 +157,26 @@ impl Key {
}
}

impl AsRef<[u8]> for Key {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}

impl Borrow<[u8]> for Key {
fn borrow(&self) -> &[u8] {
self.0.borrow()
}
}

impl Deref for Key {
type Target = [u8];

fn deref(&self) -> &Self::Target {
self.0.deref()
}
}

impl From<Vec<u8>> for Key {
fn from(v: Vec<u8>) -> Self {
Key(v)
Expand Down Expand Up @@ -185,6 +206,7 @@ impl<'a> From<&'a Vec<u8>> for &'a Key {
unsafe { &*(key as *const Vec<u8> as *const Key) }
}
}

impl AsRef<Key> for Key {
fn as_ref(&self) -> &Key {
self
Expand Down
6 changes: 3 additions & 3 deletions src/region_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<C: RetryClientTrait> RegionCache<C> {
let res = {
region_cache_guard
.key_to_ver_id
.range(..=key)
.range::<Key, _>(..=key)
.next_back()
.map(|(x, y)| (x.clone(), y.clone()))
};
Expand Down Expand Up @@ -180,9 +180,9 @@ impl<C: RetryClientTrait> RegionCache<C> {

let mut search_range = {
if end_key.is_empty() {
cache.key_to_ver_id.range(..)
cache.key_to_ver_id.range::<Key, _>(..)
} else {
cache.key_to_ver_id.range(..end_key)
cache.key_to_ver_id.range::<Key, _>(..end_key)
}
};
while let Some((_, ver_id_in_cache)) = search_range.next_back() {
Expand Down