generational_box/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::error::Error;
use std::fmt::Debug;
use std::fmt::Display;

use crate::GenerationalLocation;

/// A result that can be returned from a borrow operation.
pub type BorrowResult<T = ()> = std::result::Result<T, BorrowError>;

/// A result that can be returned from a borrow mut operation.
pub type BorrowMutResult<T = ()> = std::result::Result<T, BorrowMutError>;

#[derive(Debug, Clone, PartialEq)]
/// An error that can occur when trying to borrow a value.
pub enum BorrowError {
    /// The value was dropped.
    Dropped(ValueDroppedError),
    /// The value was already borrowed mutably.
    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)]
/// An error that can occur when trying to borrow a value mutably.
pub enum BorrowMutError {
    /// The value was dropped.
    Dropped(ValueDroppedError),
    /// The value was already borrowed.
    AlreadyBorrowed(AlreadyBorrowedError),
    /// The value was already borrowed mutably.
    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),
        }
    }
}

/// An error that can occur when trying to use a value that has been dropped.
#[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 {
    /// Create a new `ValueDroppedError`.
    #[allow(unused)]
    pub fn new(created_at: &'static std::panic::Location<'static>) -> Self {
        Self {
            #[cfg(any(debug_assertions, feature = "debug_ownership"))]
            created_at,
        }
    }

    /// Create a new `ValueDroppedError` for a [`GenerationalLocation`].
    #[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 {}

/// An error that can occur when trying to borrow a value that has already been borrowed mutably.
#[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 {
    /// Create a new `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 {}

/// An error that can occur when trying to borrow a value mutably that has already been borrowed immutably.
#[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 {
    /// Create a new `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 {}