Trait TriHashItem

Source
pub trait TriHashItem {
    type K1<'a>: Eq + Hash
       where Self: 'a;
    type K2<'a>: Eq + Hash
       where Self: 'a;
    type K3<'a>: Eq + Hash
       where Self: 'a;

    // Required methods
    fn key1(&self) -> Self::K1<'_>;
    fn key2(&self) -> Self::K2<'_>;
    fn key3(&self) -> Self::K3<'_>;
    fn upcast_key1<'short, 'long: 'short>(
        long: Self::K1<'long>,
    ) -> Self::K1<'short>;
    fn upcast_key2<'short, 'long: 'short>(
        long: Self::K2<'long>,
    ) -> Self::K2<'short>;
    fn upcast_key3<'short, 'long: 'short>(
        long: Self::K3<'long>,
    ) -> Self::K3<'short>;
}
Expand description

An item in a TriHashMap.

This trait is used to define the keys.

§Examples

use iddqd::{TriHashItem, TriHashMap, tri_upcast};

// Define a struct with three keys.
#[derive(Debug, PartialEq, Eq, Hash)]
struct Person {
    id: u32,
    name: String,
    email: String,
}

// Implement TriHashItem for the struct.
impl TriHashItem for Person {
    type K1<'a> = u32;
    type K2<'a> = &'a str;
    type K3<'a> = &'a str;

    fn key1(&self) -> Self::K1<'_> {
        self.id
    }

    fn key2(&self) -> Self::K2<'_> {
        &self.name
    }

    fn key3(&self) -> Self::K3<'_> {
        &self.email
    }

    tri_upcast!();
}

// Create a TriHashMap and insert items.
let mut map = TriHashMap::new();
map.insert_unique(Person {
    id: 1,
    name: "Alice".to_string(),
    email: "alice@example.com".to_string(),
})
.unwrap();
map.insert_unique(Person {
    id: 2,
    name: "Bob".to_string(),
    email: "bob@example.com".to_string(),
})
.unwrap();

Required Associated Types§

Source

type K1<'a>: Eq + Hash where Self: 'a

The first key type.

Source

type K2<'a>: Eq + Hash where Self: 'a

The second key type.

Source

type K3<'a>: Eq + Hash where Self: 'a

The third key type.

Required Methods§

Source

fn key1(&self) -> Self::K1<'_>

Retrieves the first key.

Source

fn key2(&self) -> Self::K2<'_>

Retrieves the second key.

Source

fn key3(&self) -> Self::K3<'_>

Retrieves the third key.

Source

fn upcast_key1<'short, 'long: 'short>(long: Self::K1<'long>) -> Self::K1<'short>

Upcasts the first key to a shorter lifetime, in effect asserting that the lifetime 'a on TriHashItem::K1 is covariant.

Typically implemented via the tri_upcast macro.

Source

fn upcast_key2<'short, 'long: 'short>(long: Self::K2<'long>) -> Self::K2<'short>

Upcasts the second key to a shorter lifetime, in effect asserting that the lifetime 'a on TriHashItem::K2 is covariant.

Typically implemented via the tri_upcast macro.

Source

fn upcast_key3<'short, 'long: 'short>(long: Self::K3<'long>) -> Self::K3<'short>

Upcasts the third key to a shorter lifetime, in effect asserting that the lifetime 'a on TriHashItem::K3 is covariant.

Typically implemented via the tri_upcast macro.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<'b, T: 'b + ?Sized + TriHashItem> TriHashItem for &'b T

Source§

type K1<'a> = <T as TriHashItem>::K1<'a> where Self: 'a

Source§

type K2<'a> = <T as TriHashItem>::K2<'a> where Self: 'a

Source§

type K3<'a> = <T as TriHashItem>::K3<'a> where Self: 'a

Source§

fn key1(&self) -> Self::K1<'_>

Source§

fn key2(&self) -> Self::K2<'_>

Source§

fn key3(&self) -> Self::K3<'_>

Source§

fn upcast_key1<'short, 'long: 'short>(long: Self::K1<'long>) -> Self::K1<'short>
where Self: 'long,

