Fix assertion for same DB in DbGuard

This commit is contained in:
Micha Reiser 2024-07-26 11:26:19 +02:00
parent e4ce917f6e
commit 18faece05e
No known key found for this signature in database

View file

@ -83,12 +83,16 @@ impl LocalState {
impl<'s> DbGuard<'s> { impl<'s> DbGuard<'s> {
fn new(state: &'s LocalState, db: &dyn Database) -> Self { fn new(state: &'s LocalState, db: &dyn Database) -> Self {
if let Some(current_db) = state.database.get() { if let Some(current_db) = state.database.get() {
let new_db = NonNull::from(db);
// Already attached? Assert that the database has not changed. // Already attached? Assert that the database has not changed.
assert_eq!( // NOTE: It's important to use `addr_eq` here because `NonNull` not only compares the address but also the type's metadata.
current_db, if !std::ptr::addr_eq(current_db.as_ptr(), new_db.as_ptr()) {
NonNull::from(db), panic!(
"cannot change database mid-query", "Cannot change database mid-query. current: {current_db:?}, new: {new_db:?}",
); );
}
Self { state: None } Self { state: None }
} else { } else {
// Otherwise, set the database. // Otherwise, set the database.