fend_core/
interrupt.rs

1use crate::{error::FendError, result::FResult};
2
3/// This trait controls fend's interrupt functionality.
4///
5/// If the `should_interrupt` method returns `true`, fend will attempt to
6/// interrupt the current calculation and return `Err(FendError::Interrupted)`.
7///
8/// This can be used to implement timeouts or user interrupts via e.g. Ctrl-C.
9pub trait Interrupt: Sync {
10	/// Returns `true` if the current calculation should be interrupted.
11	fn should_interrupt(&self) -> bool;
12}
13
14pub(crate) fn test_int<I: crate::error::Interrupt>(int: &I) -> FResult<()> {
15	if int.should_interrupt() {
16		Err(FendError::Interrupted)
17	} else {
18		Ok(())
19	}
20}
21
22#[derive(Default)]
23pub(crate) struct Never;
24impl Interrupt for Never {
25	fn should_interrupt(&self) -> bool {
26		false
27	}
28}