1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! The error a query resolution returns when it cannot complete.
use fmt;
/// The error returned from [`Database::get`](crate::Database::get) and
/// [`System::compute`](crate::System::compute) when a query cannot be resolved.
///
/// The engine resolves a derived query by running its
/// [`compute`](crate::System::compute), which in turn reads other queries. That
/// recursion terminates only when the dependency graph is acyclic. If a query
/// asks — directly or through a chain of other queries — for a result that is
/// itself still being computed, there is no value to return and no way to make
/// progress: the queries form a cycle. Rather than recurse without bound or
/// panic, the engine unwinds the whole chain with [`QueryError::Cycle`].
///
/// The type is [`non_exhaustive`](https://doc.rust-lang.org/reference/attributes/type_system.html):
/// resolution has exactly one failure mode today, and new engine-level failure
/// modes can be added later without breaking a `match` that already handles
/// `Cycle`.
///
/// # Examples
///
/// A query that reads itself cannot resolve:
///
/// ```
/// use query_lang::{Database, System, QueryError};
///
/// struct SelfReferential;
/// impl System for SelfReferential {
/// type Key = u32;
/// type Value = u32;
/// fn compute(&self, db: &Database<Self>, key: &u32) -> Result<u32, QueryError> {
/// // Asking for the very key being computed closes a cycle.
/// db.get(key)
/// }
/// }
///
/// let db = Database::new(SelfReferential);
/// assert_eq!(db.get(&1), Err(QueryError::Cycle));
/// ```