use core::marker::PhantomData;

use crate::Architecture;

/// Token type used to prevent IRQs from firing during some critical section. Normal IRQ operation
/// (if enabled before) is resumed when [IrqGuard]'s lifetime is over.
pub struct IrqGuard<A: Architecture>(bool, PhantomData<A>);

// IrqGuard impls
impl<A: Architecture> IrqGuard<A> {
    /// Saves the current IRQ state and masks them
    pub fn acquire() -> Self {
        let mask = unsafe { A::set_interrupt_mask(true) };
        Self(mask, PhantomData)
    }
}

impl<A: Architecture> Drop for IrqGuard<A> {
    fn drop(&mut self) {
        unsafe {
            A::set_interrupt_mask(self.0);
        }
    }
}
