Skip to content

Instantly share code, notes, and snippets.

@OLUWAMUYIWA
Last active May 14, 2024 03:59
Show Gist options
  • Save OLUWAMUYIWA/c55593866a5e2a937a2613ae5d14eeaa to your computer and use it in GitHub Desktop.
Save OLUWAMUYIWA/c55593866a5e2a937a2613ae5d14eeaa to your computer and use it in GitHub Desktop.
Salsa Builder API

Input example

#[salsa::input(jar = Jar)]
struct MyInput {
    field1: u32,
    field2: u32,
}

#[salsa::input(singleton)]
struct MySingletonInput {
    field1: u32,
}

Output Example:

fn test_builder() {
    let mut db = Database::default();
    // durability set with new is always low
    let mut input = MyInput::new(&db, 12, 13);
    input
        .set_field1(&mut db)
        .with_durability(Durability::HIGH)
        .to(20);
    input
        .set_field2(&mut db)
        .with_durability(Durability::HIGH)
        .to(40);
    let input_from_builder = MyInput::new_builder()
        .with_durability(Durability::HIGH)
        .with_fields(20, 40)
        .build(&db)
        .unwrap();

    assert_eq!(input.field1(&db), input_from_builder.field1(&db));
    assert_eq!(input.field2(&db), input_from_builder.field2(&db));
}

On a more general note, ass suggested by #476, we should probably do this for all Salsa structs and fields:

    #[salsa::input]
    pub struct SourceProgram {
        #[return_ref]
        pub text: String,
        #[return_ref]
        pub path: PathBuf,
    }

    SourceProgram::build().with_text("text").with_path("foo.rs").finish()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment