From 7b0744ec2108ee4996a023381e4e1b92d04fd6d0 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Wed, 5 Feb 2025 13:55:49 -0500 Subject: [PATCH] feature: expose database contents as inherent method on Ingredients --- src/input.rs | 14 ++++++++++++ src/interned.rs | 14 ++++++++++++ src/storage.rs | 47 -------------------------------------- src/tracked_struct.rs | 14 ++++++++++++ tests/debug_db_contents.rs | 16 ++++++------- 5 files changed, 49 insertions(+), 56 deletions(-) diff --git a/src/input.rs b/src/input.rs index 431acf218..cf887bdb0 100644 --- a/src/input.rs +++ b/src/input.rs @@ -184,6 +184,20 @@ impl IngredientImpl { &value.fields } + #[cfg(feature = "salsa_unstable")] + /// Returns all data corresponding to the input struct. + pub fn entries<'db>( + &'db self, + db: &'db dyn crate::Database, + ) -> impl Iterator> { + db.zalsa() + .table() + .pages + .iter() + .filter_map(|page| page.cast_type::>>()) + .flat_map(|page| page.slots()) + } + /// Peek at the field values without recording any read dependency. /// Used for debug printouts. pub fn leak_fields<'db>(&'db self, db: &'db dyn Database, id: C::Struct) -> &'db C::Fields { diff --git a/src/interned.rs b/src/interned.rs index 246bd7ca7..5f8eecccc 100644 --- a/src/interned.rs +++ b/src/interned.rs @@ -253,6 +253,20 @@ where self.data(db, C::deref_struct(s)) } + #[cfg(feature = "salsa_unstable")] + /// Returns all data corresponding to the interned struct. + pub fn entries<'db>( + &'db self, + db: &'db dyn crate::Database, + ) -> impl Iterator> { + db.zalsa() + .table() + .pages + .iter() + .filter_map(|page| page.cast_type::>>()) + .flat_map(|page| page.slots()) + } + pub fn reset(&mut self, revision: Revision) { assert!(revision > self.reset_at); self.reset_at = revision; diff --git a/src/storage.rs b/src/storage.rs index 886fa91ba..10341e0b8 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -3,7 +3,6 @@ use std::{marker::PhantomData, panic::RefUnwindSafe, sync::Arc}; use parking_lot::{Condvar, Mutex}; use crate::{ - plumbing::{input, interned, tracked_struct}, zalsa::{Zalsa, ZalsaDatabase}, zalsa_local::{self, ZalsaLocal}, Database, Event, EventKind, @@ -61,52 +60,6 @@ impl Default for Storage { } impl Storage { - pub fn debug_input_entries(&self) -> impl Iterator> - where - T: input::Configuration, - { - let zalsa = self.zalsa_impl(); - zalsa - .table() - .pages - .iter() - .filter_map(|page| page.cast_type::>>()) - .flat_map(|page| page.slots()) - } - - pub fn debug_interned_entries(&self) -> impl Iterator> - where - T: interned::Configuration, - { - let zalsa = self.zalsa_impl(); - zalsa - .table() - .pages - .iter() - .filter_map(|page| page.cast_type::>>()) - .flat_map(|page| page.slots()) - } - - pub fn debug_tracked_entries(&self) -> impl Iterator> - where - T: tracked_struct::Configuration, - { - let zalsa = self.zalsa_impl(); - zalsa - .table() - .pages - .iter() - .filter_map(|page| page.cast_type::>>()) - .flat_map(|pages| pages.slots()) - } - - /// Access the `Arc`. This should always be - /// possible as `zalsa_impl` only becomes - /// `None` once we are in the `Drop` impl. - fn zalsa_impl(&self) -> &Arc { - &self.zalsa_impl - } - // ANCHOR: cancel_other_workers /// Sets cancellation flag and blocks until all other workers with access /// to this storage have completed. diff --git a/src/tracked_struct.rs b/src/tracked_struct.rs index ea40d1acc..382a1b660 100644 --- a/src/tracked_struct.rs +++ b/src/tracked_struct.rs @@ -688,6 +688,20 @@ where unsafe { self.to_self_ref(&data.fields) } } + + #[cfg(feature = "salsa_unstable")] + /// Returns all data corresponding to the tracked struct. + pub fn entries<'db>( + &'db self, + db: &'db dyn crate::Database, + ) -> impl Iterator> { + db.zalsa() + .table() + .pages + .iter() + .filter_map(|page| page.cast_type::>>()) + .flat_map(|page| page.slots()) + } } impl Ingredient for IngredientImpl diff --git a/tests/debug_db_contents.rs b/tests/debug_db_contents.rs index b37b7ef72..dca656c3f 100644 --- a/tests/debug_db_contents.rs +++ b/tests/debug_db_contents.rs @@ -26,9 +26,8 @@ fn execute() { let _ = InternedStruct::new(&db, "Salsa2".to_string()); // test interned structs - let interned = db - .storage() - .debug_interned_entries::() + let interned = InternedStruct::ingredient(&db) + .entries(&db) .collect::>(); assert_eq!(interned.len(), 2); @@ -37,9 +36,9 @@ fn execute() { // test input structs let input = InputStruct::new(&db, 22); - let inputs = db - .storage() - .debug_input_entries::() + + let inputs = InputStruct::ingredient(&db) + .entries(&db) .collect::>(); assert_eq!(inputs.len(), 1); @@ -48,9 +47,8 @@ fn execute() { // test tracked structs let computed = tracked_fn(&db, input).field(&db); assert_eq!(computed, 44); - let tracked = db - .storage() - .debug_tracked_entries::() + let tracked = TrackedStruct::ingredient(&db) + .entries(&db) .collect::>(); assert_eq!(tracked.len(), 1);