Source§

fn upcast_key2<'short, 'long: 'short>(long: Self::K2<'long>) -> Self::K2<'short>
where Self: 'long,

Source§

fn upcast_key3<'short, 'long: 'short>(long: Self::K3<'long>) -> Self::K3<'short>
where Self: 'long,

Source§

impl<'b, T: 'b + ?Sized + TriHashItem> TriHashItem for &'b mut T

Source§

type K1<'a> = <T as TriHashItem>::K1<'a> where Self: 'a

Source§

type K2<'a> = <T as TriHashItem>::K2<'a> where Self: 'a

Source§

type K3<'a> = <T as TriHashItem>::K3<'a> where Self: 'a

Source§

fn key1(&self) -> Self::K1<'_>

Source§

fn key2(&self) -> Self::K2<'_>

Source§

fn key3(&self) -> Self::K3<'_>

Source§

fn upcast_key1<'short, 'long: 'short>(long: Self::K1<'long>) -> Self::K1<'short>
where Self: 'long,

Source§

fn upcast_key2<'short, 'long: 'short>(long: Self::K2<'long>) -> Self::K2<'short>
where Self: 'long,

Source§

fn upcast_key3<'short, 'long: 'short>(long: Self::K3<'long>) -> Self::K3<'short>
where Self: 'long,

Source§

impl<T: ?Sized + TriHashItem> TriHashItem for Box<T>

Source§

type K1<'a> = <T as TriHashItem>::K1<'a> where Self: 'a

Source§

type K2<'a> = <T as TriHashItem>::K2<'a> where Self: 'a

Source§

type K3<'a> = <T as TriHashItem>::K3<'a> where Self: 'a

Source§

fn key1(&self) -> Self::K1<'_>

Source§

fn key2(&self) -> Self::K2<'_>

Source§

fn key3(&self) -> Self::K3<'_>

Source§

fn upcast_key1<'short, 'long: 'short>(long: Self::K1<'long>) -> Self::K1<'short>

Source§

fn upcast_key2<'short, 'long: 'short>(long: Self::K2<'long>) -> Self::K2<'short>

Source§

fn upcast_key3<'short, 'long: 'short>(long: Self::K3<'long>) -> Self::K3<'short>

Source§

impl<T: ?Sized + TriHashItem> TriHashItem for Rc<T>

Source§

type K1<'a> = <T as TriHashItem>::K1<'a> where Self: 'a

Source§

type K2<'a> = <T as TriHashItem>::K2<'a> where Self: 'a

Source§

type K3<'a> = <T as TriHashItem>::K3<'a> where Self: 'a

Source§

fn key1(&self) -> Self::K1<'_>

Source§

fn key2(&self) -> Self::K2<'_>

Source§

fn key3(&self) -> Self::K3<'_>

Source§

fn upcast_key1<'short, 'long: 'short>(long: Self::K1<'long>) -> Self::K1<'short>

Source§

fn upcast_key2<'short, 'long: 'short>(long: Self::K2<'long>) -> Self::K2<'short>

Source§

fn upcast_key3<'short, 'long: 'short>(long: Self::K3<'long>) -> Self::K3<'short>

Source§

impl<T: ?Sized + TriHashItem> TriHashItem for Arc<T>

Source§

type K1<'a> = <T as TriHashItem>::K1<'a> where Self: 'a

Source§

type K2<'a> = <T as TriHashItem>::K2<'a> where Self: 'a

Source§

type K3<'a> = <T as TriHashItem>::K3<'a> where Self: 'a

Source§

fn key1(&self) -> Self::K1<'_>

Source§

fn key2(&self) -> Self::K2<'_>

Source§

fn key3(&self) -> Self::K3<'_>

Source§

fn upcast_key1<'short, 'long: 'short>(long: Self::K1<'long>) -> Self::K1<'short>

Source§

fn upcast_key2<'short, 'long: 'short>(long: Self::K2<'long>) -> Self::K2<'short>

Source§

fn upcast_key3<'short, 'long: 'short>(long: Self::K3<'long>) -> Self::K3<'short>

Implementors§