Require Ord for ComparableDoc.

This commit is contained in:
Stu Hood
2025-12-16 15:25:46 -08:00
parent b91d86da63
commit 35c9eb6da2
2 changed files with 5 additions and 7 deletions

View File

@@ -12,7 +12,7 @@ pub trait Comparator<T>: Send + Sync + std::fmt::Debug + Default {
fn compare(&self, lhs: &T, rhs: &T) -> Ordering;
/// Return the order between two ComparableDoc values.
#[inline(always)]
fn compare_doc<D: PartialOrd>(
fn compare_doc<D: Ord>(
&self,
lhs: &ComparableDoc<T, D>,
rhs: &ComparableDoc<T, D>,
@@ -21,7 +21,7 @@ pub trait Comparator<T>: Send + Sync + std::fmt::Debug + Default {
// In case of a tie on the feature, we always sort by descending `DocAddress` in order
// to ensure a stable sorting of the documents. See the TopNComputer docs for more
// information.
rhs.doc.partial_cmp(&lhs.doc).unwrap_or(Ordering::Equal)
rhs.doc.cmp(&lhs.doc)
})
}
}

View File

@@ -6,17 +6,15 @@ use serde::{Deserialize, Serialize};
#[derive(Clone, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct ComparableDoc<T, D> {
/// The feature of the document. In practice, this is
/// is any type that implements `PartialOrd`.
/// is a type which can be compared with a `Comparator<T>`.
pub sort_key: T,
/// The document address. In practice, this is any
/// type that implements `PartialOrd`, and is guaranteed
/// to be unique for each document.
/// The document address. In practice, this is either a `DocId` or `DocAddress`.
pub doc: D,
}
impl<T: std::fmt::Debug, D: std::fmt::Debug> std::fmt::Debug for ComparableDoc<T, D> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct(format!("ComparableDoc").as_str())
f.debug_struct("ComparableDoc")
.field("feature", &self.sort_key)
.field("doc", &self.doc)
.finish()