diff --git a/components/salsa-macros/src/db.rs b/components/salsa-macros/src/db.rs index 091175d7..17ab2869 100644 --- a/components/salsa-macros/src/db.rs +++ b/components/salsa-macros/src/db.rs @@ -84,21 +84,18 @@ impl DbMacro { fn has_storage_impl(&self, input: &syn::ItemStruct) -> syn::Result { let storage = self.find_storage_field(input)?; let db = &input.ident; - - let SalsaHasStorage = self.hygiene.ident("SalsaHasStorage"); - let SalsaStorage = self.hygiene.ident("SalsaStorage"); + let zalsa = self.hygiene.ident("zalsa"); Ok(quote! { const _: () = { - use salsa::storage::HasStorage as #SalsaHasStorage; - use salsa::storage::Storage as #SalsaStorage; + use salsa::plumbing as #zalsa; - unsafe impl #SalsaHasStorage for #db { - fn storage(&self) -> &#SalsaStorage { + unsafe impl #zalsa::HasStorage for #db { + fn storage(&self) -> &#zalsa::Storage { &self.#storage } - fn storage_mut(&mut self) -> &mut #SalsaStorage { + fn storage_mut(&mut self) -> &mut #zalsa::Storage { &mut self.#storage } } @@ -115,17 +112,21 @@ impl DbMacro { } fn add_salsa_view_method_impl(&self, input: &mut syn::ItemImpl) -> syn::Result<()> { + let zalsa = self.hygiene.ident("zalsa"); + let Some((_, TraitPath, _)) = &input.trait_ else { return Err(syn::Error::new_spanned( &input.self_ty, "impl must be on a trait", )); }; + input.items.push(parse_quote! { #[doc(hidden)] #[allow(uncommon_codepoins)] fn zalsa_add_view(&self) { - salsa::storage::views(self).add::(|t| t, |t| t); + use salsa::plumbing as #zalsa; + #zalsa::views(self).add::(|t| t, |t| t); } }); Ok(()) diff --git a/src/function.rs b/src/function.rs index 5293ae58..555f9e0e 100644 --- a/src/function.rs +++ b/src/function.rs @@ -82,7 +82,7 @@ pub trait Configuration: 'static { /// * the `specify` method, which can only be used when the key is an entity created by the active query. /// It sets the value of the function imperatively, so that when later fetches occur, they'll return this value. /// * the `store` method, which can only be invoked with an `&mut` reference, and is to set input fields. -pub struct FunctionIngredient { +pub struct IngredientImpl { /// The ingredient index we were assigned in the database. /// Used to construct `DatabaseKeyIndex` values. index: IngredientIndex, @@ -127,7 +127,7 @@ pub fn should_backdate_value(old_value: &V, new_value: &V) -> bool { /// e.g. you can do `Config` and `Config`. pub struct Config(std::marker::PhantomData<[(); C]>); -impl FunctionIngredient +impl IngredientImpl where C: Configuration, { @@ -199,7 +199,7 @@ where } } -impl Ingredient for FunctionIngredient +impl Ingredient for IngredientImpl where C: Configuration, { @@ -276,7 +276,7 @@ where } } -impl std::fmt::Debug for FunctionIngredient +impl std::fmt::Debug for IngredientImpl where C: Configuration, { @@ -287,7 +287,7 @@ where } } -impl IngredientRequiresReset for FunctionIngredient +impl IngredientRequiresReset for IngredientImpl where C: Configuration, { diff --git a/src/function/accumulated.rs b/src/function/accumulated.rs index 0e747428..4c9139ce 100644 --- a/src/function/accumulated.rs +++ b/src/function/accumulated.rs @@ -3,10 +3,10 @@ use crate::{ storage::DatabaseGen, DatabaseKeyIndex, Id, }; -use super::{Configuration, FunctionIngredient}; +use super::{Configuration, IngredientImpl}; use crate::accumulator::Accumulator; -impl FunctionIngredient +impl IngredientImpl where C: Configuration, { diff --git a/src/function/backdate.rs b/src/function/backdate.rs index 5d9b4ece..abc54cbc 100644 --- a/src/function/backdate.rs +++ b/src/function/backdate.rs @@ -1,8 +1,8 @@ use crate::runtime::local_state::QueryRevisions; -use super::{memo::Memo, Configuration, FunctionIngredient}; +use super::{memo::Memo, Configuration, IngredientImpl}; -impl FunctionIngredient +impl IngredientImpl where C: Configuration, { diff --git a/src/function/delete.rs b/src/function/delete.rs index 0a4039d7..1be4ea45 100644 --- a/src/function/delete.rs +++ b/src/function/delete.rs @@ -3,9 +3,9 @@ use crossbeam::queue::SegQueue; use crate::{runtime::local_state::QueryOrigin, Id}; -use super::{memo, Configuration, FunctionIngredient}; +use super::{memo, Configuration, IngredientImpl}; -impl FunctionIngredient +impl IngredientImpl where C: Configuration, { diff --git a/src/function/diff_outputs.rs b/src/function/diff_outputs.rs index ced2dc1c..3bc64449 100644 --- a/src/function/diff_outputs.rs +++ b/src/function/diff_outputs.rs @@ -3,9 +3,9 @@ use crate::{ storage::DatabaseGen, Database, DatabaseKeyIndex, Event, EventKind, }; -use super::{memo::Memo, Configuration, FunctionIngredient}; +use super::{memo::Memo, Configuration, IngredientImpl}; -impl FunctionIngredient +impl IngredientImpl where C: Configuration, { diff --git a/src/function/execute.rs b/src/function/execute.rs index 60ab4918..ed966936 100644 --- a/src/function/execute.rs +++ b/src/function/execute.rs @@ -6,9 +6,9 @@ use crate::{ Cycle, Database, Event, EventKind, }; -use super::{memo::Memo, Configuration, FunctionIngredient}; +use super::{memo::Memo, Configuration, IngredientImpl}; -impl FunctionIngredient +impl IngredientImpl where C: Configuration, { diff --git a/src/function/fetch.rs b/src/function/fetch.rs index a41afc78..0f7986b0 100644 --- a/src/function/fetch.rs +++ b/src/function/fetch.rs @@ -2,9 +2,9 @@ use arc_swap::Guard; use crate::{runtime::StampedValue, storage::DatabaseGen, Id}; -use super::{Configuration, FunctionIngredient}; +use super::{Configuration, IngredientImpl}; -impl FunctionIngredient +impl IngredientImpl where C: Configuration, { diff --git a/src/function/inputs.rs b/src/function/inputs.rs index 09b56410..1b284003 100644 --- a/src/function/inputs.rs +++ b/src/function/inputs.rs @@ -1,8 +1,8 @@ use crate::{runtime::local_state::QueryOrigin, Id}; -use super::{Configuration, FunctionIngredient}; +use super::{Configuration, IngredientImpl}; -impl FunctionIngredient +impl IngredientImpl where C: Configuration, { diff --git a/src/function/maybe_changed_after.rs b/src/function/maybe_changed_after.rs index 916a5734..c3b24dc6 100644 --- a/src/function/maybe_changed_after.rs +++ b/src/function/maybe_changed_after.rs @@ -10,9 +10,9 @@ use crate::{ Id, Revision, Runtime, }; -use super::{memo::Memo, Configuration, FunctionIngredient}; +use super::{memo::Memo, Configuration, IngredientImpl}; -impl FunctionIngredient +impl IngredientImpl where C: Configuration, { diff --git a/src/function/specify.rs b/src/function/specify.rs index 3b70563a..32de25d1 100644 --- a/src/function/specify.rs +++ b/src/function/specify.rs @@ -7,9 +7,9 @@ use crate::{ Database, DatabaseKeyIndex, Id, }; -use super::{memo::Memo, Configuration, FunctionIngredient}; +use super::{memo::Memo, Configuration, IngredientImpl}; -impl FunctionIngredient +impl IngredientImpl where C: Configuration, { diff --git a/src/function/store.rs b/src/function/store.rs index c0d17e50..49b2b4b5 100644 --- a/src/function/store.rs +++ b/src/function/store.rs @@ -8,9 +8,9 @@ use crate::{ Id, Runtime, }; -use super::{memo::Memo, Configuration, FunctionIngredient}; +use super::{memo::Memo, Configuration, IngredientImpl}; -impl FunctionIngredient +impl IngredientImpl where C: Configuration, { diff --git a/src/input.rs b/src/input.rs index f85873f3..d7599bd6 100644 --- a/src/input.rs +++ b/src/input.rs @@ -18,14 +18,14 @@ pub trait Configuration: Any { type Id: FromId + 'static + Send + Sync; } -pub struct InputIngredient { +pub struct IngredientImpl { ingredient_index: IngredientIndex, counter: AtomicU32, debug_name: &'static str, _phantom: std::marker::PhantomData, } -impl InputIngredient { +impl IngredientImpl { pub fn new(index: IngredientIndex, debug_name: &'static str) -> Self { Self { ingredient_index: index, @@ -62,7 +62,7 @@ impl InputIngredient { } } -impl Ingredient for InputIngredient { +impl Ingredient for IngredientImpl { fn ingredient_index(&self) -> IngredientIndex { self.ingredient_index } @@ -125,11 +125,11 @@ impl Ingredient for InputIngredient { } } -impl IngredientRequiresReset for InputIngredient { +impl IngredientRequiresReset for IngredientImpl { const RESET_ON_NEW_REVISION: bool = false; } -impl std::fmt::Debug for InputIngredient { +impl std::fmt::Debug for IngredientImpl { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct(std::any::type_name::()) .field("index", &self.ingredient_index) diff --git a/src/input_field.rs b/src/input_field.rs index 8e9e0110..b686d597 100644 --- a/src/input_field.rs +++ b/src/input_field.rs @@ -2,7 +2,6 @@ use crate::cycle::CycleRecoveryStrategy; use crate::id::{AsId, FromId}; use crate::ingredient::{fmt_index, Ingredient, IngredientRequiresReset}; use crate::input::Configuration; -use crate::plumbing::transmute_lifetime; use crate::runtime::local_state::QueryOrigin; use crate::runtime::StampedValue; use crate::storage::IngredientIndex; @@ -23,13 +22,13 @@ impl InputFieldData for T {} /// a shared reference, so some locking is required. /// Altogether this makes the implementation somewhat simpler than tracked /// structs. -pub struct InputFieldIngredient { +pub struct FieldIngredientImpl { index: IngredientIndex, map: DashMap>>, debug_name: &'static str, } -impl InputFieldIngredient +impl FieldIngredientImpl where C: Configuration, F: InputFieldData, @@ -110,7 +109,16 @@ where } } -impl Ingredient for InputFieldIngredient +/// More limited wrapper around transmute that copies lifetime from `a` to `b`. +/// +/// # Safety condition +/// +/// `b` must be owned by `a` +unsafe fn transmute_lifetime<'a, 'b, A, B>(_a: &'a A, b: &'b B) -> &'a B { + std::mem::transmute(b) +} + +impl Ingredient for FieldIngredientImpl where C: Configuration, F: InputFieldData, @@ -166,7 +174,7 @@ where } } -impl IngredientRequiresReset for InputFieldIngredient +impl IngredientRequiresReset for FieldIngredientImpl where C: Configuration, F: InputFieldData, @@ -174,7 +182,7 @@ where const RESET_ON_NEW_REVISION: bool = false; } -impl std::fmt::Debug for InputFieldIngredient +impl std::fmt::Debug for FieldIngredientImpl where C: Configuration, F: InputFieldData, diff --git a/src/interned.rs b/src/interned.rs index 741b5375..3dc78d8f 100644 --- a/src/interned.rs +++ b/src/interned.rs @@ -53,7 +53,7 @@ impl InternedData for T {} /// The interned ingredient has the job of hashing values of type `Data` to produce an `Id`. /// It used to store interned structs but also to store the id fields of a tracked struct. /// Interned values endure until they are explicitly removed in some way. -pub struct InternedIngredient { +pub struct IngredientImpl { /// Index of this ingredient in the database (used to construct database-ids, etc). ingredient_index: IngredientIndex, @@ -86,7 +86,7 @@ where fields: C::Data<'static>, } -impl InternedIngredient +impl IngredientImpl where C: Configuration, { @@ -182,7 +182,7 @@ where } } -impl Ingredient for InternedIngredient +impl Ingredient for IngredientImpl where C: Configuration, { @@ -247,14 +247,14 @@ where } } -impl IngredientRequiresReset for InternedIngredient +impl IngredientRequiresReset for IngredientImpl where C: Configuration, { const RESET_ON_NEW_REVISION: bool = false; } -impl std::fmt::Debug for InternedIngredient +impl std::fmt::Debug for IngredientImpl where C: Configuration, { diff --git a/src/lib.rs b/src/lib.rs index 916e251a..0c79c550 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,28 +1,27 @@ -pub mod accumulator; +mod accumulator; mod alloc; -pub mod cancelled; -pub mod cycle; -pub mod database; -pub mod durability; -pub mod event; -pub mod function; -pub mod hash; -pub mod id; -pub mod ingredient; -pub mod ingredient_list; -pub mod input; -pub mod input_field; -pub mod interned; -pub mod key; +mod cancelled; +mod cycle; +mod database; +mod durability; +mod event; +mod function; +mod hash; +mod id; +mod ingredient; +mod ingredient_list; +mod input; +mod input_field; +mod interned; +mod key; mod nonce; -pub mod plumbing; -pub mod revision; -pub mod runtime; -pub mod salsa_struct; -pub mod setter; -pub mod storage; -pub mod tracked_struct; -pub mod update; +mod revision; +mod runtime; +mod salsa_struct; +mod setter; +mod storage; +mod tracked_struct; +mod update; mod views; pub use self::cancelled::Cancelled; @@ -45,3 +44,47 @@ pub use salsa_macros::interned; pub use salsa_macros::tracked; pub use salsa_macros::DebugWithDb; pub use salsa_macros::Update; + +/// Internal names used by salsa macros. +/// +/// # WARNING +/// +/// The contents of this module are NOT subject to semver. +pub mod plumbing { + pub use crate::cycle::Cycle; + pub use crate::cycle::CycleRecoveryStrategy; + pub use crate::database::Database; + pub use crate::id::AsId; + pub use crate::id::FromId; + pub use crate::id::Id; + pub use crate::ingredient::Ingredient; + pub use crate::ingredient::Jar; + pub use crate::salsa_struct::SalsaStructInDb; + pub use crate::storage::views; + pub use crate::storage::IngredientCache; + pub use crate::storage::IngredientIndex; + + pub mod input { + pub use crate::input::Configuration; + pub use crate::input::IngredientImpl; + pub use crate::input_field::FieldIngredientImpl; + } + + pub mod interned { + pub use crate::interned::Configuration; + pub use crate::interned::IngredientImpl; + pub use crate::interned::ValueStruct; + } + + pub mod function { + pub use crate::function::Configuration; + pub use crate::function::IngredientImpl; + } + + pub mod tracked_struct { + pub use crate::tracked_struct::tracked_field::FieldIngredientImpl; + pub use crate::tracked_struct::Configuration; + pub use crate::tracked_struct::IngredientImpl; + pub use crate::tracked_struct::JarImpl; + } +} diff --git a/src/plumbing.rs b/src/plumbing.rs deleted file mode 100644 index 7be05616..00000000 --- a/src/plumbing.rs +++ /dev/null @@ -1,8 +0,0 @@ -// Returns `u` but with the lifetime of `t`. -// -// Safe if you know that data at `u` will remain shared -// until the reference `t` expires. -#[allow(clippy::needless_lifetimes)] -pub(crate) unsafe fn transmute_lifetime<'t, 'u, T, U>(_t: &'t T, u: &'u U) -> &'t U { - std::mem::transmute(u) -} diff --git a/src/setter.rs b/src/setter.rs index 75d0a341..d0669b47 100644 --- a/src/setter.rs +++ b/src/setter.rs @@ -1,6 +1,6 @@ use crate::id::AsId; use crate::input::Configuration; -use crate::input_field::{InputFieldData, InputFieldIngredient}; +use crate::input_field::{FieldIngredientImpl, InputFieldData}; use crate::{Durability, Runtime}; use std::hash::Hash; @@ -8,7 +8,7 @@ use std::hash::Hash; pub struct Setter<'setter, C: Configuration, F: InputFieldData> { runtime: &'setter mut Runtime, key: C::Id, - ingredient: &'setter mut InputFieldIngredient, + ingredient: &'setter mut FieldIngredientImpl, durability: Durability, } @@ -20,7 +20,7 @@ where pub fn new( runtime: &'setter mut Runtime, key: C::Id, - ingredient: &'setter mut InputFieldIngredient, + ingredient: &'setter mut FieldIngredientImpl, ) -> Self { Setter { runtime, diff --git a/src/tracked_struct.rs b/src/tracked_struct.rs index a749cdf5..e869a536 100644 --- a/src/tracked_struct.rs +++ b/src/tracked_struct.rs @@ -2,6 +2,7 @@ use std::{fmt, hash::Hash, marker::PhantomData, ptr::NonNull}; use crossbeam::atomic::AtomicCell; use dashmap::mapref::entry::Entry; +use tracked_field::FieldIngredientImpl; use crate::{ cycle::CycleRecoveryStrategy, @@ -17,10 +18,9 @@ use crate::{ }; use self::struct_map::{StructMap, Update}; -pub use self::tracked_field::TrackedFieldIngredient; mod struct_map; -mod tracked_field; +pub mod tracked_field; // ANCHOR: Configuration /// Trait that defines the key properties of a tracked struct. @@ -99,14 +99,14 @@ pub trait Configuration: Jar + Sized + 'static { } // ANCHOR_END: Configuration -pub struct TrackedStructJar +pub struct JarImpl where C: Configuration, { phantom: PhantomData, } -impl Default for TrackedStructJar { +impl Default for JarImpl { fn default() -> Self { Self { phantom: Default::default(), @@ -114,18 +114,18 @@ impl Default for TrackedStructJar { } } -impl Jar for TrackedStructJar { +impl Jar for JarImpl { fn create_ingredients( &self, struct_index: crate::storage::IngredientIndex, ) -> Vec> { - let struct_ingredient = TrackedStructIngredient::new(struct_index); + let struct_ingredient = IngredientImpl::new(struct_index); let struct_map = &struct_ingredient.struct_map.view(); std::iter::once(Box::new(struct_ingredient) as _) .chain( (0..u32::try_from(C::FIELD_DEBUG_NAMES.len()).unwrap()).map(|field_index| { - Box::new(TrackedFieldIngredient::::new( + Box::new(FieldIngredientImpl::::new( struct_index, field_index, struct_map, @@ -149,7 +149,7 @@ pub trait TrackedStructInDb: SalsaStructInDb { /// Unlike normal interners, tracked struct indices can be deleted and reused aggressively: /// when a tracked function re-executes, /// any tracked structs that it created before but did not create this time can be deleted. -pub struct TrackedStructIngredient +pub struct IngredientImpl where C: Configuration, { @@ -236,7 +236,7 @@ where #[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)] pub struct Disambiguator(pub u32); -impl TrackedStructIngredient +impl IngredientImpl where C: Configuration, { @@ -420,7 +420,7 @@ where } } -impl Ingredient for TrackedStructIngredient +impl Ingredient for IngredientImpl where C: Configuration, { @@ -482,7 +482,7 @@ where } } -impl std::fmt::Debug for TrackedStructIngredient +impl std::fmt::Debug for IngredientImpl where C: Configuration, { @@ -493,7 +493,7 @@ where } } -impl IngredientRequiresReset for TrackedStructIngredient +impl IngredientRequiresReset for IngredientImpl where C: Configuration, { diff --git a/src/tracked_struct/tracked_field.rs b/src/tracked_struct/tracked_field.rs index ee754c04..dae34ec2 100644 --- a/src/tracked_struct/tracked_field.rs +++ b/src/tracked_struct/tracked_field.rs @@ -16,7 +16,7 @@ use super::{struct_map::StructMapView, Configuration}; /// Unlike normal interners, tracked struct indices can be deleted and reused aggressively: /// when a tracked function re-executes, /// any tracked structs that it created before but did not create this time can be deleted. -pub struct TrackedFieldIngredient +pub struct FieldIngredientImpl where C: Configuration, { @@ -26,7 +26,7 @@ where struct_map: StructMapView, } -impl TrackedFieldIngredient +impl FieldIngredientImpl where C: Configuration, { @@ -68,7 +68,7 @@ where } } -impl Ingredient for TrackedFieldIngredient +impl Ingredient for FieldIngredientImpl where C: Configuration, { @@ -139,7 +139,7 @@ where } } -impl std::fmt::Debug for TrackedFieldIngredient +impl std::fmt::Debug for FieldIngredientImpl where C: Configuration, { @@ -151,7 +151,7 @@ where } } -impl IngredientRequiresReset for TrackedFieldIngredient +impl IngredientRequiresReset for FieldIngredientImpl where C: Configuration, {