query-lang 1.0.0

Query-based incremental compilation framework.
Documentation
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
# query-lang — API Reference

> Complete reference for every public item in `query-lang`, with examples.
> **Status: stable (1.0).** The surface below is the `1.0` contract; it follows
> [Semantic Versioning]#stability and will not change in a breaking way before
> `2.0`. See [`../dev/ROADMAP.md`]../dev/ROADMAP.md.

<sub>Copyright &copy; 2026 <strong>James Gober</strong>.</sub>

## Table of contents

- [Overview]#overview
- [Installation]#installation
- [Quick start]#quick-start
- [The model]#the-model
- [`System`]#system
  - [`System::Key`]#systemkey
  - [`System::Value`]#systemvalue
  - [`System::compute`]#systemcompute
- [`Database`]#database
  - [`Database::new`]#databasenew
  - [`Database::set`]#databaseset
  - [`Database::get`]#databaseget
  - [`Database::revision`]#databaserevision
  - [`Database::stats`]#databasestats
  - [`Database::system`]#databasesystem
- [`Revision`]#revision
- [`Stats`]#stats
- [`QueryError`]#queryerror
- [Feature flags]#feature-flags
- [Stability]#stability

---

## Overview

query-lang is an incremental computation engine — the model behind `salsa` and
rust-analyzer, reduced to a small, dependency-free core. You describe a set of
queries once; the engine stores the base facts (**inputs**), caches the computed
results (**derived queries**), records what each result was read from, and
recomputes only what a change actually affects.

Four public types and one trait make up the whole surface:

| Item | Role |
|---|---|
| [`System`]#system | The trait you implement to define your queries. |
| [`Database`]#database | The engine: stores inputs, caches results, tracks dependencies. |
| [`Revision`]#revision | The version clock that drives validation. |
| [`Stats`]#stats | Cumulative counters for how the engine spent its work. |
| [`QueryError`]#queryerror | The error a resolution returns (a query cycle). |

The crate is `#![forbid(unsafe_code)]`, `no_std`-compatible (needs only `alloc`),
and wires no first-party dependency.

---

## Installation

```toml
[dependencies]
query-lang = "1"
```

Or from the terminal:

```bash
cargo add query-lang
```

MSRV: Rust 1.85 (Rust 2024 edition).

---

## Quick start

Define a system, set an input, and get a derived result. Ask again with no edit
and the result is a cache hit.

```rust
use query_lang::{Database, System, QueryError};

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
enum Key {
    Radius,        // an input
    Circumference, // = 2 * PI * Radius, in whole units
}

struct Circle;
impl System for Circle {
    type Key = Key;
    type Value = i64;
    fn compute(&self, db: &Database<Self>, key: &Key) -> Result<i64, QueryError> {
        match key {
            Key::Radius => Ok(0), // default if never set
            Key::Circumference => Ok(db.get(&Key::Radius)? * 628 / 100),
        }
    }
}

let mut db = Database::new(Circle);
db.set(Key::Radius, 10);
assert_eq!(db.get(&Key::Circumference)?, 62);

// No edit: the second query is a hit.
let before = db.stats().hits;
assert_eq!(db.get(&Key::Circumference)?, 62);
assert_eq!(db.stats().hits, before + 1);
# Ok::<(), QueryError>(())
```

---

## The model

