Attribute Macro yeter::query

source ·
#[query]
Expand description

Annotates a function to make it a query that benefits from Yéter’s features

Usage

When annotated, a function will be turned into a Yéter query. Its return value is turned into an Rc<T> where T is the original declared return value. Calls to query functions will benefit from Yéter’s memoization and side effect system.

In addition to the modified function, the macro also produces a type-level empty enum that is used to uniquely identify a given query. If the function is declared without a body, the query is considered an input query; it must return an Option and its return value for a given input can be set in an imperative way using Database::set.

Syntax

#[yeter::query] doesn’t expect any attribute parameters. It must be applied to a function with or without a body, whose first argument is present and is typed as a &yeter::Database. The function cannot be an instance method (i.e. have a self receiver as its first argument).

Example

// Declaration and implicit definition
#[yeter::query]
fn length(db: &yeter::Database, input: String) -> usize {
    input.len()
}

// Usage
let db = yeter::Database::new();
println!("{}", length(&db, "hello world".into()));

It is also possible to declare an input query by omitting the body. Input queries must return Options and upon invocation, will return None by default. They can be assigned a value with Database::set.

// Declaration of an input query
#[yeter::query]
fn all_workspace_files(db: &yeter::Database) -> Option<Vec<PathBuf>>;

// Definition of its value
let mut db = yeter::Database::new();
db.set::<all_workspace_files>((), Some(vec![ /* ... */ ]));