2018-12-27 03:59:30 -06:00
|
|
|
//! Utility types to support cancellation.
|
|
|
|
//!
|
|
|
|
//! In a typical IDE use-case, requests and modification happen concurrently, as
|
|
|
|
//! in the following scenario:
|
|
|
|
//!
|
|
|
|
//! * user types a character,
|
|
|
|
//! * a syntax highlighting process is started
|
|
|
|
//! * user types next character, while syntax highlighting *is still in
|
|
|
|
//! progress*.
|
|
|
|
//!
|
2019-01-08 17:47:12 -06:00
|
|
|
//! In this situation, we want to react to modification as quickly as possible.
|
2018-12-27 03:59:30 -06:00
|
|
|
//! At the same time, in-progress results are not very interesting, because they
|
|
|
|
//! are invalidated by the edit anyway. So, we first cancel all in-flight
|
2019-01-08 17:47:12 -06:00
|
|
|
//! requests, and then apply modification knowing that it won't interfere with
|
|
|
|
//! any background processing (this bit is handled by salsa, see the
|
2018-12-27 03:59:30 -06:00
|
|
|
//! `BaseDatabase::check_canceled` method).
|
|
|
|
|
2019-02-11 10:18:27 -06:00
|
|
|
/// An "error" signifying that the operation was canceled.
|
2018-12-30 15:38:50 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2018-12-27 03:59:30 -06:00
|
|
|
pub struct Canceled {
|
2018-12-30 15:38:50 -06:00
|
|
|
_private: (),
|
2018-12-27 03:59:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Canceled {
|
|
|
|
pub(crate) fn new() -> Canceled {
|
2018-12-30 15:38:50 -06:00
|
|
|
Canceled { _private: () }
|
2018-12-27 03:59:30 -06:00
|
|
|
}
|
2019-01-10 03:20:32 -06:00
|
|
|
|
|
|
|
pub fn throw() -> ! {
|
2019-01-10 04:06:42 -06:00
|
|
|
// We use resume and not panic here to avoid running the panic
|
|
|
|
// hook (that is, to avoid collecting and printing backtrace).
|
2019-01-10 03:20:32 -06:00
|
|
|
std::panic::resume_unwind(Box::new(Canceled::new()))
|
|
|
|
}
|
2018-12-27 03:59:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for Canceled {
|
|
|
|
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
fmt.write_str("canceled")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Canceled {
|
|
|
|
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2018-12-30 15:38:50 -06:00
|
|
|
write!(fmt, "Canceled")
|
2018-12-27 03:59:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for Canceled {}
|