Skip to main content

QueryOutput

Trait QueryOutput 

Source
pub trait QueryOutput:
    PartialEq
    + Send
    + Sync
    + 'static { }
Expand description

Convenience trait for query output types.

This trait combines the bounds needed for a type to be used as a query output: PartialEq + Send + Sync + 'static.

  • PartialEq is required for the default output_eq comparison (early cutoff optimization)
  • Send + Sync + 'static allows the output to be cached and shared across threads

§When to Use

Use QueryOutput for generic type parameters that appear only in query output:

use std::fmt::Display;
use std::str::FromStr;

use query_flow::{query, Db, QueryError, QueryOutput, QueryRuntime};

#[query]
fn parse<T: QueryOutput + FromStr>(db: &impl Db, text: String) -> Result<T, QueryError>
where
    T::Err: Display,
{
    let _ = db;
    text.parse().map_err(|e| anyhow::anyhow!("{}", e).into())
}

let runtime = QueryRuntime::new();
assert_eq!(*runtime.query(Parse::<i32>::new("42".into())).unwrap(), 42);
assert_eq!(*runtime.query(Parse::<u64>::new("42".into())).unwrap(), 42u64);

§When Not to Use

If you supply a custom comparator with #[query(output_eq = path)], the output does not need PartialEq. In that case, use raw bounds instead:

use query_flow::{query, Db, QueryError, QueryRuntime};

// No `PartialEq` on `T`, so the comparator is supplied explicitly.
// Returning `false` disables early cutoff: dependents always recompute.
fn always_recompute<T>(_old: &T, _new: &T) -> bool {
    false
}

#[query(output_eq = always_recompute)]
fn create<T: Default + Send + Sync + 'static>(db: &impl Db) -> Result<T, QueryError> {
    let _ = db;
    Ok(T::default())
}

let runtime = QueryRuntime::new();
assert_eq!(*runtime.query(Create::<i32>::new()).unwrap(), 0);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T: PartialEq + Send + Sync + 'static> QueryOutput for T