Skip to content

Commit 6a22568

Browse files
authored
Have salsa not depend on salsa-macros (#750)
That way salsa can build without having to build the proc-macro ecosystem, improving build times for dependants of salsa
1 parent 8b8a50a commit 6a22568

File tree

5 files changed

+61
-39
lines changed

5 files changed

+61
-39
lines changed

Cargo.toml

+11-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ description = "A generic framework for on-demand, incrementalized computation (e
1010

1111
[dependencies]
1212
salsa-macro-rules = { version = "0.18.0", path = "components/salsa-macro-rules" }
13-
salsa-macros = { version = "0.18.0", path = "components/salsa-macros" }
13+
salsa-macros = { version = "0.18.0", path = "components/salsa-macros", optional = true }
1414

1515
boxcar = "0.2.9"
1616
crossbeam-queue = "0.3.11"
@@ -22,7 +22,7 @@ parking_lot = "0.12"
2222
portable-atomic = "1"
2323
rustc-hash = "2"
2424
smallvec = "1"
25-
tracing = "0.1"
25+
tracing = { version = "0.1", default-features = false, features = ["std"] }
2626

2727
# parallel map
2828
rayon = { version = "1.10.0", optional = true }
@@ -31,9 +31,17 @@ rayon = { version = "1.10.0", optional = true }
3131
compact_str = { version = "0.8", optional = true }
3232

3333
[features]
34-
default = ["salsa_unstable", "rayon"]
34+
default = ["salsa_unstable", "rayon", "macros"]
3535
# FIXME: remove `salsa_unstable` before 1.0.
3636
salsa_unstable = []
37+
macros = ["dep:salsa-macros"]
38+
39+
# This interlocks the `salsa-macros` and `salsa` versions together
40+
# preventing scenarios where they could diverge in a given project
41+
# which may ultimately result in odd issues due to the proc-macro
42+
# output mismatching with the declarative macro inputs
43+
[target.'cfg(any())'.dependencies]
44+
salsa-macros = { version = "=0.18.0", path = "components/salsa-macros" }
3745

3846
[dev-dependencies]
3947
# examples

src/database.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::{
77

88
/// The trait implemented by all Salsa databases.
99
/// You can create your own subtraits of this trait using the `#[salsa::db]`(`crate::db`) procedural macro.
10-
#[crate::db]
1110
pub trait Database: Send + AsDynDatabase + Any + ZalsaDatabase {
1211
/// This function is invoked by the salsa runtime at various points during execution.
1312
/// You can customize what happens by implementing the [`UserData`][] trait.
@@ -89,6 +88,21 @@ pub trait Database: Send + AsDynDatabase + Any + ZalsaDatabase {
8988
{
9089
crate::attach::attach(self, || op(self))
9190
}
91+
92+
#[doc(hidden)]
93+
fn zalsa_register_downcaster(&self) {
94+
// The no-op downcaster is special cased in view caster construction.
95+
}
96+
97+
#[doc(hidden)]
98+
#[inline(always)]
99+
unsafe fn downcast(db: &dyn Database) -> &dyn Database
100+
where
101+
Self: Sized,
102+
{
103+
// No-op
104+
db
105+
}
92106
}
93107

94108
/// Upcast to a `dyn Database`.

src/database_impl.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::{self as salsa, Database, Event, Storage};
1+
use crate::{storage::HasStorage, Database, Event, Storage};
22

3-
#[salsa::db]
43
/// Default database implementation that you can use if you don't
54
/// require any custom user data.
65
#[derive(Default, Clone)]
@@ -19,10 +18,23 @@ impl DatabaseImpl {
1918
}
2019
}
2120

22-
#[salsa::db]
2321
impl Database for DatabaseImpl {
2422
/// Default behavior: tracing debug log the event.
2523
fn salsa_event(&self, event: &dyn Fn() -> Event) {
2624
tracing::debug!("salsa_event({:?})", event());
2725
}
2826
}
27+
28+
// # Safety
29+
//
30+
// The `storage` and `storage_mut` fields return a reference to the same
31+
// storage field owned by `self`.
32+
unsafe impl HasStorage for DatabaseImpl {
33+
fn storage(&self) -> &Storage<Self> {
34+
&self.storage
35+
}
36+
37+
fn storage_mut(&mut self) -> &mut Storage<Self> {
38+
&mut self.storage
39+
}
40+
}

src/lib.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#![forbid(unsafe_op_in_unsafe_fn)]
22

3-
extern crate self as salsa;
4-
53
mod accumulator;
64
mod active_query;
75
mod array;
@@ -54,13 +52,8 @@ pub use self::zalsa::IngredientIndex;
5452
pub use crate::attach::with_attached_database;
5553
#[cfg(feature = "rayon")]
5654
pub use par_map::par_map;
57-
pub use salsa_macros::accumulator;
58-
pub use salsa_macros::db;
59-
pub use salsa_macros::input;
60-
pub use salsa_macros::interned;
61-
pub use salsa_macros::tracked;
62-
pub use salsa_macros::Supertype;
63-
pub use salsa_macros::Update;
55+
#[cfg(feature = "macros")]
56+
pub use salsa_macros::{accumulator, db, input, interned, tracked, Supertype, Update};
6457

6558
pub mod prelude {
6659
pub use crate::Accumulator;

src/views.rs

+18-23
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ struct ViewCaster {
2424
cast: ErasedDatabaseDownCasterSig,
2525
}
2626

27+
impl ViewCaster {
28+
fn new<DbView: ?Sized + Any>(func: unsafe fn(&dyn Database) -> &DbView) -> ViewCaster {
29+
ViewCaster {
30+
target_type_id: TypeId::of::<DbView>(),
31+
type_name: std::any::type_name::<DbView>(),
32+
// SAFETY: We are type erasing for storage, taking care of unerasing before we call
33+
// the function pointer.
34+
cast: unsafe {
35+
std::mem::transmute::<DatabaseDownCasterSig<DbView>, ErasedDatabaseDownCasterSig>(
36+
func,
37+
)
38+
},
39+
}
40+
}
41+
}
42+
2743
type ErasedDatabaseDownCasterSig = unsafe fn(&dyn Database) -> *const ();
2844
type DatabaseDownCasterSig<DbView> = unsafe fn(&dyn Database) -> &DbView;
2945

@@ -55,18 +71,7 @@ impl Views {
5571
let source_type_id = TypeId::of::<Db>();
5672
let view_casters = boxcar::Vec::new();
5773
// special case the no-op transformation, that way we skip out on reconstructing the wide pointer
58-
view_casters.push(ViewCaster {
59-
target_type_id: TypeId::of::<dyn Database>(),
60-
type_name: std::any::type_name::<dyn Database>(),
61-
// SAFETY: We are type erasing for storage, taking care of unerasing before we call
62-
// the function pointer.
63-
cast: unsafe {
64-
std::mem::transmute::<
65-
DatabaseDownCasterSig<dyn Database>,
66-
ErasedDatabaseDownCasterSig,
67-
>(|db| db)
68-
},
69-
});
74+
view_casters.push(ViewCaster::new::<dyn Database>(|db| db));
7075
Self {
7176
source_type_id,
7277
view_casters,
@@ -83,17 +88,7 @@ impl Views {
8388
{
8489
return;
8590
}
86-
self.view_casters.push(ViewCaster {
87-
target_type_id,
88-
type_name: std::any::type_name::<DbView>(),
89-
// SAFETY: We are type erasing for storage, taking care of unerasing before we call
90-
// the function pointer.
91-
cast: unsafe {
92-
std::mem::transmute::<DatabaseDownCasterSig<DbView>, ErasedDatabaseDownCasterSig>(
93-
func,
94-
)
95-
},
96-
});
91+
self.view_casters.push(ViewCaster::new::<DbView>(func));
9792
}
9893

9994
/// Retrieve an downcaster function from `dyn Database` to `dyn DbView`.

0 commit comments

Comments
 (0)