salsa/
debug.rs

1//! Debugging APIs: these are meant for use when unit-testing or
2//! debugging your application but aren't ordinarily needed.
3
4use crate::durability::Durability;
5use crate::plumbing::QueryStorageOps;
6use crate::Query;
7use crate::QueryTable;
8use std::iter::FromIterator;
9
10/// Additional methods on queries that can be used to "peek into"
11/// their current state. These methods are meant for debugging and
12/// observing the effects of garbage collection etc.
13pub trait DebugQueryTable {
14    /// Key of this query.
15    type Key;
16
17    /// Value of this query.
18    type Value;
19
20    /// Returns a lower bound on the durability for the given key.
21    /// This is typically the minimum durability of all values that
22    /// the query accessed, but we may return a lower durability in
23    /// some cases.
24    fn durability(&self, key: Self::Key) -> Durability;
25
26    /// Get the (current) set of the entries in the query table.
27    fn entries<C>(&self) -> C
28    where
29        C: FromIterator<TableEntry<Self::Key, Self::Value>>;
30}
31
32/// An entry from a query table, for debugging and inspecting the table state.
33#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
34#[non_exhaustive]
35pub struct TableEntry<K, V> {
36    /// key of the query
37    pub key: K,
38    /// value of the query, if it is stored
39    pub value: Option<V>,
40}
41
42impl<K, V> TableEntry<K, V> {
43    pub(crate) fn new(key: K, value: Option<V>) -> TableEntry<K, V> {
44        TableEntry { key, value }
45    }
46}
47
48impl<'d, Q> DebugQueryTable for QueryTable<'_, Q>
49where
50    Q: Query,
51    Q::Storage: QueryStorageOps<Q>,
52{
53    type Key = Q::Key;
54    type Value = Q::Value;
55
56    fn durability(&self, key: Q::Key) -> Durability {
57        self.storage.durability(self.db, &key)
58    }
59
60    fn entries<C>(&self) -> C
61    where
62        C: FromIterator<TableEntry<Self::Key, Self::Value>>,
63    {
64        self.storage.entries(self.db)
65    }
66}