1 unstable release

0.1.0 Jan 11, 2022

#930 in Asynchronous

MIT/Apache

140KB
2.5K SLoC

Salvia

CI Security audit

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 using salsa crate instead.

Primer

salvia 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.

Usage example

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));
}

License

This project is dual licensed under either of

at your option.

Contribution

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.

Dependencies

~5–7MB
~132K SLoC