query_flow/output_eq.rs
1//! Utility functions for output equality comparison in queries.
2//!
3//! These functions are designed to be used with the `#[query(output_eq = ...)]` attribute
4//! when the output type is `Result<T, E>` and `E` does not implement `PartialEq`.
5
6/// Compare only the `Ok` values. Returns `false` for any `Err` case,
7/// causing downstream queries to be invalidated (recomputed).
8///
9/// # Example
10/// ```ignore
11/// #[query(output_eq = query_flow::output_eq::ok_or_invalidate)]
12/// fn my_query(ctx: &mut QueryContext) -> Result<Result<i32, MyError>, QueryError> {
13/// // ...
14/// }
15/// ```
16pub fn ok_or_invalidate<T: PartialEq, E>(a: &Result<T, E>, b: &Result<T, E>) -> bool {
17 match (a, b) {
18 (Ok(a), Ok(b)) => a == b,
19 _ => false,
20 }
21}
22
23/// Compare `Ok` values for equality, treat all `Err` as equal.
24///
25/// Use this when you want to suppress downstream recomputation if both results are errors,
26/// regardless of the error content.
27///
28/// # Example
29/// ```ignore
30/// #[query(output_eq = query_flow::output_eq::ignore_err)]
31/// fn my_query(ctx: &mut QueryContext) -> Result<Result<i32, MyError>, QueryError> {
32/// // ...
33/// }
34/// ```
35pub fn ignore_err<T: PartialEq, E>(a: &Result<T, E>, b: &Result<T, E>) -> bool {
36 match (a, b) {
37 (Ok(a), Ok(b)) => a == b,
38 (Err(_), Err(_)) => true,
39 _ => false,
40 }
41}