Skip to main content

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#[derive(Debug, Clone)]
31pub struct NearestResult<T: SpatialItem> {
32    pub item: T,
33    pub distance: f64,
34}
35
36#[derive(Debug, Clone)]
37pub struct SpatialQueryResult<T: SpatialItem> {
38    pub items: Vec<T>,
39    pub total_scanned: usize,
40}