sphereql_index/item.rs
1use sphereql_core::SphericalPoint;
2use std::hash::Hash;
3
4/// Trait for items that can be stored in a [`SpatialIndex`](crate::SpatialIndex).
5///
6/// Implementors must provide an id and a spherical position.
7///
8/// ```
9/// use sphereql_index::SpatialItem;
10/// use sphereql_core::SphericalPoint;
11///
12/// #[derive(Debug, Clone)]
13/// struct MyItem { id: u32, pos: SphericalPoint }
14///
15/// impl SpatialItem for MyItem {
16/// type Id = u32;
17/// fn id(&self) -> &u32 { &self.id }
18/// fn position(&self) -> &SphericalPoint { &self.pos }
19/// }
20///
21/// let item = MyItem { id: 1, pos: SphericalPoint::new_unchecked(1.0, 0.5, 0.7) };
22/// assert_eq!(*item.id(), 1);
23/// ```
24pub trait SpatialItem: Clone + Send + Sync {
25 type Id: Eq + Hash + Clone + Send + Sync + std::fmt::Debug;
26 fn id(&self) -> &Self::Id;
27 fn position(&self) -> &SphericalPoint;
28}
29
30/// One result from a k-nearest-neighbor query.
31#[derive(Debug, Clone)]
32pub struct NearestResult<T: SpatialItem> {
33 pub item: T,
34 /// Angular distance in radians from the query point to this item.
35 pub distance: f64,
36}
37
38/// Result of a spatial region query.
39#[derive(Debug, Clone)]
40pub struct SpatialQueryResult<T: SpatialItem> {
41 pub items: Vec<T>,
42 /// Number of items inspected (including those that did not match). Useful for profiling index efficiency.
43 pub total_scanned: usize,
44}