flexcell 0.1.0-alpha.1

A flexible cell that allows safe circumvention of double borrow issues.
Documentation
use core::error::Error;
use core::fmt;

/// An error indicating that an attempt was made to borrow a value immutably while it was already mutably borrowed or the cell is empty.
///
/// This error is returned by methods like [`FlexCell::try_with`](crate::FlexCell::try_with) when the value inside the `FlexCell` is already mutably borrowed or the cell does not contain a value.
#[derive(Debug)]
pub struct BorrowError;

impl fmt::Display for BorrowError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Cannot borrow the value because it is already exclusively borrowed or the cell does not contain a value.",
        )
    }
}

impl Error for BorrowError {}

/// An error indicating that an attempt was made to borrow a value mutably while it was already borrowed or the cell is empty.
///
/// This error is returned by methods like [`FlexCell::try_with_mut`](crate::FlexCell::try_with_mut) when the value inside the `FlexCell` is already borrowed (either immutably or mutably) or the cell does not contain a value.
#[derive(Debug)]
pub struct BorrowMutError;

impl fmt::Display for BorrowMutError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Cannot exclusively borrow the value because it is already borrowed or the cell does not contain a value.",
        )
    }
}

impl Error for BorrowMutError {}

/// An error indicating that an attempt was made to consume a value that is not fully owned by the `FlexCell`.
///
/// This error is returned by methods like [`FlexCell::try_into_inner`](crate::FlexCell::try_into_inner), [`FlexCell::try_take`](crate::FlexCell::try_take) and [`FlexCell::try_replace`](crate::FlexCell::try_replace) when the value inside the `FlexCell` is already borrowed or the cell does not fully own the value.
#[derive(Debug)]
pub struct ConsumeError;

impl fmt::Display for ConsumeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Cannot consume the value because the cell does not fully own it.",
        )
    }
}

impl Error for ConsumeError {}