From 08c7a6bd636f22b931e7702f68153b3800228504 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 6 May 2021 19:56:05 +0200 Subject: [PATCH] Include user-readable query keys in cycle errors --- src/lib.rs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 47a0849e..b30003c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,7 @@ pub mod debug; #[doc(hidden)] pub mod plumbing; +use crate::plumbing::DatabaseOps; use crate::plumbing::DerivedQueryStorageOps; use crate::plumbing::InputQueryStorageOps; use crate::plumbing::LruQueryStorageOps; @@ -487,7 +488,8 @@ where /// queries (those with no inputs, or those with more than one /// input) the key will be a tuple. pub fn get(&self, key: Q::Key) -> Q::Value { - self.try_get(key).unwrap_or_else(|err| panic!("{}", err)) + self.try_get(key) + .unwrap_or_else(|err| panic!("{:?}", err.debug(self.db))) } fn try_get(&self, key: Q::Key) -> Result> { @@ -513,7 +515,8 @@ where Q::Storage: plumbing::QueryStorageMassOps, { self.storage.purge(); - }} + } +} /// Return value from [the `query_mut` method] on `Database`. /// Gives access to the `set` method, notably, that is used to @@ -604,6 +607,30 @@ pub struct CycleError { durability: Durability, } +impl CycleError { + fn debug<'a, D: ?Sized>(&'a self, db: &'a D) -> impl Debug + 'a + where + D: DatabaseOps, + { + struct CycleErrorDebug<'a, D: ?Sized> { + db: &'a D, + error: &'a CycleError, + } + + impl<'a, D: ?Sized + DatabaseOps> Debug for CycleErrorDebug<'a, D> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + writeln!(f, "Internal error, cycle detected:\n")?; + for i in &self.error.cycle { + writeln!(f, "{:?}", i.debug(self.db))?; + } + Ok(()) + } + } + + CycleErrorDebug { db, error: self } + } +} + impl fmt::Display for CycleError where K: fmt::Debug,