A single `Key` type names every query in a system. A key is an **input** once its
value is placed into the database with [`Database::set`](#databaseset); every
other key is **derived**, and its value comes from [`compute`](#systemcompute). A
query reads an input and another derived query the same way — through
[`Database::get`](#databaseget) — and the engine records the dependency either
way.

Every resolution of a derived query takes one of three paths, counted in
[`Stats`](#stats):

- **Computed**`compute` ran (a cache miss, or a dependency that truly changed).
- **Validated** — the query was stale, but re-examining its dependencies showed
  none had changed its inputs, so the cached value was reused (*early cutoff*).
- **Hit** — the query was already current and returned immediately.

The engine never compares whole values to decide validity; it compares
[`Revision`](#revision) stamps, which is one integer compare regardless of value
size. Values are compared only at the moment a query recomputes, to decide
whether the new value differs from the old — the check that drives early cutoff.

---

## `System`

```rust,ignore
pub trait System: Sized {
    type Key: Clone + Ord;
    type Value: Clone + Eq;
    fn compute(&self, db: &Database<Self>, key: &Self::Key) -> Result<Self::Value, QueryError>;
}
```

The definition of a query system: how every derived query is computed. You
implement `System` once to describe an entire incremental computation. It ties
together the [`Key`](#systemkey) that names a query, the [`Value`](#systemvalue) a
query produces, and the [`compute`](#systemcompute) function that turns one into
the other. The [`Database`](#database) supplies everything else — caching,
dependency tracking, and invalidation.

The `System` value itself can hold immutable configuration a `compute` reads (a
grammar, a set of options); it is borrowed, not mutated, during resolution.

### `System::Key`

```rust,ignore
type Key: Clone + Ord;
```

The identifier that names a query, usually an `enum` with one variant per kind of
query (`Key::Source(FileId)`, `Key::Ast(FileId)`, `Key::TypeOf(DefId)`, …). Keys
are stored in the dependency graph and the memo table (a `BTreeMap`, hence `Ord`
rather than `Hash` — this keeps the engine `no_std`- and dependency-free).

Cloning a key should be cheap: prefer small copyable keys or interned identifiers
over owned strings.

```rust
use query_lang::{Database, System, QueryError};

// A key that names two kinds of query over a file id.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
enum Key {
    Source(u32),
    LineCount(u32),
}

struct Files;
impl System for Files {
    type Key = Key;
    type Value = usize;
    fn compute(&self, db: &Database<Self>, key: &Key) -> Result<usize, QueryError> {
        match key {
            Key::Source(_) => Ok(0),
            Key::LineCount(f) => Ok(db.get(&Key::Source(*f))?),
        }
    }
}

let mut db = Database::new(Files);
db.set(Key::Source(1), 42);
assert_eq!(db.get(&Key::LineCount(1))?, 42);
# Ok::<(), QueryError>(())
```

### `System::Value`

```rust,ignore
type Value: Clone + Eq;
```

The value a query produces. The engine clones a value to hand it back and
compares the new value against the old to decide whether a recomputation changed
anything — so it should be cheap to clone and compare. Wrap a large result in an
[`Arc`](https://doc.rust-lang.org/std/sync/struct.Arc.html) so a clone bumps a
refcount rather than copying, and equality short-circuits on pointer identity in
the common case.

```rust
use std::sync::Arc;
use query_lang::{Database, System, QueryError};

struct Parser;
impl System for Parser {
    type Key = u32;
    // A large parsed payload, shared rather than copied.
    type Value = Arc<Vec<u32>>;
    fn compute(&self, _db: &Database<Self>, file: &u32) -> Result<Arc<Vec<u32>>, QueryError> {
        Ok(Arc::new(vec![*file; 3]))
    }
}

let db = Database::new(Parser);
let a = db.get(&7)?;
let b = db.get(&7)?;             // a hit
assert!(Arc::ptr_eq(&a, &b));   // same allocation handed back
# Ok::<(), QueryError>(())
```

### `System::compute`

```rust,ignore
fn compute(&self, db: &Database<Self>, key: &Self::Key) -> Result<Self::Value, QueryError>;
```

Compute the value of a derived query. The engine calls this only on a cache miss
or when a dependency has genuinely changed — never for a key that is currently a
set input, and never when a cached value is still valid.

**Parameters**

- `&self` — the query system, for reading immutable configuration.
- `db` — the database handle. Read every dependency through it (`db.get(&other)`)
  so the engine can track the edge.
- `key` — the query to compute.

**Contract.** `compute` must be a pure function of the queries it reads. It must
read *every* value it depends on through `db` — a value pulled in from outside (a
global, the clock, a direct file read) is invisible to the engine and will not
trigger invalidation when it changes, leaving the cache serving stale results.
Given the same inputs, `compute` must return the same value.

**Errors.** Returns [`QueryError::Cycle`](#queryerror) if resolving a dependency
closes a cycle back onto a query still being computed. Propagate it with `?`; do
not try to recover from it inside `compute`, as the whole resolution chain is
already unwinding.

Reading one dependency:

```rust
use query_lang::{Database, System, QueryError};

struct Doubler;
impl System for Doubler {
    type Key = u32;
    type Value = i64;
    fn compute(&self, db: &Database<Self>, key: &u32) -> Result<i64, QueryError> {
        // key 0 is an input; every other key doubles key 0.
        if *key == 0 {
            Ok(0)
        } else {
            Ok(db.get(&0)? * 2)
        }
    }
}

let mut db = Database::new(Doubler);
db.set(0, 21);
assert_eq!(db.get(&1)?, 42);
# Ok::<(), QueryError>(())
```

Reading several dependencies, and branching on one — only the branch actually
taken becomes a dependency:

```rust
use query_lang::{Database, System, QueryError};

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
enum Key { Flag, A, B, Chosen }

struct Switch;
impl System for Switch {
    type Key = Key;
    type Value = i64;
    fn compute(&self, db: &Database<Self>, key: &Key) -> Result<i64, QueryError> {
        match key {
            Key::Flag | Key::A | Key::B => Ok(0),
            Key::Chosen => {
                if db.get(&Key::Flag)? != 0 { db.get(&Key::A) } else { db.get(&Key::B) }
            }
        }
    }
}

let mut db = Database::new(Switch);
db.set(Key::Flag, 1);
db.set(Key::A, 100);
db.set(Key::B, 200);
assert_eq!(db.get(&Key::Chosen)?, 100); // took the A branch; B is not a dependency
# Ok::<(), QueryError>(())
```

---

## `Database`

```rust,ignore
pub struct Database<S: System> { /* private */ }
```

The engine: the store of inputs and the cache of derived results, with automatic
dependency tracking and invalidation. Construct one with [`new`](#databasenew),
seed base facts with [`set`](#databaseset), and ask for results with
[`get`](#databaseget). It also exposes its [`revision`](#databaserevision) clock,
its cache [`stats`](#databasestats), and the [`system`](#databasesystem) it holds.

A `Database` is single-threaded by design: it is not `Sync`, since resolution
walks a shared cache and dependency stack through interior mutability. Drive one
database from one thread; run independent databases on separate threads for
parallelism.

### `Database::new`

```rust,ignore
pub fn new(system: S) -> Self
```

Create an empty database for the given query system. It starts at the initial
revision with no inputs and an empty cache.

```rust
use query_lang::{Database, System, QueryError};

struct S;
impl System for S {
    type Key = u32;
    type Value = u32;
    fn compute(&self, _db: &Database<Self>, k: &u32) -> Result<u32, QueryError> { Ok(*k) }
}

let db = Database::new(S);
assert_eq!(db.revision().as_u64(), 0);
assert_eq!(db.stats().total(), 0);
```

### `Database::set`

```rust,ignore
pub fn set(&mut self, key: S::Key, value: S::Value)
```

Set an input to a value. This is the only way a value enters the database from
outside. Once set, a key is an input: [`get`](#databaseget) returns it directly
and [`compute`](#systemcompute) is never called for it.

**Parameters**

- `key` — the input to set.
- `value` — its new value.

**Behaviour**

- Setting the **same** value a key already holds is a no-op — the revision does
  not advance and nothing that depends on the input is invalidated, so
  re-feeding unchanged facts costs nothing downstream.
- Setting a **different** value advances the [`revision`]#databaserevision,
  which is what later marks dependent queries stale.
- Setting a key that previously held a derived value promotes it to an input and
  discards the stale cached result.

Taking `&mut self` is deliberate: mutating an input is the one operation that can
invalidate cached results, so it is kept distinct from the shared `&self` reads
of [`get`](#databaseget).

```rust
use query_lang::{Database, System, QueryError};

struct S;
impl System for S {
    type Key = u32;
    type Value = i64;
    fn compute(&self, db: &Database<Self>, k: &u32) -> Result<i64, QueryError> {
        if *k == 0 { Ok(0) } else { Ok(db.get(&0)? + 1) }
    }
}

let mut db = Database::new(S);
db.set(0, 41);
let r0 = db.revision();

db.set(0, 41);               // same value
assert_eq!(db.revision(), r0);  // clock did not move

db.set(0, 99);               // new value
assert!(db.revision() > r0);    // clock advanced
assert_eq!(db.get(&1)?, 100);
# Ok::<(), QueryError>(())
```

### `Database::get`

```rust,ignore
pub fn get(&self, key: &S::Key) -> Result<S::Value, QueryError>
```

Resolve a query to its value, computing and caching it as needed. If `key` is a
set input, its value is returned directly. Otherwise the query is derived: a
valid cached value is reused (a hit or an early-cutoff validation), and only a
real miss or a genuinely changed dependency runs [`compute`](#systemcompute).

Call `get` both from application code and, from inside a `compute`, to read the
queries a result depends on — reads through `get` are exactly what the engine
records as dependencies.

**Parameters**

- `key` — the query to resolve.

**Errors.** Returns [`QueryError::Cycle`](#queryerror) if resolving `key`
requires a value still being computed further up the call chain — that is, if the
query graph has a cycle.

Memoized recursion — each subproblem is computed once and cached:

```rust
use query_lang::{Database, System, QueryError};

struct Fib;
impl System for Fib {
    type Key = u64;
    type Value = u64;
    fn compute(&self, db: &Database<Self>, n: &u64) -> Result<u64, QueryError> {
        if *n < 2 { return Ok(*n); }
        Ok(db.get(&(n - 1))?.wrapping_add(db.get(&(n - 2))?))
    }
}

let db = Database::new(Fib);
assert_eq!(db.get(&50)?, 12586269025);
# Ok::<(), QueryError>(())
```

Reusing the cache — an unrelated edit recomputes nothing:

```rust
use query_lang::{Database, System, QueryError};

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
enum Key { A, B, FromA }

struct S;
impl System for S {
    type Key = Key;
    type Value = i64;
    fn compute(&self, db: &Database<Self>, key: &Key) -> Result<i64, QueryError> {
        match key {
            Key::A | Key::B => Ok(0),
            Key::FromA => Ok(db.get(&Key::A)? + 1), // depends on A, never B
        }
    }
}

let mut db = Database::new(S);
db.set(Key::A, 10);
db.set(Key::B, 20);
assert_eq!(db.get(&Key::FromA)?, 11);
let computed = db.stats().computed;

db.set(Key::B, 999);            // edit an input FromA never read
assert_eq!(db.get(&Key::FromA)?, 11);
assert_eq!(db.stats().computed, computed); // nothing recomputed
# Ok::<(), QueryError>(())
```

### `Database::revision`

```rust,ignore
pub const fn revision(&self) -> Revision
```

The current [`Revision`](#revision) of the database. Advances by one each time
[`set`](#databaseset) gives an input a new value. Useful for asserting that an
operation did (or did not) change any input, and for correlating cache behaviour
with edits in logs.

```rust
use query_lang::{Database, System, QueryError};

struct S;
impl System for S {
    type Key = u32;
    type Value = u32;
    fn compute(&self, _db: &Database<Self>, k: &u32) -> Result<u32, QueryError> { Ok(*k) }
}

let mut db = Database::new(S);
assert_eq!(db.revision().as_u64(), 0);
db.set(1, 10);
db.set(2, 20);
assert_eq!(db.revision().as_u64(), 2);
```

### `Database::stats`

```rust,ignore
pub fn stats(&self) -> Stats
```

A snapshot of the cumulative resolution counters (see [`Stats`](#stats)).
Snapshot before and after an operation and subtract to measure exactly what that
operation cost.

```rust
use query_lang::{Database, System, QueryError};

struct S;
impl System for S {
    type Key = u32;
    type Value = i64;
    fn compute(&self, db: &Database<Self>, k: &u32) -> Result<i64, QueryError> {
        if *k == 0 { Ok(0) } else { Ok(db.get(&0)? + 1) }
    }
}

let mut db = Database::new(S);
db.set(0, 5);

let before = db.stats();
let _ = db.get(&1)?;                 // computes key 1
let after = db.stats();
assert_eq!(after.computed - before.computed, 1);

let _ = db.get(&1)?;                 // now a hit
assert_eq!(db.stats().hits, 1);
# Ok::<(), QueryError>(())
```

### `Database::system`

```rust,ignore
pub const fn system(&self) -> &S
```

A shared reference to the query system backing this database. Handy when the
system holds state a `compute` recorded (a counter, a diagnostics sink) that the
caller wants to read back.

```rust
use std::cell::Cell;
use query_lang::{Database, System, QueryError};

struct Counted { runs: Cell<u32> }
impl System for Counted {
    type Key = u32;
    type Value = u32;
    fn compute(&self, _db: &Database<Self>, k: &u32) -> Result<u32, QueryError> {
        self.runs.set(self.runs.get() + 1);
        Ok(*k)
    }
}

let db = Database::new(Counted { runs: Cell::new(0) });
let _ = db.get(&1)?;
let _ = db.get(&2)?;
assert_eq!(db.system().runs.get(), 2);
# Ok::<(), QueryError>(())
```

---

## `Revision`

```rust,ignore
pub struct Revision(/* private */);
```

A monotonic version stamp for the database. Every time an input changes, the
database advances its revision by one. The engine compares revisions — not
values — to decide whether a cached query is still good, which is a single
integer compare regardless of how large the cached value is.

Revisions are opaque and ordered: newer revisions compare greater than older
ones. The concrete number is exposed through `as_u64` for logging and tests, and
carries no meaning beyond its order. `Revision` implements `Copy`, `Ord`, `Hash`,
`Default`, and `Display` (as `r<n>`).

```rust,ignore
pub const fn as_u64(self) -> u64
```

The underlying counter value.

```rust
use query_lang::{Database, System, QueryError};

struct S;
impl System for S {
    type Key = u32;
    type Value = u32;
    fn compute(&self, _db: &Database<Self>, k: &u32) -> Result<u32, QueryError> { Ok(*k) }
}

let mut db = Database::new(S);
let start = db.revision();
db.set(1, 1);
let next = db.revision();

assert!(next > start);              // ordered
assert_eq!(next.as_u64(), 1);       // and inspectable
assert_eq!(alloc_display(next), "r1");

fn alloc_display(r: query_lang::Revision) -> String { format!("{r}") }
```

---

## `Stats`

```rust,ignore
pub struct Stats {
    pub computed: u64,
    pub validated: u64,
    pub hits: u64,
}
```

A snapshot of how a [`Database`](#database) resolved its queries. The counters are
cumulative over the life of the database and only ever increase.

| Field | Meaning |
|---|---|
| `computed` | Times a query ran its `compute` (a cache miss or a forced recomputation). |
| `validated` | Times a stale query was revalidated and its cached value reused because no dependency changed its inputs (*early cutoff*). |
| `hits` | Times a query was already current and returned immediately. |

`Stats` implements `Copy`, `PartialEq`, `Eq`, `Default`, and `Display`. Behind the
`serde` feature it also derives `Serialize`.

```rust,ignore
pub const fn total(self) -> u64
```

The total number of derived-query resolutions across all three paths. The ratio
of `computed` to `total` is how much of the requested work actually cost a
recomputation.

```rust
use query_lang::Stats;

let s = Stats { computed: 2, validated: 3, hits: 5 };
assert_eq!(s.total(), 10);
assert_eq!(s.to_string(), "computed=2, validated=3, hits=5");
```

Measuring early cutoff end to end — a value-preserving edit validates the
top query instead of recomputing it:

```rust
use query_lang::{Database, System, QueryError};

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
enum Key { In, Abs, Report }

struct S;
impl System for S {
    type Key = Key;
    type Value = i64;
    fn compute(&self, db: &Database<Self>, key: &Key) -> Result<i64, QueryError> {
        match key {
            Key::In => Ok(0),
            Key::Abs => Ok(db.get(&Key::In)?.abs()),
            Key::Report => Ok(db.get(&Key::Abs)? + 1),
        }
    }
}

let mut db = Database::new(S);
db.set(Key::In, 5);
assert_eq!(db.get(&Key::Report)?, 6);

// -5 has the same absolute value as 5, so `Abs` recomputes to the same result
// and `Report` is validated by early cutoff rather than recomputed.
db.set(Key::In, -5);
assert_eq!(db.get(&Key::Report)?, 6);
assert!(db.stats().validated >= 1);
# Ok::<(), QueryError>(())
```

---

## `QueryError`

```rust,ignore
#[non_exhaustive]
pub enum QueryError {
    Cycle,
}
```

The error returned from [`Database::get`](#databaseget) and
[`System::compute`](#systemcompute) when a query cannot be resolved. Resolution
terminates only when the dependency graph is acyclic; if a query asks — directly
or through a chain — for a result still being computed, there is no value to
return. Rather than recurse without bound or panic, the engine unwinds the whole
chain with `QueryError::Cycle`.

The type is `#[non_exhaustive]`: resolution has one failure mode today, and a
`match` that handles `Cycle` plus a wildcard stays correct if a variant is added
later. `QueryError` implements `Copy`, `Eq`, `Display`, and `core::error::Error`.

```rust
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> {
        db.get(key) // asks for the very key being computed
    }
}

let db = Database::new(SelfReferential);
assert_eq!(db.get(&1), Err(QueryError::Cycle));

// The database stays usable; a non-cyclic query still resolves.
assert!(db.get(&1).is_err());
```

Handling a cycle gracefully — treat it as a domain sentinel, the way a
spreadsheet shows `#CYCLE!`:

```rust
use query_lang::{Database, System, QueryError};

struct S;
impl System for S {
    type Key = u32;
    type Value = i64;
    fn compute(&self, db: &Database<Self>, k: &u32) -> Result<i64, QueryError> {
        if *k == 0 { db.get(&0) } else { Ok(*k as i64) }
    }
}

let db = Database::new(S);
let shown = match db.get(&0) {
    Ok(v) => v,
    Err(_) => -1, // the only resolution error is a cycle
};
assert_eq!(shown, -1);
assert_eq!(db.get(&5)?, 5);
# Ok::<(), QueryError>(())
```

---

## Feature flags

| Feature | Default | Description |
|---|---|---|
| `std` | yes | Links the standard library. Without it the crate is `#![no_std]` and needs only `alloc`; the engine uses no OS facilities either way. |
| `serde` | no | Derives `serde::Serialize` for [`Revision`]#revision and [`Stats`]#stats, so a database's version and cache metrics can be logged or inspected. |

```toml
# no_std build:
query-lang = { version = "1", default-features = false }

# with serde:
query-lang = { version = "1", features = ["serde"] }
```

Feature flags are additive: enabling one never removes or changes existing
behaviour.

---

## Stability

As of `1.0.0` the public API is frozen. query-lang follows
[Semantic Versioning](https://semver.org/); within the `1.x` series:

- The **surface**[`System`]#system (its associated types and `compute`),
  [`Database`]#database (`new` / `set` / `get` / `revision` / `stats` /
  `system`), [`Revision`]#revision, [`Stats`]#stats, and
  [`QueryError`]#queryerror — will not change in a breaking way. A breaking
  change means a new major version.
- The **resolution semantics** are part of the contract, not an implementation
  detail. A `set` with an unchanged value does not advance the revision or
  invalidate dependents; a derived query is recomputed only on a real miss or a
  changed dependency; and *early cutoff* holds — when a recomputed value equals
  its predecessor, dependents are validated rather than recomputed. Code may rely
  on these, and on [`Stats`]#stats reflecting them.
- `QueryError` is `#[non_exhaustive]`, so distinguishing a new resolution failure
  in the future is an additive minor change. Match it with a wildcard arm.
- The `serde` representations are fixed within `1.x`: a [`Revision`]#revision
  serializes as its underlying integer, and [`Stats`]#stats as an object with
  `computed` / `validated` / `hits` fields.
- MSRV (Rust 1.85) is a compatibility surface: raising it is a documented minor
  change, never a patch.

What is **not** promised: the concrete `Revision` numbering (only its order and
monotonicity are contractual), the internal cache representation, and the exact
`Debug` output of a [`Database`](#database).

See [`../dev/ROADMAP.md`](../dev/ROADMAP.md) and
[`../CHANGELOG.md`](../CHANGELOG.md).