1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use crate::{error::FendError, result::FResult};

/// This trait controls fend's interrupt functionality.
///
/// If the `should_interrupt` method returns `true`, fend will attempt to
/// interrupt the current calculation and return `Err(FendError::Interrupted)`.
///
/// This can be used to implement timeouts or user interrupts via e.g. Ctrl-C.
pub trait Interrupt {
	/// Returns `true` if the current calculation should be interrupted.
	fn should_interrupt(&self) -> bool;
}

pub(crate) fn test_int<I: crate::error::Interrupt>(int: &I) -> FResult<()> {
	if int.should_interrupt() {
		Err(FendError::Interrupted)
	} else {
		Ok(())
	}
}

#[derive(Default)]
pub(crate) struct Never;
impl Interrupt for Never {
	fn should_interrupt(&self) -> bool {
		false
	}
}