Skip to content

Commit 34e109d

Browse files
committed
remove type parameter from ZalsaImpl
1 parent 8562824 commit 34e109d

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

src/database.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{any::Any, panic::RefUnwindSafe, sync::Arc};
1+
use std::{any::Any, marker::PhantomData, panic::RefUnwindSafe, sync::Arc};
22

33
use parking_lot::{Condvar, Mutex};
44

@@ -113,14 +113,17 @@ impl dyn Database {
113113
/// Takes an optional type parameter `U` that allows you to thread your own data.
114114
pub struct DatabaseImpl<U: UserData = ()> {
115115
/// Reference to the database. This is always `Some` except during destruction.
116-
zalsa_impl: Option<Arc<ZalsaImpl<U>>>,
116+
zalsa_impl: Option<Arc<ZalsaImpl>>,
117117

118118
/// Coordination data for cancellation of other handles when `zalsa_mut` is called.
119119
/// This could be stored in ZalsaImpl but it makes things marginally cleaner to keep it separate.
120120
coordinate: Arc<Coordinate>,
121121

122122
/// Per-thread state
123123
zalsa_local: local_state::LocalState,
124+
125+
/// The `U` is stored as a `dyn Any` in `zalsa_impl`
126+
phantom: PhantomData<U>,
124127
}
125128

126129
impl<U: UserData + Default> Default for DatabaseImpl<U> {
@@ -150,13 +153,14 @@ impl<U: UserData> DatabaseImpl<U> {
150153
cvar: Default::default(),
151154
}),
152155
zalsa_local: LocalState::new(),
156+
phantom: PhantomData::<U>,
153157
}
154158
}
155159

156160
/// Access the `Arc<ZalsaImpl>`. This should always be
157161
/// possible as `zalsa_impl` only becomes
158162
/// `None` once we are in the `Drop` impl.
159-
fn zalsa_impl(&self) -> &Arc<ZalsaImpl<U>> {
163+
fn zalsa_impl(&self) -> &Arc<ZalsaImpl> {
160164
self.zalsa_impl.as_ref().unwrap()
161165
}
162166

@@ -188,7 +192,7 @@ impl<U: UserData> std::ops::Deref for DatabaseImpl<U> {
188192
type Target = U;
189193

190194
fn deref(&self) -> &U {
191-
self.zalsa_impl().user_data()
195+
self.zalsa_impl().user_data().downcast_ref::<U>().unwrap()
192196
}
193197
}
194198

@@ -228,6 +232,7 @@ impl<U: UserData> Clone for DatabaseImpl<U> {
228232
zalsa_impl: self.zalsa_impl.clone(),
229233
coordinate: Arc::clone(&self.coordinate),
230234
zalsa_local: LocalState::new(),
235+
phantom: PhantomData::<U>,
231236
}
232237
}
233238
}

src/storage.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::any::TypeId;
1+
use std::any::{Any, TypeId};
22

33
use orx_concurrent_vec::ConcurrentVec;
44
use parking_lot::Mutex;
@@ -61,7 +61,7 @@ pub trait Zalsa {
6161
fn report_tracked_write(&mut self, durability: Durability);
6262
}
6363

64-
impl<U: UserData> Zalsa for ZalsaImpl<U> {
64+
impl Zalsa for ZalsaImpl {
6565
fn views(&self) -> &Views {
6666
&self.views_of
6767
}
@@ -182,8 +182,8 @@ impl IngredientIndex {
182182

183183
/// The "storage" struct stores all the data for the jars.
184184
/// It is shared between the main database and any active snapshots.
185-
pub(crate) struct ZalsaImpl<U: UserData> {
186-
user_data: U,
185+
pub(crate) struct ZalsaImpl {
186+
user_data: Box<dyn Any + Send + Sync>,
187187

188188
views_of: Views,
189189

@@ -210,29 +210,21 @@ pub(crate) struct ZalsaImpl<U: UserData> {
210210
runtime: Runtime,
211211
}
212212

213-
// ANCHOR: default
214-
impl<U: UserData + Default> Default for ZalsaImpl<U> {
215-
fn default() -> Self {
216-
Self::with(Default::default())
217-
}
218-
}
219-
// ANCHOR_END: default
220-
221-
impl<U: UserData> ZalsaImpl<U> {
222-
pub(crate) fn with(user_data: U) -> Self {
213+
impl ZalsaImpl {
214+
pub(crate) fn with<U: UserData>(user_data: U) -> Self {
223215
Self {
224216
views_of: Views::new::<DatabaseImpl<U>>(),
225217
nonce: NONCE.nonce(),
226218
jar_map: Default::default(),
227219
ingredients_vec: Default::default(),
228220
ingredients_requiring_reset: Default::default(),
229221
runtime: Runtime::default(),
230-
user_data,
222+
user_data: Box::new(user_data),
231223
}
232224
}
233225

234-
pub(crate) fn user_data(&self) -> &U {
235-
&self.user_data
226+
pub(crate) fn user_data(&self) -> &(dyn Any + Send + Sync) {
227+
&*self.user_data
236228
}
237229

238230
/// Triggers a new revision. Invoked automatically when you call `zalsa_mut`

0 commit comments

Comments
 (0)