remove type parameter from ZalsaImpl

This commit is contained in:
Niko Matsakis 2024-07-28 13:01:09 +00:00
parent 85628247e5
commit 34e109d390
2 changed files with 18 additions and 21 deletions

View file

@ -1,4 +1,4 @@
use std::{any::Any, panic::RefUnwindSafe, sync::Arc};
use std::{any::Any, marker::PhantomData, panic::RefUnwindSafe, sync::Arc};
use parking_lot::{Condvar, Mutex};
@ -113,7 +113,7 @@ impl dyn Database {
/// Takes an optional type parameter `U` that allows you to thread your own data.
pub struct DatabaseImpl<U: UserData = ()> {
/// Reference to the database. This is always `Some` except during destruction.
zalsa_impl: Option<Arc<ZalsaImpl<U>>>,
zalsa_impl: Option<Arc<ZalsaImpl>>,
/// Coordination data for cancellation of other handles when `zalsa_mut` is called.
/// This could be stored in ZalsaImpl but it makes things marginally cleaner to keep it separate.
@ -121,6 +121,9 @@ pub struct DatabaseImpl<U: UserData = ()> {
/// Per-thread state
zalsa_local: local_state::LocalState,
/// The `U` is stored as a `dyn Any` in `zalsa_impl`
phantom: PhantomData<U>,
}
impl<U: UserData + Default> Default for DatabaseImpl<U> {
@ -150,13 +153,14 @@ impl<U: UserData> DatabaseImpl<U> {
cvar: Default::default(),
}),
zalsa_local: LocalState::new(),
phantom: PhantomData::<U>,
}
}
/// Access the `Arc<ZalsaImpl>`. This should always be
/// possible as `zalsa_impl` only becomes
/// `None` once we are in the `Drop` impl.
fn zalsa_impl(&self) -> &Arc<ZalsaImpl<U>> {
fn zalsa_impl(&self) -> &Arc<ZalsaImpl> {
self.zalsa_impl.as_ref().unwrap()
}
@ -188,7 +192,7 @@ impl<U: UserData> std::ops::Deref for DatabaseImpl<U> {
type Target = U;
fn deref(&self) -> &U {
self.zalsa_impl().user_data()
self.zalsa_impl().user_data().downcast_ref::<U>().unwrap()
}
}
@ -228,6 +232,7 @@ impl<U: UserData> Clone for DatabaseImpl<U> {
zalsa_impl: self.zalsa_impl.clone(),
coordinate: Arc::clone(&self.coordinate),
zalsa_local: LocalState::new(),
phantom: PhantomData::<U>,
}
}
}

View file

@ -1,4 +1,4 @@
use std::any::TypeId;
use std::any::{Any, TypeId};
use orx_concurrent_vec::ConcurrentVec;
use parking_lot::Mutex;
@ -61,7 +61,7 @@ pub trait Zalsa {
fn report_tracked_write(&mut self, durability: Durability);
}
impl<U: UserData> Zalsa for ZalsaImpl<U> {
impl Zalsa for ZalsaImpl {
fn views(&self) -> &Views {
&self.views_of
}
@ -182,8 +182,8 @@ impl IngredientIndex {
/// The "storage" struct stores all the data for the jars.
/// It is shared between the main database and any active snapshots.
pub(crate) struct ZalsaImpl<U: UserData> {
user_data: U,
pub(crate) struct ZalsaImpl {
user_data: Box<dyn Any + Send + Sync>,
views_of: Views,
@ -210,16 +210,8 @@ pub(crate) struct ZalsaImpl<U: UserData> {
runtime: Runtime,
}
// ANCHOR: default
impl<U: UserData + Default> Default for ZalsaImpl<U> {
fn default() -> Self {
Self::with(Default::default())
}
}
// ANCHOR_END: default
impl<U: UserData> ZalsaImpl<U> {
pub(crate) fn with(user_data: U) -> Self {
impl ZalsaImpl {
pub(crate) fn with<U: UserData>(user_data: U) -> Self {
Self {
views_of: Views::new::<DatabaseImpl<U>>(),
nonce: NONCE.nonce(),
@ -227,12 +219,12 @@ impl<U: UserData> ZalsaImpl<U> {
ingredients_vec: Default::default(),
ingredients_requiring_reset: Default::default(),
runtime: Runtime::default(),
user_data,
user_data: Box::new(user_data),
}
}
pub(crate) fn user_data(&self) -> &U {
&self.user_data
pub(crate) fn user_data(&self) -> &(dyn Any + Send + Sync) {
&*self.user_data
}
/// Triggers a new revision. Invoked automatically when you call `zalsa_mut`