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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// Copyright (c) 2025-2026 Ćukasz Szpakowski
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
//! An interruption module.
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use crate::ctrlc;
use crate::error::*;
/// A trait of interruption checker.
///
/// The interruption checker is used by a `checkintr` built-in function to check whether an
/// interruption is occurred. If the interruption is occurred, this built-in function returns an
/// interruption error.
pub trait IntrCheck
{
/// Checks whether an interruption is occurred.
///
/// This method returns an interruption error if the interruption is occurred.
fn check(&self) -> Result<()>;
}
/// A structure of empty interruption checker.
///
/// The empty interruption checker is dummy that ignores interruptions.
#[derive(Copy, Clone, Debug)]
pub struct EmptyIntrChecker;
impl EmptyIntrChecker
{
/// Create an empty interruption checker.
pub fn new() -> Self
{ EmptyIntrChecker }
}
impl IntrCheck for EmptyIntrChecker
{
fn check(&self) -> Result<()>
{ Ok(()) }
}
static INTR_FLAG: AtomicBool = AtomicBool::new(false);
/// A structure of `Ctrl-C` interruption checker.
///
/// The `Ctrl-C` interruption checker checks whether keys `Ctrl-C` are pressed. If the keys
/// `Ctrl-C` is pressed, the `Ctrl-C` interruption checker interprets it as an interruption.
#[derive(Copy, Clone, Debug)]
pub struct CtrlCIntrChecker;
impl CtrlCIntrChecker
{
/// Creates an Ctrl-C` interruption checker.
pub fn new() -> Self
{ CtrlCIntrChecker }
/// Initializes the Ctrl-C` interruption checker.
pub fn initialize() -> Result<()>
{
match ctrlc::set_handler(move || INTR_FLAG.store(true, Ordering::SeqCst)) {
Ok(()) => Ok(()),
Err(err) => Err(Error::Ctrlc(err)),
}
}
/// Resets the Ctrl-C` interruption checker.
pub fn reset()
{ INTR_FLAG.store(false, Ordering::SeqCst); }
}
impl IntrCheck for CtrlCIntrChecker
{
fn check(&self) -> Result<()>
{
if INTR_FLAG.swap(false, Ordering::SeqCst) {
Err(Error::Intr)
} else {
Ok(())
}
}
}