molframe-core 0.5.1

Core types, storage and analysis contracts for molframe
Documentation
//! Cooperative cancellation shared by all stages of an execution.

use std::fmt;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

/// A cheap, thread-safe cooperative cancellation handle.
#[derive(Clone, Debug, Default)]
pub struct CancellationToken {
    cancelled: Arc<AtomicBool>,
}

impl CancellationToken {
    /// Creates an active token.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Requests cancellation for this token and all of its clones.
    pub fn cancel(&self) {
        self.cancelled.store(true, Ordering::Release);
    }

    /// Reports whether cancellation has been requested.
    #[must_use]
    pub fn is_cancelled(&self) -> bool {
        self.cancelled.load(Ordering::Acquire)
    }

    /// Converts the current state into a branch-friendly result.
    ///
    /// # Errors
    ///
    /// Returns [`Cancelled`] after this token or one of its clones is cancelled.
    pub fn check(&self) -> Result<(), Cancelled> {
        if self.is_cancelled() {
            return Err(Cancelled);
        }
        Ok(())
    }
}

/// An execution stopped after cooperative cancellation was requested.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Cancelled;

impl fmt::Display for Cancelled {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("execution cancelled")
    }
}

impl std::error::Error for Cancelled {}

#[cfg(test)]
#[path = "cancellation_tests.rs"]
mod tests;