1#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct DocId(pub u32);
11
12pub const NO_MORE_DOCS: DocId = DocId(u32::MAX);
18
19impl DocId {
20 pub const fn new(id: u32) -> Self {
21 Self(id)
22 }
23
24 pub const fn as_u32(self) -> u32 {
25 self.0
26 }
27}
28
29impl std::fmt::Display for DocId {
30 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31 write!(f, "{}", self.0)
32 }
33}
34
35#[cfg(test)]
36mod tests {
37 use super::*;
38
39 #[test]
40 fn no_more_docs_is_greater_than_any_valid_doc_id() {
41 assert!(NO_MORE_DOCS > DocId(0));
42 assert!(NO_MORE_DOCS > DocId(u32::MAX - 1));
43 }
44
45 #[test]
46 fn ordering() {
47 assert!(DocId(0) < DocId(1));
48 assert!(DocId(1) < DocId(100));
49 assert!(DocId(100) < DocId(u32::MAX - 1));
50 }
51
52 #[test]
53 fn as_u32_round_trips() {
54 assert_eq!(DocId::new(42).as_u32(), 42);
55 assert_eq!(DocId(0).as_u32(), 0);
56 }
57
58 #[test]
59 fn display() {
60 assert_eq!(format!("{}", DocId(123)), "123");
61 }
62}