Skip to content

Commit

Permalink
Add DisplayWithDb
Browse files Browse the repository at this point in the history
  • Loading branch information
gavrilikhin-d committed Mar 23, 2024
1 parent 4151b09 commit c42ef95
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
27 changes: 27 additions & 0 deletions components/salsa-2022/src/display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::fmt::{self, Display, Formatter};

pub trait DisplayWithDb<'db, DB: ?Sized + 'db> {
fn fmt_with(&self, db: &DB, f: &mut Formatter<'_>) -> fmt::Result;

fn display_with(&self, db: &DB) -> impl Display {
FormatterFn(|f| self.fmt_with(db, f))
}

fn to_string_with(&self, db: &DB) -> String {
self.display_with(db).to_string()
}
}

// TODO: replace with `[std::fmt::FormatterFn]` when it becomes stable
struct FormatterFn<F>(pub F)
where
F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result;

impl<F> Display for FormatterFn<F>
where
F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(self.0)(f)
}
}
2 changes: 2 additions & 0 deletions components/salsa-2022/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod cancelled;
pub mod cycle;
pub mod database;
pub mod debug;
pub mod display;
pub mod durability;
pub mod event;
pub mod function;
Expand Down Expand Up @@ -32,6 +33,7 @@ pub use self::database::ParallelDatabase;
pub use self::database::Snapshot;
pub use self::debug::DebugWith;
pub use self::debug::DebugWithDb;
pub use self::display::DisplayWithDb;
pub use self::durability::Durability;
pub use self::event::Event;
pub use self::event::EventKind;
Expand Down
76 changes: 76 additions & 0 deletions salsa-2022-tests/tests/display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//! Test that `DeriveWithDb` is correctly derived.

use std::fmt::Display;

use expect_test::expect;
use salsa::DisplayWithDb;

#[salsa::jar(db = Db)]
struct Jar(MyInput, ComplexStruct);

trait Db: salsa::DbWithJar<Jar> {}

#[salsa::input]
struct MyInput {
field: u32,
}

impl<'db> DisplayWithDb<'db, dyn Db + 'db> for MyInput {
fn fmt_with(&self, db: &dyn Db, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "MyInput({})", self.field(db))
}
}

#[derive(Debug, Eq, PartialEq, Clone)]
struct NotSalsa {
field: String,
}

impl Display for NotSalsa {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "NotSalsa({})", self.field)
}
}

#[salsa::input]
struct ComplexStruct {
my_input: MyInput,
not_salsa: NotSalsa,
}

impl<'db> DisplayWithDb<'db, dyn Db + 'db> for ComplexStruct {
fn fmt_with(&self, db: &dyn Db, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"ComplexStruct {{ {}, {} }}",
self.my_input(db).display_with(db),
self.not_salsa(db)
)
}
}

#[salsa::db(Jar)]
#[derive(Default)]
struct Database {
storage: salsa::Storage<Self>,
}

impl salsa::Database for Database {}

impl Db for Database {}

#[test]
fn input() {
let db = Database::default();

let input = MyInput::new(&db, 22);
let not_salsa = NotSalsa {
field: "it's salsa time".to_string(),
};
let complex_struct = ComplexStruct::new(&db, input, not_salsa);

// all fields
let actual = format!("{}", complex_struct.display_with(&db));
let expected = expect!["ComplexStruct { MyInput(22), NotSalsa(it's salsa time) }"];
expected.assert_eq(&actual);
}

0 comments on commit c42ef95

Please sign in to comment.