fix signal error message and add tests (#2118)

This commit is contained in:
Evan Almloff authored and GitHub committed 2024-03-20 08:45:27 -07:00
1 parent 58f7efafea
commit 9f283f571f
10 files changed
+187 -121

No files matched your search

+38 -5
View File
@@ -2,7 +2,7 @@ use std::error::Error;
use std::fmt::Debug;
use std::fmt::Display;
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
/// An error that can occur when trying to borrow a value.
pub enum BorrowError {
/// The value was dropped.
@@ -22,7 +22,7 @@ impl Display for BorrowError {
impl Error for BorrowError {}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
/// An error that can occur when trying to borrow a value mutably.
pub enum BorrowMutError {
/// The value was dropped.
@@ -46,12 +46,23 @@ impl Display for BorrowMutError {
impl Error for BorrowMutError {}
/// An error that can occur when trying to use a value that has been dropped.
#[derive(Debug, Copy, Clone)]
#[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,
}
}
}
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.")?;
@@ -64,12 +75,23 @@ impl Display for ValueDroppedError {
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)]
#[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.")?;
@@ -82,12 +104,23 @@ impl Display for AlreadyBorrowedMutError {
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)]
#[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.")?;
Whitespace-only changes.
+20 -4
View File
@@ -65,7 +65,7 @@ impl Drop for GenerationalRefBorrowInfo {
pub struct GenerationalRefMut<W> {
pub(crate) inner: W,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
pub(crate) borrow: GenerationalRefMutBorrowInfo,
pub(crate) borrow: GenerationalRefBorrowMutGuard,
}
impl<T, R: DerefMut<Target = T>> GenerationalRefMut<R> {
@@ -77,7 +77,7 @@ impl<T, R: DerefMut<Target = T>> GenerationalRefMut<R> {
Self {
inner,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow,
borrow: borrow.into(),
}
}
}
@@ -105,8 +105,24 @@ pub struct GenerationalRefMutBorrowInfo {
}
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
impl Drop for GenerationalRefMutBorrowInfo {
pub(crate) struct GenerationalRefBorrowMutGuard {
borrow_info: GenerationalRefMutBorrowInfo,
}
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
impl From<GenerationalRefMutBorrowInfo> for GenerationalRefBorrowMutGuard {
fn from(borrow_info: GenerationalRefMutBorrowInfo) -> Self {
Self { borrow_info }
}
}
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
impl Drop for GenerationalRefBorrowMutGuard {
fn drop(&mut self) {
self.borrowed_from.borrowed_mut_at.write().take();
self.borrow_info
.borrowed_from
.borrowed_mut_at
.write()
.take();
}
}
+12 -18
View File
@@ -73,10 +73,7 @@ impl AnyStorage for SyncStorage {
.map(|inner| GenerationalRefMut {
inner,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow: crate::GenerationalRefMutBorrowInfo {
borrowed_from: borrow.borrowed_from,
created_at: borrow.created_at,
},
borrow,
})
}
@@ -147,20 +144,17 @@ impl<T: Sync + Send + 'static> Storage<T> for SyncStorage {
) -> Result<Self::Mut<'static, T>, error::BorrowMutError> {
let write = self.0.write();
RwLockWriteGuard::try_map(write, |any| any.as_mut()?.downcast_mut())
.map_err(|_| {
error::BorrowMutError::Dropped(ValueDroppedError {
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
created_at: at.created_at,
})
})
.map(|guard| {
GenerationalRefMut::new(
guard,
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
at,
)
})
match RwLockWriteGuard::try_map(write, |any| any.as_mut()?.downcast_mut()) {
Ok(guard) => Ok(GenerationalRefMut::new(
guard,
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
at,
)),
Err(_) => Err(error::BorrowMutError::Dropped(ValueDroppedError {
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
created_at: at.created_at,
})),
}
}
fn set(&self, value: T) {
+12 -19
View File
@@ -12,7 +12,6 @@ pub struct UnsyncStorage(RefCell<Option<Box<dyn std::any::Any>>>);
impl<T: 'static> Storage<T> for UnsyncStorage {
fn try_read(
&'static self,
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
at: crate::GenerationalRefBorrowInfo,
) -> Result<Self::Ref<'static, T>, error::BorrowError> {
@@ -26,20 +25,17 @@ impl<T: 'static> Storage<T> for UnsyncStorage {
error::BorrowError::AlreadyBorrowedMut(error::AlreadyBorrowedMutError {})
})?;
Ref::filter_map(borrow, |any| any.as_ref()?.downcast_ref())
.map_err(|_| {
error::BorrowError::Dropped(error::ValueDroppedError {
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
created_at: at.created_at,
})
})
.map(|guard| {
GenerationalRef::new(
guard,
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
at,
)
})
match Ref::filter_map(borrow, |any| any.as_ref()?.downcast_ref()) {
Ok(guard) => Ok(GenerationalRef::new(
guard,
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
at,
)),
Err(_) => Err(error::BorrowError::Dropped(error::ValueDroppedError {
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
created_at: at.created_at,
})),
}
}
fn try_write(
@@ -136,10 +132,7 @@ impl AnyStorage for UnsyncStorage {
.map(|inner| GenerationalRefMut {
inner,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow: crate::GenerationalRefMutBorrowInfo {
borrowed_from: borrow.borrowed_from,
created_at: borrow.created_at,
},
borrow,
})
}