interrupt_support/interruptee.rs
1/* This Source Code Form is subject to the terms of the Mozilla Public
2License, v. 2.0. If a copy of the MPL was not distributed with this
3* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use crate::Interrupted;
6
7/// Represents the state of something that may be interrupted. Decoupled from
8/// the interrupt mechanics so that things which want to check if they have been
9/// interrupted are simpler.
10pub trait Interruptee {
11 fn was_interrupted(&self) -> bool;
12
13 fn err_if_interrupted(&self) -> Result<(), Interrupted> {
14 if self.was_interrupted() {
15 return Err(Interrupted);
16 }
17 Ok(())
18 }
19}
20
21/// A convenience implementation, should only be used in tests.
22pub struct NeverInterrupts;
23
24impl Interruptee for NeverInterrupts {
25 #[inline]
26 fn was_interrupted(&self) -> bool {
27 false
28 }
29}