-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add DiplayWithDb #477
Comments
Haha, I already add this trait in my personal customized version of salsa |
@xiyuzhai how did you do it? I have troubles with references lifetimes |
I imitated DebugWithDb's implementation. However, my implementation differs from salsa in that I remove the generic parameter Db, and use a single Db type for the whole workspace to save compilation time significantly.
|
I came up with something like this at the end: use std::fmt::{self, Display, Formatter, FormatterFn};
pub trait DisplayWithDb<'me, DB: ?Sized + 'me> {
fn fmt_with(&self, db: &DB, f: &mut Formatter<'_>) -> fmt::Result;
fn display_with(&self, db: &DB) -> FormatterFn<impl Fn(&mut Formatter<'_>) -> fmt::Result> {
FormatterFn(|f| self.fmt_with(db, f)) // Unstable rust. Write a struct wrapper, if you want to use stable rust
}
fn to_string_with(&self, db: &DB) -> String {
self.display_with(db).to_string()
}
}
impl<'me, DB: ?Sized + 'me, D: Display> DisplayWithDb<'me, DB> for D {
fn fmt_with(&self, _db: &DB, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(&self, f)
}
} Implement it like this: #[salsa::interned]
pub struct Typename {
#[return_ref]
pub text: String,
}
impl<'me> DisplayWithDb<'me, dyn Db + 'me> for Typename {
fn fmt_with(&self, db: &dyn Db, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.text(db))
}
} There is something tricky with |
Add trait to Display items with database
The text was updated successfully, but these errors were encountered: