pyority_queue/src/backing/containers/pair.rs
Michael Bradley 38a544db76
Add PyItem container
Also reduce queue item trait bound from Ord to just PartialOrd
2025-01-10 20:53:28 +13:00

33 lines
914 B
Rust

use std::cmp::Ordering;
/// Container to associate an item with a priority
#[derive(Debug, Clone, Copy)]
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 }
}
/// Retrieves 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
}
}
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
}
}