generational_box/
error.rsuse std::error::Error;
use std::fmt::Debug;
use std::fmt::Display;
use crate::GenerationalLocation;
pub type BorrowResult<T = ()> = std::result::Result<T, BorrowError>;
pub type BorrowMutResult<T = ()> = std::result::Result<T, BorrowMutError>;
#[derive(Debug, Clone, PartialEq)]
pub enum BorrowError {
Dropped(ValueDroppedError),
AlreadyBorrowedMut(AlreadyBorrowedMutError),
}
impl Display for BorrowError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BorrowError::Dropped(error) => Display::fmt(error, f),
BorrowError::AlreadyBorrowedMut(error) => Display::fmt(error, f),
}
}
}
impl Error for BorrowError {}
#[derive(Debug, Clone, PartialEq)]
pub enum BorrowMutError {
Dropped(ValueDroppedError),
AlreadyBorrowed(AlreadyBorrowedError),
AlreadyBorrowedMut(AlreadyBorrowedMutError),
}
impl Display for BorrowMutError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BorrowMutError::Dropped(error) => Display::fmt(error, f),
BorrowMutError::AlreadyBorrowedMut(error) => Display::fmt(error, f),
BorrowMutError::AlreadyBorrowed(error) => Display::fmt(error, f),
}
}
}
impl Error for BorrowMutError {}
impl From<BorrowError> for BorrowMutError {
fn from(error: BorrowError) -> Self {
match error {
BorrowError::Dropped(error) => BorrowMutError::Dropped(error),
BorrowError::AlreadyBorrowedMut(error) => BorrowMutError::AlreadyBorrowedMut(error),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct ValueDroppedError {
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
pub(crate) created_at: &'static std::panic::Location<'static>,
}
impl ValueDroppedError {
#[allow(unused)]
pub fn new(created_at: &'static std::panic::Location<'static>) -> Self {
Self {
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
created_at,
}
}
#[allow(unused)]
pub(crate) fn new_for_location(location: GenerationalLocation) -> Self {
Self {
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
created_at: location.created_at,
}
}
}
impl Display for ValueDroppedError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Failed to borrow because the value was dropped.")?;
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
f.write_fmt(format_args!("created_at: {}", self.created_at))?;
Ok(())
}
}
impl std::error::Error for ValueDroppedError {}
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct AlreadyBorrowedMutError {
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
pub(crate) borrowed_mut_at: &'static std::panic::Location<'static>,
}
impl AlreadyBorrowedMutError {
#[allow(unused)]
pub fn new(borrowed_mut_at: &'static std::panic::Location<'static>) -> Self {
Self {
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrowed_mut_at,
}
}
}
impl Display for AlreadyBorrowedMutError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Failed to borrow because the value was already borrowed mutably.")?;
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
f.write_fmt(format_args!("borrowed_mut_at: {}", self.borrowed_mut_at))?;
Ok(())
}
}
impl std::error::Error for AlreadyBorrowedMutError {}
#[derive(Debug, Clone, PartialEq)]
pub struct AlreadyBorrowedError {
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
pub(crate) borrowed_at: Vec<&'static std::panic::Location<'static>>,
}
impl AlreadyBorrowedError {
#[allow(unused)]
pub fn new(borrowed_at: Vec<&'static std::panic::Location<'static>>) -> Self {
Self {
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrowed_at,
}
}
}
impl Display for AlreadyBorrowedError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Failed to borrow mutably because the value was already borrowed immutably.")?;
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
f.write_str("borrowed_at:")?;
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
for location in self.borrowed_at.iter() {
f.write_fmt(format_args!("\t{}", location))?;
}
Ok(())
}
}
impl std::error::Error for AlreadyBorrowedError {}