Rename Item -> containers::Pair

This commit is contained in:
Michael Bradley 2025-01-10 20:02:52 +13:00
parent e424dd42f5
commit ee004bac19
Signed by: MichaelBradley
SSH key fingerprint: SHA256:cj/YZ5VT+QOKncqSkx+ibKTIn0Obg7OIzwzl9BL8EO8
5 changed files with 20 additions and 17 deletions

View file

@ -0,0 +1,3 @@
mod pair;
pub use pair::Pair;

View file

@ -0,0 +1,46 @@
use std::cmp::Ordering;
/// Helper struct to associate an item with its priority
#[derive(Debug, Clone, Copy)]
// I mean I guess P should be Ord but I want to use f64 so whatever
pub struct Pair<D: Clone, P: PartialOrd + Clone> {
data: D,
priority: P,
}
impl<D: Clone, P: PartialOrd + Clone> Pair<D, P> {
/// Creates a new instance
pub fn new(data: D, priority: P) -> Self {
Self { data, priority }
}
/// Retrieve the internal data, it would be nicer to implement this using [`From`] or [`Into`], but I don't see a way to do that using generics
pub fn data(self) -> D {
self.data
}
}
// The relevant Ord implementations are based just on the priority
impl<D: Clone, P: PartialOrd + Clone> Ord for Pair<D, P> {
fn cmp(&self, other: &Self) -> Ordering {
// Yeah this is bad design
// My excuse is that i'm still learning Rust
self.priority
.partial_cmp(&other.priority)
.unwrap_or(Ordering::Equal)
}
}
impl<D: Clone, P: PartialOrd + Clone> PartialOrd for Pair<D, P> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.priority.partial_cmp(&other.priority)
}
}
impl<D: Clone, P: PartialOrd + Clone> PartialEq for Pair<D, P> {
fn eq(&self, other: &Self) -> bool {
self.priority == other.priority
}
}
impl<D: Clone, P: PartialOrd + Clone> Eq for Pair<D, P> {}