don't panic in try variants even in release mode

This commit is contained in:
Evan Almloff 2024-01-22 08:32:13 -06:00
parent 5167cad689
commit a10f9496c7
2 changed files with 16 additions and 9 deletions

View file

@ -90,13 +90,14 @@ impl<T: Sync + Send + 'static> Storage<T> for SyncStorage {
at: crate::GenerationalRefBorrowInfo,
) -> Result<Self::Ref, error::BorrowError> {
let read = self.0.try_read();
// .ok_or_else(|| at.borrowed_from.borrow_error())?;
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
let read = read.ok_or_else(|| at.borrowed_from.borrow_error())?;
#[cfg(not(any(debug_assertions, feature = "debug_ownership")))]
let read = read.unwrap();
let read = read.ok_or_else(|| {
error::BorrowError::AlreadyBorrowedMut(error::AlreadyBorrowedMutError {})
})?;
RwLockReadGuard::try_map(read, |any| any.as_ref()?.downcast_ref())
.map_err(|_| {
@ -127,7 +128,9 @@ impl<T: Sync + Send + 'static> Storage<T> for SyncStorage {
let write = write.ok_or_else(|| at.borrowed_from.borrow_mut_error())?;
#[cfg(not(any(debug_assertions, feature = "debug_ownership")))]
let write = write.unwrap();
let write = write.ok_or_else(|| {
error::BorrowMutError::AlreadyBorrowed(error::AlreadyBorrowedError {})
})?;
RwLockWriteGuard::try_map(write, |any| any.as_mut()?.downcast_mut())
.map_err(|_| {

View file

@ -1,4 +1,5 @@
use crate::{
error,
references::{GenerationalRef, GenerationalRefMut},
AnyStorage, Mappable, MappableMut, MemoryLocation, MemoryLocationInner, Storage,
};
@ -53,18 +54,20 @@ impl<T: 'static> Storage<T> for UnsyncStorage {
created_at: &'static std::panic::Location<'static>,
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
at: crate::GenerationalRefBorrowInfo,
) -> Result<Self::Ref, crate::error::BorrowError> {
) -> Result<Self::Ref, error::BorrowError> {
let borrow = self.0.try_borrow();
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
let borrow = borrow.map_err(|_| at.borrowed_from.borrow_error())?;
#[cfg(not(any(debug_assertions, feature = "debug_ownership")))]
let borrow = borrow.unwrap();
let borrow = borrow.map_err(|_| {
error::BorrowError::AlreadyBorrowedMut(error::AlreadyBorrowedMutError {})
})?;
Ref::filter_map(borrow, |any| any.as_ref()?.downcast_ref())
.map_err(|_| {
crate::error::BorrowError::Dropped(crate::error::ValueDroppedError {
error::BorrowError::Dropped(error::ValueDroppedError {
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
created_at,
})
@ -84,18 +87,19 @@ impl<T: 'static> Storage<T> for UnsyncStorage {
created_at: &'static std::panic::Location<'static>,
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
at: crate::GenerationalRefMutBorrowInfo,
) -> Result<Self::Mut, crate::error::BorrowMutError> {
) -> Result<Self::Mut, error::BorrowMutError> {
let borrow = self.0.try_borrow_mut();
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
let borrow = borrow.map_err(|_| at.borrowed_from.borrow_mut_error())?;
#[cfg(not(any(debug_assertions, feature = "debug_ownership")))]
let borrow = borrow.unwrap();
let borrow = borrow
.map_err(|_| error::BorrowMutError::AlreadyBorrowed(error::AlreadyBorrowedError {}))?;
RefMut::filter_map(borrow, |any| any.as_mut()?.downcast_mut())
.map_err(|_| {
crate::error::BorrowMutError::Dropped(crate::error::ValueDroppedError {
error::BorrowMutError::Dropped(error::ValueDroppedError {
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
created_at,
})