phf_shared

Trait PhfBorrow

Source
pub trait PhfBorrow<B: ?Sized> {
    // Required method
    fn borrow(&self) -> &B;
}
Expand description

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:

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§

Source

fn borrow(&self) -> &B

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

Implementations on Foreign Types§

Source§

impl PhfBorrow<bool> for bool

Source§

fn borrow(&self) -> &bool

Source§

impl PhfBorrow<char> for char

Source§

fn borrow(&self) -> &char

Source§

impl PhfBorrow<i8> for i8

Source§

fn borrow(&self) -> &i8

Source§

impl PhfBorrow<i16> for i16

Source§

fn borrow(&self) -> &i16

Source§

impl PhfBorrow<i32> for i32

Source§

fn borrow(&self) -> &i32

Source§

impl PhfBorrow<i64> for i64

Source§

fn borrow(&self) -> &i64

Source§

impl PhfBorrow<i128> for i128

Source§

fn borrow(&self) -> &i128

Source§

impl PhfBorrow<isize> for isize

Source§

fn borrow(&self) -> &isize

Source§

impl PhfBorrow<str> for str

Source§

fn borrow(&self) -> &str

Source§

impl PhfBorrow<str> for String

Source§

fn borrow(&self) -> &str

Source§

impl PhfBorrow<u8> for u8

Source§

fn borrow(&self) -> &u8

Source§

impl PhfBorrow<u16> for u16

Source§

fn borrow(&self) -> &u16

Source§

impl PhfBorrow<u32> for u32

Source§

fn borrow(&self) -> &u32

Source§

impl PhfBorrow<u64> for u64

Source§

fn borrow(&self) -> &u64

Source§

impl PhfBorrow<u128> for u128

Source§

fn borrow(&self) -> &u128

Source§

impl PhfBorrow<usize> for usize

Source§

fn borrow(&self) -> &usize

Source§

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

Source§

fn borrow(&self) -> &[u8]

Source§

impl PhfBorrow<[u8]> for [u8]

Source§

fn borrow(&self) -> &[u8]

Source§

impl<'a> PhfBorrow<str> for &'a str

Source§

fn borrow(&self) -> &str

Source§

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

Source§

fn borrow(&self) -> &[u8]

Source§

impl<const N: usize> PhfBorrow<[bool]> for [bool; N]

Source§

fn borrow(&self) -> &[bool]

Source§

impl<const N: usize> PhfBorrow<[char]> for [char; N]

Source§

fn borrow(&self) -> &[char]

Source§

impl<const N: usize> PhfBorrow<[i8]> for [i8; N]

Source§

fn borrow(&self) -> &[i8]

Source§

impl<const N: usize> PhfBorrow<[i16]> for [i16; N]

Source§

fn borrow(&self) -> &[i16]

Source§

impl<const N: usize> PhfBorrow<[i32]> for [i32; N]

Source§

fn borrow(&self) -> &[i32]

Source§

impl<const N: usize> PhfBorrow<[i64]> for [i64; N]

Source§

fn borrow(&self) -> &[i64]

Source§

impl<const N: usize> PhfBorrow<[i128]> for [i128; N]

Source§

fn borrow(&self) -> &[i128]

Source§

impl<const N: usize> PhfBorrow<[isize]> for [isize; N]

Source§

fn borrow(&self) -> &[isize]

Source§

impl<const N: usize> PhfBorrow<[u8]> for [u8; N]

Source§

fn borrow(&self) -> &[u8]

Source§

impl<const N: usize> PhfBorrow<[u16]> for [u16; N]

Source§

fn borrow(&self) -> &[u16]

Source§

impl<const N: usize> PhfBorrow<[u32]> for [u32; N]

Source§

fn borrow(&self) -> &[u32]

Source§

impl<const N: usize> PhfBorrow<[u64]> for [u64; N]

Source§

fn borrow(&self) -> &[u64]

Source§

impl<const N: usize> PhfBorrow<[u128]> for [u128; N]

Source§

fn borrow(&self) -> &[u128]

Source§

impl<const N: usize> PhfBorrow<[usize]> for [usize; N]

Source§

fn borrow(&self) -> &[usize]

Implementors§