Trait phf_shared::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<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<UncasedStr> for &UncasedStr

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<[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]

Implementors§