-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4151b09
commit 285ec93
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//! Test that `DisplayWithDb` works correctly | ||
|
||
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); | ||
} |