vecdb/traits/
index.rs

1use std::{fmt::Debug, ops::Add};
2
3use crate::PrintableIndex;
4
5/// Trait for types that can be used as vector indices.
6///
7/// This trait is automatically implemented for any type that satisfies the
8/// required bounds. No manual implementation is needed.
9pub trait VecIndex
10where
11    Self: Debug
12        + Default
13        + Copy
14        + Clone
15        + PartialEq
16        + Eq
17        + PartialOrd
18        + Ord
19        + From<usize>
20        + Into<usize>
21        + Add<usize, Output = Self>
22        + Send
23        + Sync
24        + PrintableIndex
25        + 'static,
26{
27    /// Converts this index to a `usize`.
28    #[inline]
29    fn to_usize(self) -> usize {
30        self.into()
31    }
32
33    /// Returns the previous index, or `None` if this is zero.
34    #[inline]
35    fn decremented(self) -> Option<Self> {
36        self.to_usize().checked_sub(1).map(Self::from)
37    }
38}
39
40impl<I> VecIndex for I where
41    I: Debug
42        + Default
43        + Copy
44        + Clone
45        + PartialEq
46        + Eq
47        + PartialOrd
48        + Ord
49        + From<usize>
50        + Into<usize>
51        + Add<usize, Output = Self>
52        + Send
53        + Sync
54        + PrintableIndex
55        + 'static
56{
57}