Skip to main content

ferroui_dynamic_data/kernel/
item_with_index.rs

1//! `ItemWithIndex<T>` (`Kernel/ItemWithIndex.cs`).
2
3use std::fmt;
4use std::hash::{Hash, Hasher};
5
6/// An item and its index (`ItemWithIndex<T>`): equal and hashed by the item alone, as upstream's.
7#[derive(Clone, Copy, Debug)]
8pub struct ItemWithIndex<T> {
9    item: T,
10    index: usize,
11}
12
13impl<T> ItemWithIndex<T> {
14    /// The pair (`ItemWithIndex(T, int)`).
15    pub fn new(item: T, index: usize) -> Self {
16        Self { item, index }
17    }
18
19    /// The item (`Item`).
20    pub fn item(&self) -> &T {
21        &self.item
22    }
23
24    /// The index (`Index`).
25    pub fn index(&self) -> usize {
26        self.index
27    }
28
29    /// The item and the index.
30    pub fn into_parts(self) -> (T, usize) {
31        (self.item, self.index)
32    }
33}
34
35impl<T: PartialEq> PartialEq for ItemWithIndex<T> {
36    fn eq(&self, other: &Self) -> bool {
37        self.item == other.item
38    }
39}
40
41impl<T: Eq> Eq for ItemWithIndex<T> {}
42
43impl<T: Hash> Hash for ItemWithIndex<T> {
44    fn hash<H: Hasher>(&self, state: &mut H) {
45        self.item.hash(state);
46    }
47}
48
49impl<T: fmt::Display> fmt::Display for ItemWithIndex<T> {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        write!(f, "{} ({})", self.item, self.index)
52    }
53}