Add PyItem container
Also reduce queue item trait bound from Ord to just PartialOrd
This commit is contained in:
parent
ee004bac19
commit
38a544db76
8 changed files with 91 additions and 32 deletions
|
@ -1,8 +1,7 @@
|
|||
use std::cmp::Ordering;
|
||||
|
||||
/// Helper struct to associate an item with its priority
|
||||
/// Container to associate an item with a 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,
|
||||
|
@ -14,23 +13,13 @@ impl<D: Clone, P: PartialOrd + Clone> Pair<D, P> {
|
|||
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
|
||||
/// 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
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
@ -42,5 +31,3 @@ impl<D: Clone, P: PartialOrd + Clone> PartialEq for Pair<D, P> {
|
|||
self.priority == other.priority
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Clone, P: PartialOrd + Clone> Eq for Pair<D, P> {}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue