Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/attach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,38 @@ impl Attached {
op()
}

#[inline]
fn attach_allow_change<Db, R>(&self, db: &Db, op: impl FnOnce() -> R) -> R
where
Db: ?Sized + Database,
{
struct DbGuard<'s> {
state: &'s Attached,
prev: Option<NonNull<dyn Database>>,
}

impl<'s> DbGuard<'s> {
#[inline]
fn new(attached: &'s Attached, db: &dyn Database) -> Self {
let prev = attached.database.replace(Some(NonNull::from(db)));
Self {
state: attached,
prev,
}
}
}

impl Drop for DbGuard<'_> {
#[inline]
fn drop(&mut self) {
self.state.database.set(self.prev);
}
}

let _guard = DbGuard::new(self, db.as_dyn_database());
op()
}

/// Access the "attached" database. Returns `None` if no database is attached.
/// Databases are attached with `attach_database`.
#[inline]
Expand All @@ -104,6 +136,23 @@ where
)
}

/// Attach the database to the current thread and execute `op`.
/// Allows a different database than currently attached. The original database
/// will be restored on return.
///
/// **Note:** Switching databases can cause bugs. If you do not intend to switch
/// databases, prefer [`attach`] which will panic if you accidentally do.
#[inline]
pub fn attach_allow_change<R, Db>(db: &Db, op: impl FnOnce() -> R) -> R
where
Db: ?Sized + Database,
{
ATTACHED.with(
#[inline]
|a| a.attach_allow_change(db, op),
)
}

/// Access the "attached" database. Returns `None` if no database is attached.
/// Databases are attached with `attach_database`.
#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub use self::runtime::Runtime;
pub use self::storage::{Storage, StorageHandle};
pub use self::update::Update;
pub use self::zalsa::IngredientIndex;
pub use crate::attach::{attach, with_attached_database};
pub use crate::attach::{attach, attach_allow_change, with_attached_database};

pub mod prelude {
#[cfg(feature = "accumulator")]
Expand Down
3 changes: 3 additions & 0 deletions src/zalsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,9 @@ impl ErasedJar {
pub const fn erase<I: HasJar>() -> Self {
Self {
kind: I::KIND,
// This is a false positive of the lint on beta, fixed on nightly.
// FIXME: Remove this when nightly stabilizes.
#[allow(clippy::incompatible_msrv)]
type_id: TypeId::of::<I::Jar>,
type_name: std::any::type_name::<I::Jar>,
create_ingredients: <I::Jar>::create_ingredients,
Expand Down
8 changes: 4 additions & 4 deletions tests/compile-fail/incomplete_persistence.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0277]: the trait bound `NotPersistable<'_>: Serialize` is not satisfied
error[E0277]: the trait bound `NotPersistable<'_>: serde::Serialize` is not satisfied
--> tests/compile-fail/incomplete_persistence.rs:1:1
|
1 | #[salsa::tracked(persist)]
Expand All @@ -22,7 +22,7 @@ error[E0277]: the trait bound `NotPersistable<'_>: Serialize` is not satisfied
= note: required for `(NotPersistable<'_>,)` to implement `Serialize`
= note: this error originates in the macro `salsa::plumbing::setup_tracked_struct` which comes from the expansion of the attribute macro `salsa::tracked` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `NotPersistable<'_>: Deserialize<'_>` is not satisfied
error[E0277]: the trait bound `NotPersistable<'_>: serde::Deserialize<'de>` is not satisfied
--> tests/compile-fail/incomplete_persistence.rs:1:1
|
1 | #[salsa::tracked(persist)]
Expand All @@ -43,7 +43,7 @@ error[E0277]: the trait bound `NotPersistable<'_>: Deserialize<'_>` is not satis
= note: required for `(NotPersistable<'_>,)` to implement `Deserialize<'_>`
= note: this error originates in the macro `salsa::plumbing::setup_tracked_struct` which comes from the expansion of the attribute macro `salsa::tracked` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `NotPersistable<'db>: Serialize` is not satisfied
error[E0277]: the trait bound `NotPersistable<'db>: serde::Serialize` is not satisfied
--> tests/compile-fail/incomplete_persistence.rs:12:45
|
12 | fn query(_db: &dyn salsa::Database, _input: NotPersistable<'_>) {}
Expand Down Expand Up @@ -71,7 +71,7 @@ note: required by a bound in `query_input_is_persistable`
| required by this bound in `query_input_is_persistable`
= note: this error originates in the macro `salsa::plumbing::setup_tracked_fn` which comes from the expansion of the attribute macro `salsa::tracked` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `for<'de> NotPersistable<'db>: Deserialize<'de>` is not satisfied
error[E0277]: the trait bound `NotPersistable<'db>: serde::Deserialize<'de>` is not satisfied
--> tests/compile-fail/incomplete_persistence.rs:12:45
|
12 | fn query(_db: &dyn salsa::Database, _input: NotPersistable<'_>) {}
Expand Down