From dbf9820e35cf2d96bd3295e6890e8ef1cb0a060a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 3 Feb 2019 22:15:31 +0300 Subject: make HirDatabase object-safe --- crates/ra_db/src/lib.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'crates/ra_db/src') diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index ca775030d..926cf0bd5 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -19,7 +19,7 @@ pub use crate::{ loc2id::LocationIntener, }; -pub trait CheckCanceled: salsa::Database + panic::RefUnwindSafe { +pub trait CheckCanceled: panic::RefUnwindSafe { /// Aborts current query if there are pending changes. /// /// rust-analyzer needs to be able to answer semantic questions about the @@ -33,16 +33,13 @@ pub trait CheckCanceled: salsa::Database + panic::RefUnwindSafe { /// /// We implement cancellation by panicking with a special value and catching /// it on the API boundary. Salsa explicitly supports this use-case. - fn check_canceled(&self) { - if self.salsa_runtime().is_current_revision_canceled() { - Canceled::throw() - } - } + fn check_canceled(&self); - fn catch_canceled T + panic::UnwindSafe, T>( - &self, - f: F, - ) -> Result { + fn catch_canceled(&self, f: F) -> Result + where + Self: Sized, + F: FnOnce(&Self) -> T + panic::UnwindSafe, + { panic::catch_unwind(|| f(self)).map_err(|err| match err.downcast::() { Ok(canceled) => *canceled, Err(payload) => panic::resume_unwind(payload), @@ -50,6 +47,14 @@ pub trait CheckCanceled: salsa::Database + panic::RefUnwindSafe { } } +impl CheckCanceled for T { + fn check_canceled(&self) { + if self.salsa_runtime().is_current_revision_canceled() { + Canceled::throw() + } + } +} + #[derive(Clone, Copy, Debug)] pub struct FilePosition { pub file_id: FileId, @@ -65,7 +70,7 @@ pub struct FileRange { /// Database which stores all significant input facts: source code and project /// model. Everything else in rust-analyzer is derived from these queries. #[salsa::query_group(SourceDatabaseStorage)] -pub trait SourceDatabase: salsa::Database + CheckCanceled { +pub trait SourceDatabase: CheckCanceled { /// Text of the file. #[salsa::input] fn file_text(&self, file_id: FileId) -> Arc; -- cgit v1.2.3