generational_box/
error.rs1#![allow(clippy::uninlined_format_args, reason = "causes compile error")]
3
4use std::error::Error;
5use std::fmt::Debug;
6use std::fmt::Display;
7
8use crate::GenerationalLocation;
9
10pub type BorrowResult<T = ()> = std::result::Result<T, BorrowError>;
12
13pub type BorrowMutResult<T = ()> = std::result::Result<T, BorrowMutError>;
15
16#[derive(Debug, Clone, PartialEq)]
17pub enum BorrowError {
19 Dropped(ValueDroppedError),
21 AlreadyBorrowedMut(AlreadyBorrowedMutError),
23}
24
25impl Display for BorrowError {
26 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27 match self {
28 BorrowError::Dropped(error) => Display::fmt(error, f),
29 BorrowError::AlreadyBorrowedMut(error) => Display::fmt(error, f),
30 }
31 }
32}
33
34impl Error for BorrowError {}
35
36#[derive(Debug, Clone, PartialEq)]
37pub enum BorrowMutError {
39 Dropped(ValueDroppedError),
41 AlreadyBorrowed(AlreadyBorrowedError),
43 AlreadyBorrowedMut(AlreadyBorrowedMutError),
45}
46
47impl Display for BorrowMutError {
48 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49 match self {
50 BorrowMutError::Dropped(error) => Display::fmt(error, f),
51 BorrowMutError::AlreadyBorrowedMut(error) => Display::fmt(error, f),
52 BorrowMutError::AlreadyBorrowed(error) => Display::fmt(error, f),
53 }
54 }
55}
56
57impl Error for BorrowMutError {}
58
59impl From<BorrowError> for BorrowMutError {
60 fn from(error: BorrowError) -> Self {
61 match error {
62 BorrowError::Dropped(error) => BorrowMutError::Dropped(error),
63 BorrowError::AlreadyBorrowedMut(error) => BorrowMutError::AlreadyBorrowedMut(error),
64 }
65 }
66}
67
68#[derive(Debug, Copy, Clone, PartialEq)]
70pub struct ValueDroppedError {
71 #[cfg(any(debug_assertions, feature = "debug_ownership"))]
72 pub(crate) created_at: &'static std::panic::Location<'static>,
73}
74
75impl ValueDroppedError {
76 #[allow(unused)]
78 pub fn new(created_at: &'static std::panic::Location<'static>) -> Self {
79 Self {
80 #[cfg(any(debug_assertions, feature = "debug_ownership"))]
81 created_at,
82 }
83 }
84
85 #[allow(unused)]
87 pub(crate) fn new_for_location(location: GenerationalLocation) -> Self {
88 Self {
89 #[cfg(any(debug_assertions, feature = "debug_borrows"))]
90 created_at: location.created_at,
91 }
92 }
93}
94
95impl Display for ValueDroppedError {
96 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97 f.write_str("Failed to borrow because the value was dropped.")?;
98 #[cfg(any(debug_assertions, feature = "debug_ownership"))]
99 f.write_fmt(format_args!("created_at: {}", self.created_at))?;
100 Ok(())
101 }
102}
103
104impl std::error::Error for ValueDroppedError {}
105
106#[derive(Debug, Copy, Clone, PartialEq)]
108pub struct AlreadyBorrowedMutError {
109 #[cfg(any(debug_assertions, feature = "debug_borrows"))]
110 pub(crate) borrowed_mut_at: &'static std::panic::Location<'static>,
111}
112
113impl AlreadyBorrowedMutError {
114 #[allow(unused)]
116 pub fn new(borrowed_mut_at: &'static std::panic::Location<'static>) -> Self {
117 Self {
118 #[cfg(any(debug_assertions, feature = "debug_borrows"))]
119 borrowed_mut_at,
120 }
121 }
122}
123
124impl Display for AlreadyBorrowedMutError {
125 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
126 f.write_str("Failed to borrow because the value was already borrowed mutably.")?;
127 #[cfg(any(debug_assertions, feature = "debug_borrows"))]
128 f.write_fmt(format_args!("borrowed_mut_at: {}", self.borrowed_mut_at))?;
129 Ok(())
130 }
131}
132
133impl std::error::Error for AlreadyBorrowedMutError {}
134
135#[derive(Debug, Clone, PartialEq)]
137pub struct AlreadyBorrowedError {
138 #[cfg(any(debug_assertions, feature = "debug_borrows"))]
139 pub(crate) borrowed_at: Vec<&'static std::panic::Location<'static>>,
140}
141
142impl AlreadyBorrowedError {
143 #[allow(unused)]
145 pub fn new(borrowed_at: Vec<&'static std::panic::Location<'static>>) -> Self {
146 Self {
147 #[cfg(any(debug_assertions, feature = "debug_borrows"))]
148 borrowed_at,
149 }
150 }
151}
152
153impl Display for AlreadyBorrowedError {
154 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
155 f.write_str("Failed to borrow mutably because the value was already borrowed immutably.")?;
156 #[cfg(any(debug_assertions, feature = "debug_borrows"))]
157 f.write_str("borrowed_at:")?;
158 #[cfg(any(debug_assertions, feature = "debug_borrows"))]
159 for location in self.borrowed_at.iter() {
160 f.write_fmt(format_args!("\t{}", location))?;
161 }
162 Ok(())
163 }
164}
165
166impl std::error::Error for AlreadyBorrowedError {}