pub mod map;
pub mod queue;
pub mod scalar;
pub mod semaphore;
pub mod set;
mod guard;
mod range;
use std::fmt;
#[derive(Clone, Eq, PartialEq)]
pub enum Error {
Committed,
Conflict,
Outdated,
WouldBlock,
Background(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Committed => f.write_str("cannot acquire an exclusive lock after committing"),
Self::Conflict => {
f.write_str("there is a conflicting transactional lock on this resource")
}
Self::Outdated => f.write_str("the value has already been finalized"),
Self::WouldBlock => f.write_str("synchronous lock acquisition failed"),
Self::Background(cause) => write!(f, "an error occured in a background task: {cause}"),
}
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl std::error::Error for Error {}
impl From<tokio::task::JoinError> for Error {
fn from(cause: tokio::task::JoinError) -> Self {
Self::Background(cause.to_string())
}
}
impl From<tokio::sync::AcquireError> for Error {
fn from(_: tokio::sync::AcquireError) -> Self {
Self::Outdated
}
}
impl From<tokio::sync::TryAcquireError> for Error {
fn from(cause: tokio::sync::TryAcquireError) -> Self {
match cause {
tokio::sync::TryAcquireError::Closed => Error::Outdated,
tokio::sync::TryAcquireError::NoPermits => Error::WouldBlock,
}
}
}
type Result<T> = std::result::Result<T, Error>;