podo-core-driver 0.4.4

Podo Driver FFI
Documentation
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use super::message::*;
use crate::error::RuntimeError;

#[cfg(feature = "util-serde")]
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "util-serde", derive(Serialize, Deserialize))]
pub struct AliveFlag {
    flag: Arc<AtomicBool>,
}

impl AliveFlag {
    #[inline]
    pub fn new(alive: bool) -> Self {
        Self {
            flag: AtomicBool::new(alive).into(),
        }
    }

    #[inline]
    pub fn start(&self) -> Result<(), RuntimeError> {
        match self.flag.swap(true, Ordering::Relaxed) {
            true => RuntimeError::expect(ERR_STARTED),
            false => Ok(()),
        }
    }

    #[inline]
    pub fn stop(&self) -> Result<(), RuntimeError> {
        match self.flag.swap(false, Ordering::Relaxed) {
            false => RuntimeError::expect(ERR_STOPPED),
            true => Ok(()),
        }
    }

    #[inline]
    pub fn is_running(&self) -> bool {
        self.flag.load(Ordering::Relaxed)
    }

    #[inline]
    pub fn assert_running(&self) -> Result<(), RuntimeError> {
        match self.is_running() {
            true => Ok(()),
            false => RuntimeError::expect(ERR_NOT_STARTED),
        }
    }
}