simplelock 0.4.1

Simple abstractions for inter-process synchronization.
Documentation
use crate::*;

/// Possible status a Lock can have.
pub enum LockStatus {
    /// The current process owns the lock.
    Mine,

    /// Last I checked, someone else owns the lock.
    Taken,

    /// Last I checked, no-one has the lock.
    Open,
}

/// A full implementation of a Lock which can handle all
/// LockBuilder configurations.
pub trait ConcreteLock {
    /// Get the currently known lock status.
    fn status(&self) -> SimpleLockResult<LockStatus>;

    /// Attempt to perform the lock.
    fn try_lock(&mut self) -> SimpleLockResult<()>;

    /// Attempt to perform the lock and hang until acquire.
    fn hang_lock(&mut self) -> SimpleLockResult<()>;

    /// Attempt to perform the unlock.
    fn try_unlock(&mut self) -> SimpleLockResult<()>;
}

/// The Simple `Lock` trait.
pub trait Lock {
    /// None if unknown, otherwise will contain the status.
    fn status(&self) -> SimpleLockResult<LockStatus>;

    /// Attempt to perform the lock.
    fn lock(&mut self) -> SimpleLockResult<()>;

    /// Attempt to perform the unlock.
    fn unlock(&mut self) -> SimpleLockResult<()>;
}

impl ConcreteLock for Box<dyn ConcreteLock> {
    fn status(&self) -> SimpleLockResult<LockStatus> {
        self.as_ref().status()
    }

    fn try_lock(&mut self) -> SimpleLockResult<()> {
        self.as_mut().try_lock()
    }

    fn hang_lock(&mut self) -> SimpleLockResult<()> {
        self.as_mut().hang_lock()
    }

    fn try_unlock(&mut self) -> SimpleLockResult<()> {
        self.as_mut().try_unlock()
    }
}

impl Lock for Box<dyn Lock> {
    fn status(&self) -> SimpleLockResult<LockStatus> {
        self.as_ref().status()
    }

    fn lock(&mut self) -> SimpleLockResult<()> {
        self.as_mut().lock()
    }

    fn unlock(&mut self) -> SimpleLockResult<()> {
        self.as_mut().unlock()
    }
}