composable_indexes_core/
index.rs

1use crate::collection::{Insert, Key, Remove, Update};
2use std::collections::HashMap;
3
4/// Trait of indexes. You probably only need this if you're implementing a new index.
5pub trait Index<In> {
6    type Query<'t, Out>
7    where
8        Self: 't,
9        Out: 't;
10
11    #[doc(hidden)]
12    fn insert(&mut self, op: &Insert<In>);
13
14    #[doc(hidden)]
15    fn remove(&mut self, op: &Remove<In>);
16
17    #[doc(hidden)]
18    fn update(&mut self, op: &Update<In>) {
19        self.remove(&Remove {
20            key: op.key,
21            existing: op.existing,
22        });
23        self.insert(&Insert {
24            key: op.key,
25            new: op.new,
26        });
27    }
28
29    #[doc(hidden)]
30    fn query<'t, Out: 't>(&'t self, env: QueryEnv<'t, Out>) -> Self::Query<'t, Out>;
31}
32
33pub struct QueryEnv<'t, T> {
34    pub(crate) data: &'t HashMap<Key, T>,
35}
36
37impl<'t, T> QueryEnv<'t, T> {
38    pub fn get(&'t self, key: &Key) -> &'t T {
39        self.data.get(key).unwrap()
40    }
41
42    pub fn get_opt(&'t self, key: &Key) -> Option<&'t T> {
43        self.data.get(key)
44    }
45}
46
47impl<T> Clone for QueryEnv<'_, T> {
48    fn clone(&self) -> Self {
49        QueryEnv { data: self.data }
50    }
51}