[][src]Trait phf_shared::PhfBorrow

pub trait PhfBorrow<B: ?Sized> {
    fn borrow(&self) -> &B;
}

Identical to std::borrow::Borrow except omitting blanket impls to facilitate other borrowing patterns.

The same semantic requirements apply:

In particular Eq, Ord and Hash must be equivalent for borrowed and owned values: x.borrow() == y.borrow() should give the same result as x == y.

(This crate's API only requires Eq and PhfHash, however.)

Motivation

The conventional signature for lookup methods on collections looks something like this:

This example is not tested
impl<K, V> Map<K, V> where K: PhfHash + Eq {
    fn get<T: ?Sized>(&self, key: &T) -> Option<&V> where T: PhfHash + Eq, K: Borrow<T> {
        ...
    }
}

This allows the key type used for lookup to be different than the key stored in the map so for example you can use &str to look up a value in a Map<String, _>. However, this runs into a problem in the case where T and K are both a Foo<_> type constructor but the contained type is different (even being the same type with different lifetimes).

The main issue for this crate's API is that, with this method signature, you cannot perform a lookup on a Map<UniCase<&'static str>, _> with a UniCase<&'a str> where 'a is not 'static; there is no impl of Borrow that resolves to impl Borrow<UniCase<'a>> for UniCase<&'static str> and one cannot be added either because of all the blanket impls.

Instead, this trait is implemented conservatively, without blanket impls, so that impls like this may be added. This is feasible since the set of types that implement PhfHash is intentionally small.

This likely won't be fixable with specialization alone but will require full support for lattice impls since we technically want to add overlapping blanket impls.

Required methods

fn borrow(&self) -> &B

Convert a reference to self to a reference to the borrowed type.

Loading content...

Implementations on Foreign Types

impl PhfBorrow<str> for str[src]

impl PhfBorrow<char> for char[src]

impl PhfBorrow<u8> for u8[src]

impl PhfBorrow<i8> for i8[src]

impl PhfBorrow<u16> for u16[src]

impl PhfBorrow<i16> for i16[src]

impl PhfBorrow<u32> for u32[src]

impl PhfBorrow<i32> for i32[src]

impl PhfBorrow<u64> for u64[src]

impl PhfBorrow<i64> for i64[src]

impl PhfBorrow<u128> for u128[src]

impl PhfBorrow<i128> for i128[src]

impl PhfBorrow<bool> for bool[src]

impl PhfBorrow<[u8]> for [u8][src]

impl PhfBorrow<str> for String[src]

impl PhfBorrow<[u8]> for Vec<u8>[src]

impl<'a> PhfBorrow<str> for &'a str[src]

impl<'a> PhfBorrow<[u8]> for &'a [u8][src]

impl PhfBorrow<[u8]> for [u8; 1][src]

impl PhfBorrow<[u8]> for [u8; 2][src]

impl PhfBorrow<[u8]> for [u8; 3][src]

impl PhfBorrow<[u8]> for [u8; 4][src]

impl PhfBorrow<[u8]> for [u8; 5][src]

impl PhfBorrow<[u8]> for [u8; 6][src]

impl PhfBorrow<[u8]> for [u8; 7][src]

impl PhfBorrow<[u8]> for [u8; 8][src]

impl PhfBorrow<[u8]> for [u8; 9][src]

impl PhfBorrow<[u8]> for [u8; 10][src]

impl PhfBorrow<[u8]> for [u8; 11][src]

impl PhfBorrow<[u8]> for [u8; 12][src]

impl PhfBorrow<[u8]> for [u8; 13][src]

impl PhfBorrow<[u8]> for [u8; 14][src]

impl PhfBorrow<[u8]> for [u8; 15][src]

impl PhfBorrow<[u8]> for [u8; 16][src]

impl PhfBorrow<[u8]> for [u8; 17][src]

impl PhfBorrow<[u8]> for [u8; 18][src]

impl PhfBorrow<[u8]> for [u8; 19][src]

impl PhfBorrow<[u8]> for [u8; 20][src]

impl PhfBorrow<[u8]> for [u8; 21][src]

impl PhfBorrow<[u8]> for [u8; 22][src]

impl PhfBorrow<[u8]> for [u8; 23][src]

impl PhfBorrow<[u8]> for [u8; 24][src]

impl PhfBorrow<[u8]> for [u8; 25][src]

impl PhfBorrow<[u8]> for [u8; 26][src]

impl PhfBorrow<[u8]> for [u8; 27][src]

impl PhfBorrow<[u8]> for [u8; 28][src]

impl PhfBorrow<[u8]> for [u8; 29][src]

impl PhfBorrow<[u8]> for [u8; 30][src]

impl PhfBorrow<[u8]> for [u8; 31][src]

impl PhfBorrow<[u8]> for [u8; 32][src]

Loading content...

Implementors

Loading content...