Trait IdOrdItem

Source
pub trait IdOrdItem {
    type Key<'a>: Ord
       where Self: 'a;

    // Required methods
    fn key(&self) -> Self::Key<'_>;
    fn upcast_key<'short, 'long: 'short>(
        long: Self::Key<'long>,
    ) -> Self::Key<'short>;
}
Expand description

An element stored in an IdOrdMap.

This trait is used to define the key type for the map.

§Examples

use iddqd::{IdOrdItem, IdOrdMap, id_upcast};

// Define a struct with a key.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct MyItem {
    id: String,
    value: u32,
}

// Implement IdOrdItem for the struct.
impl IdOrdItem for MyItem {
    // Keys can borrow from the item.
    type Key<'a> = &'a str;

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

    id_upcast!();
}

// Create an IdOrdMap and insert items.
let mut map = IdOrdMap::new();
map.insert_unique(MyItem { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(MyItem { id: "bar".to_string(), value: 20 }).unwrap();

Required Associated Types§

Source

type Key<'a>: Ord where Self: 'a

The key type.

Required Methods§

Source

fn key(&self) -> Self::Key<'_>

Retrieves the key.

Source

fn upcast_key<'short, 'long: 'short>( long: Self::Key<'long>, ) -> Self::Key<'short>

Upcasts the key to a shorter lifetime, in effect asserting that the lifetime 'a on IdOrdItem::Key is covariant.

Typically implemented via the id_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 + IdOrdItem> IdOrdItem for &'b T

Source§

type Key<'a> = <T as IdOrdItem>::Key<'a> where Self: 'a

Source§

fn key(&self) -> Self::Key<'_>

Source§

fn upcast_key<'short, 'long: 'short>( long: Self::Key<'long>, ) -> Self::Key<'short>
where Self: 'long,

Source§

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

Source§

type Key<'a> = <T as IdOrdItem>::Key<'a> where Self: 'a

Source§

fn key(&self) -> Self::Key<'_>

Source§

fn upcast_key<'short, 'long: 'short>( long: Self::Key<'long>, ) -> Self::Key<'short>
where Self: 'long,

Source§

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

Source§

type Key<'a> = <T as IdOrdItem>::Key<'a> where Self: 'a

Source§

fn key(&self) -> Self::Key<'_>

Source§

fn upcast_key<'short, 'long: 'short>( long: Self::Key<'long>, ) -> Self::Key<'short>

Source§

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

Source§

type Key<'a> = <T as IdOrdItem>::Key<'a> where Self: 'a

Source§

fn key(&self) -> Self::Key<'_>

Source§

fn upcast_key<'short, 'long: 'short>( long: Self::Key<'long>, ) -> Self::Key<'short>

Source§

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

Source§

type Key<'a> = <T as IdOrdItem>::Key<'a> where Self: 'a

Source§

fn key(&self) -> Self::Key<'_>

Source§

fn upcast_key<'short, 'long: 'short>( long: Self::Key<'long>, ) -> Self::Key<'short>

Implementors§