From c85edccc1edf12346528b98f0ee4a10005041fa4 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Sat, 29 Sep 2018 06:01:44 -0400 Subject: [PATCH] remove the horrible `dyn_descriptor` and generate an enum --- src/dyn_descriptor.rs | 46 ------------------------------------------- src/lib.rs | 36 +++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 54 deletions(-) delete mode 100644 src/dyn_descriptor.rs diff --git a/src/dyn_descriptor.rs b/src/dyn_descriptor.rs deleted file mode 100644 index 5217cef7..00000000 --- a/src/dyn_descriptor.rs +++ /dev/null @@ -1,46 +0,0 @@ -use crate::Query; -use crate::QueryContext; -use crate::QueryTable; -use rustc_hash::FxHashMap; -use std::any::{Any, TypeId}; -use std::cell::RefCell; -use std::collections::hash_map::Entry; -use std::fmt::Debug; -use std::fmt::Display; -use std::fmt::Write; -use std::hash::Hash; - -// Total hack for now: assume that the Debug string -// for the key, combined with the type-id of the query, -// is sufficient for an equality comparison. - -/// A simple-to-use query descriptor that is meant only for dumping -/// out cycle stack errors and not for any real recovery; also, not -/// especially efficient. -#[derive(PartialEq, Eq)] -pub struct DynDescriptor { - type_id: TypeId, - debug_string: String, -} - -impl DynDescriptor { - pub fn from_key(_query: &QC, key: &Q::Key) -> DynDescriptor - where - QC: QueryContext, - Q: Query, - { - let type_id = TypeId::of::(); - let query = Q::default(); - let debug_string = format!("Query `{:?}` applied to `{:?}`", query, key); - DynDescriptor { - type_id, - debug_string, - } - } -} - -impl std::fmt::Debug for DynDescriptor { - fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(fmt, "{}", self.debug_string) - } -} diff --git a/src/lib.rs b/src/lib.rs index e8b3960c..c586a95d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,6 @@ use std::fmt::Display; use std::fmt::Write; use std::hash::Hash; -pub mod dyn_descriptor; pub mod memoized; pub mod runtime; pub mod transparent; @@ -48,7 +47,7 @@ pub trait QueryContextStorageTypes { /// At runtime, it can be implemented in various ways: a monster enum /// works for a fixed set of queries, but a boxed trait object is good /// for a more open-ended option. - type QueryDescriptor: Debug + Eq; + type QueryDescriptor: Debug + Eq + Hash; /// Defines the "storage type", where all the query data is kept. /// This type is defined by the `query_context_storage` macro. @@ -331,7 +330,6 @@ macro_rules! query_context_storage { )* } ) => { - #[allow(non_snake_case)] #[derive(Default)] $(#[$attr])* $v struct $Storage { @@ -342,8 +340,28 @@ macro_rules! query_context_storage { )* } + /// Identifies a query and its key. You are not meant to name + /// this type directly or use its fields etc. It is a + /// **private query descriptor type generated by salsa** and + /// its exact structure is subject to change. Sadly, I don't + /// know any way to hide this with hygiene, so use `__` + /// instead. + #[derive(Debug, PartialEq, Eq, Hash)] + $v struct __SalsaQueryDescriptor { + kind: __SalsaQueryDescriptorKind + } + + #[derive(Debug, PartialEq, Eq, Hash)] + enum __SalsaQueryDescriptorKind { + $( + $( + $query_method(<$QueryType as $crate::Query<$QueryContext>>::Key), + )* + )* + } + impl $crate::QueryContextStorageTypes for $QueryContext { - type QueryDescriptor = $crate::dyn_descriptor::DynDescriptor; + type QueryDescriptor = __SalsaQueryDescriptor; type QueryStorage = $Storage; } @@ -356,10 +374,12 @@ macro_rules! query_context_storage { $crate::QueryTable::new( self, &$crate::QueryContext::salsa_storage(self).$query_method, - $crate::dyn_descriptor::DynDescriptor::from_key::< - Self, - $QueryType, - >, + |_, key| { + let key = std::clone::Clone::clone(key); + __SalsaQueryDescriptor { + kind: __SalsaQueryDescriptorKind::$query_method(key), + } + }, ) } )*