-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supplant the
Borrow
bound on keys with a new AsKey
trait
QP-trie keys are now bound by a new public trait, `AsKey`, rather than `Borrow<[u8]>`. AsKey` types must be borrowable both as a key slice, much like in `Borrow`, and as `&[u8]`, which is used internally for nybble operations. The trait is implemented for common `std` types, roughly matching prior coverage.
- Loading branch information
tapeinosyne
committed
Oct 4, 2017
1 parent
e4e7c94
commit 2b65501
Showing
9 changed files
with
322 additions
and
443 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
use std::borrow::Borrow; | ||
|
||
|
||
/// A trait for keys in a QP-trie. | ||
/// | ||
/// Implementing types must be borrowable in the form of both a key slice, | ||
/// such as `&str`, and the plain byte slice `&[u8]`. The former is used in | ||
/// the public `trie::Trie` API, while the latter is used internally to match | ||
/// and store keys. | ||
/// | ||
/// Note that, as a consequence, keys which are not bytewise-equivalent will | ||
/// not associate to the same entry, even if they are equal under `Eq`. | ||
pub trait AsKey { | ||
/// The borrowed form of this key type. | ||
type Borrowed: ?Sized; | ||
|
||
/// Borrow this key as a slice. | ||
fn as_key_slice(&self) -> &Self::Borrowed; | ||
|
||
/// View the key slice as a plain byte sequence. | ||
fn as_key_bytes(key: &Self::Borrowed) -> &[u8]; | ||
|
||
/// Borrow the key as nybbles, in the form of a plain byte sequence. | ||
#[inline] | ||
fn as_nybbles(&self) -> &[u8] { | ||
Self::as_key_bytes(self.as_key_slice()) | ||
} | ||
|
||
/// View the nybbles of a value that can borrowed as a key slice. | ||
#[inline] | ||
fn nybbles_from<'a, Q: 'a + ?Sized>(val: &'a Q) -> &'a [u8] | ||
where | ||
Self::Borrowed: 'a, | ||
Q: Borrow<Self::Borrowed>, | ||
{ | ||
Self::as_key_bytes(val.borrow()) | ||
} | ||
} | ||
|
||
|
||
impl<'a> AsKey for &'a [u8] { | ||
type Borrowed = [u8]; | ||
|
||
#[inline] | ||
fn as_key_slice(&self) -> &Self::Borrowed { | ||
self | ||
} | ||
|
||
#[inline] | ||
fn as_key_bytes(key: &Self::Borrowed) -> &[u8] { | ||
key | ||
} | ||
} | ||
|
||
|
||
impl AsKey for Vec<u8> { | ||
type Borrowed = [u8]; | ||
|
||
#[inline] | ||
fn as_key_slice(&self) -> &Self::Borrowed { | ||
self.as_slice() | ||
} | ||
|
||
#[inline] | ||
fn as_key_bytes(key: &Self::Borrowed) -> &[u8] { | ||
key | ||
} | ||
} | ||
|
||
|
||
impl<'a> AsKey for &'a str { | ||
type Borrowed = str; | ||
|
||
#[inline] | ||
fn as_key_slice(&self) -> &Self::Borrowed { | ||
self | ||
} | ||
|
||
#[inline] | ||
fn as_key_bytes(key: &Self::Borrowed) -> &[u8] { | ||
key.as_bytes() | ||
} | ||
} | ||
|
||
|
||
impl AsKey for String { | ||
type Borrowed = str; | ||
|
||
#[inline] | ||
fn as_key_slice(&self) -> &Self::Borrowed { | ||
self.as_str() | ||
} | ||
|
||
#[inline] | ||
fn as_key_bytes(key: &Self::Borrowed) -> &[u8] { | ||
key.as_bytes() | ||
} | ||
} | ||
|
||
|
||
pub trait Break: AsKey { | ||
fn empty<'a>() -> &'a <Self as AsKey>::Borrowed; | ||
fn find_break(&self, loc: usize) -> &<Self as AsKey>::Borrowed; | ||
} | ||
|
||
|
||
impl<'b> Break for &'b [u8] { | ||
#[inline] | ||
fn empty<'a>() -> &'a [u8] { | ||
<&'a [u8]>::default() | ||
} | ||
|
||
#[inline] | ||
fn find_break(&self, loc: usize) -> &[u8] { | ||
&self[..loc] | ||
} | ||
} | ||
|
||
|
||
impl Break for Vec<u8> { | ||
#[inline] | ||
fn empty<'a>() -> &'a [u8] { | ||
<&'a [u8]>::default() | ||
} | ||
|
||
#[inline] | ||
fn find_break(&self, loc: usize) -> &[u8] { | ||
&self[..loc] | ||
} | ||
} | ||
|
||
|
||
impl<'b> Break for &'b str { | ||
#[inline] | ||
fn empty<'a>() -> &'a str { | ||
<&'a str>::default() | ||
} | ||
|
||
#[inline] | ||
fn find_break(&self, mut loc: usize) -> &str { | ||
while !self.is_char_boundary(loc) { | ||
loc -= 1; | ||
} | ||
|
||
&self[..loc] | ||
} | ||
} | ||
|
||
|
||
impl Break for String { | ||
#[inline] | ||
fn empty<'a>() -> &'a str { | ||
<&'a str>::default() | ||
} | ||
|
||
#[inline] | ||
fn find_break(&self, mut loc: usize) -> &str { | ||
while !self.is_char_boundary(loc) { | ||
loc -= 1; | ||
} | ||
|
||
&self[..loc] | ||
} | ||
} | ||
|
||
|
||
macro_rules! impl_for_arrays_of_size { | ||
($($length:expr)+) => { $( | ||
impl AsKey for [u8; $length] { | ||
type Borrowed = [u8]; | ||
|
||
#[inline] | ||
fn as_key_slice(&self) -> &Self::Borrowed { | ||
self.as_ref() | ||
} | ||
|
||
#[inline] | ||
fn as_key_bytes(key: &Self::Borrowed) -> &[u8] { | ||
key | ||
} | ||
} | ||
|
||
impl Break for [u8; $length] { | ||
#[inline] | ||
fn empty<'a>() -> &'a [u8] { | ||
<&'a [u8]>::default() | ||
} | ||
|
||
#[inline] | ||
fn find_break(&self, loc: usize) -> &[u8] { | ||
&self.as_key_slice()[..loc] | ||
} | ||
} | ||
)+ } | ||
} | ||
|
||
|
||
impl_for_arrays_of_size! { | ||
0 1 2 3 4 5 6 7 8 9 | ||
10 11 12 13 14 15 16 17 18 19 | ||
20 21 22 23 24 25 26 27 28 29 | ||
30 31 32 | ||
} | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.