Crates.io | salvia |
lib.rs | salvia |
version | 0.1.0 |
source | src |
created_at | 2022-01-11 18:05:54.994057 |
updated_at | 2022-01-11 18:05:54.994057 |
description | Incremental computing brought to async Rust |
homepage | https://github.com/haibane-tenshi/salvia |
repository | https://github.com/haibane-tenshi/salvia |
max_upload_size | |
id | 512383 |
size | 174,271 |
Incremental computing framework in async Rust.
Obligatory warning
This project is highly experimental and mostly is proof-of-concept. Unless you absolutely need
async
consider usingsalsa
crate instead.
salsa
- incremental computation framework for sync Rustsalvia
allows you to define queries (functions which values will be cached) and inputs
("functions" which value is set directly by user).
Upon execution, queries record which other queries or inputs it called and can avoid
recalculation when none of those values change.
salvia
was inspired by salsa
library.
As a major difference from it, this crate was designed to work in async Rust from the start
and had to make some concessions to achieve that.
use salvia::{query, Input, InputAccess, QueryContext, Runtime};
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, InputAccess)]
enum Team {
Blue,
Red,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
struct Score(u32);
impl Input<Score> for Team {
fn initial(&self) -> Score {
Score(0)
}
}
#[query]
async fn leader(cx: &QueryContext) -> Option<Team> {
use std::cmp::Ordering;
let red: Score = Team::Red.get(cx).await;
let blue: Score = Team::Blue.get(cx).await;
match red.cmp(&blue) {
Ordering::Less => Some(Team::Blue),
Ordering::Equal => None,
Ordering::Greater => Some(Team::Red),
}
}
#[tokio::main]
async fn main() {
let rt = Runtime::new().await;
rt.mutate(|cx| async move {
Team::Red.set(Score(1), &cx).await;
}).await;
let leader = rt.query(|cx| async move {
leader(&cx).await
}).await;
assert_eq!(leader, Some(Team::Red));
}
This project is dual licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above, without any additional terms or conditions.