remove mappable and mappablemut traits

This commit is contained in:
Evan Almloff committed 2024-01-22 16:51:25 -06:00
1 parent 636aeb4b90
commit e01f10e25d
9 files changed
+216 -367

No files matched your search

+34 -39
View File
@@ -226,7 +226,7 @@ impl<T: 'static, S: Storage<T>> GenerationalBox<T, S> {
/// Try to read the value. Returns None if the value is no longer valid.
#[track_caller]
pub fn try_read(&self) -> Result<S::Ref, BorrowError> {
pub fn try_read(&self) -> Result<S::Ref<T>, BorrowError> {
if !self.validate() {
return Err(BorrowError::Dropped(ValueDroppedError {
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
@@ -258,13 +258,13 @@ impl<T: 'static, S: Storage<T>> GenerationalBox<T, S> {
/// Read the value. Panics if the value is no longer valid.
#[track_caller]
pub fn read(&self) -> S::Ref {
pub fn read(&self) -> S::Ref<T> {
self.try_read().unwrap()
}
/// Try to write the value. Returns None if the value is no longer valid.
#[track_caller]
pub fn try_write(&self) -> Result<S::Mut, BorrowMutError> {
pub fn try_write(&self) -> Result<S::Mut<T>, BorrowMutError> {
if !self.validate() {
return Err(BorrowMutError::Dropped(ValueDroppedError {
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
@@ -292,7 +292,7 @@ impl<T: 'static, S: Storage<T>> GenerationalBox<T, S> {
/// Write the value. Panics if the value is no longer valid.
#[track_caller]
pub fn write(&self) -> S::Mut {
pub fn write(&self) -> S::Mut<T> {
self.try_write().unwrap()
}
@@ -325,42 +325,37 @@ impl<T, S> Clone for GenerationalBox<T, S> {
}
}
/// A trait for types that can be mapped.
pub trait Mappable<T: ?Sized>: Deref<Target = T> {
/// The type after the mapping.
type Mapped<U: ?Sized + 'static>: Mappable<U> + Deref<Target = U>;
/// Map the value.
fn map<U: ?Sized + 'static>(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped<U>;
/// Try to map the value.
fn try_map<U: ?Sized + 'static>(
_self: Self,
f: impl FnOnce(&T) -> Option<&U>,
) -> Option<Self::Mapped<U>>;
}
/// A trait for types that can be mapped mutably.
pub trait MappableMut<T: ?Sized>: DerefMut<Target = T> {
/// The type after the mapping.
type Mapped<U: ?Sized + 'static>: MappableMut<U> + DerefMut<Target = U>;
/// Map the value.
fn map<U: ?Sized + 'static>(_self: Self, f: impl FnOnce(&mut T) -> &mut U) -> Self::Mapped<U>;
/// Try to map the value.
fn try_map<U: ?Sized + 'static>(
_self: Self,
f: impl FnOnce(&mut T) -> Option<&mut U>,
) -> Option<Self::Mapped<U>>;
}
/// A trait for a storage backing type. (RefCell, RwLock, etc.)
pub trait Storage<Data>: AnyStorage + 'static {
pub trait Storage<Data = ()>: AnyStorage + 'static {
/// The reference this storage type returns.
type Ref: Mappable<Data> + Deref<Target = Data>;
type Ref<T: ?Sized + 'static>: Deref<Target = T>;
/// The mutable reference this storage type returns.
type Mut: MappableMut<Data> + DerefMut<Target = Data>;
type Mut<T: ?Sized + 'static>: DerefMut<Target = T>;
/// Try to map the mutable ref.
fn try_map_mut<T, U: ?Sized + 'static>(
mut_ref: Self::Mut<T>,
f: impl FnOnce(&mut T) -> Option<&mut U>,
) -> Option<Self::Mut<U>>;
/// Map the mutable ref.
fn map_mut<T, U: ?Sized + 'static>(
mut_ref: Self::Mut<T>,
f: impl FnOnce(&mut T) -> &mut U,
) -> Self::Mut<U> {
Self::try_map_mut(mut_ref, |v| Some(f(v))).unwrap()
}
/// Try to map the ref.
fn try_map<T, U: ?Sized + 'static>(
ref_: Self::Ref<T>,
f: impl FnOnce(&T) -> Option<&U>,
) -> Option<Self::Ref<U>>;
/// Map the ref.
fn map<T, U: ?Sized + 'static>(ref_: Self::Ref<T>, f: impl FnOnce(&T) -> &U) -> Self::Ref<U> {
Self::try_map(ref_, |v| Some(f(v))).unwrap()
}
/// Try to read the value. Returns None if the value is no longer valid.
fn try_read(
@@ -368,7 +363,7 @@ pub trait Storage<Data>: AnyStorage + 'static {
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
created_at: &'static std::panic::Location<'static>,
#[cfg(any(debug_assertions, feature = "debug_ownership"))] at: GenerationalRefBorrowInfo,
) -> Result<Self::Ref, BorrowError>;
) -> Result<Self::Ref<Data>, BorrowError>;
/// Try to write the value. Returns None if the value is no longer valid.
fn try_write(
@@ -376,7 +371,7 @@ pub trait Storage<Data>: AnyStorage + 'static {
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
created_at: &'static std::panic::Location<'static>,
#[cfg(any(debug_assertions, feature = "debug_ownership"))] at: GenerationalRefMutBorrowInfo,
) -> Result<Self::Mut, BorrowMutError>;
) -> Result<Self::Mut<Data>, BorrowMutError>;
/// Set the value
fn set(&'static self, value: Data);
+13 -88
View File
@@ -1,83 +1,41 @@
use std::{
fmt::{Debug, Display},
marker::PhantomData,
ops::{Deref, DerefMut},
};
use crate::{Mappable, MappableMut};
/// A reference to a value in a generational box.
pub struct GenerationalRef<T: ?Sized + 'static, R: Mappable<T>> {
inner: R,
phantom: PhantomData<T>,
pub struct GenerationalRef<R> {
pub(crate) inner: R,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow: GenerationalRefBorrowInfo,
pub(crate) borrow: GenerationalRefBorrowInfo,
}
impl<T: 'static, R: Mappable<T>> GenerationalRef<T, R> {
impl<T: 'static, R: Deref<Target = T>> GenerationalRef<R> {
pub(crate) fn new(
inner: R,
#[cfg(any(debug_assertions, feature = "debug_borrows"))] borrow: GenerationalRefBorrowInfo,
) -> Self {
Self {
inner,
phantom: PhantomData,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow,
}
}
}
impl<T: ?Sized + 'static, R: Mappable<T>> Mappable<T> for GenerationalRef<T, R> {
type Mapped<U: ?Sized + 'static> = GenerationalRef<U, R::Mapped<U>>;
fn map<U: ?Sized + 'static>(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped<U> {
GenerationalRef {
inner: R::map(_self.inner, f),
phantom: PhantomData,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow: GenerationalRefBorrowInfo {
borrowed_at: _self.borrow.borrowed_at,
borrowed_from: _self.borrow.borrowed_from,
},
}
}
fn try_map<U: ?Sized + 'static>(
_self: Self,
f: impl FnOnce(&T) -> Option<&U>,
) -> Option<Self::Mapped<U>> {
let Self {
inner,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow,
..
} = _self;
R::try_map(inner, f).map(|inner| GenerationalRef {
inner,
phantom: PhantomData,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow: GenerationalRefBorrowInfo {
borrowed_at: borrow.borrowed_at,
borrowed_from: borrow.borrowed_from,
},
})
}
}
impl<T: ?Sized + Debug, R: Mappable<T>> Debug for GenerationalRef<T, R> {
impl<T: ?Sized + Debug, R: Deref<Target = T>> Debug for GenerationalRef<R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.inner.deref().fmt(f)
}
}
impl<T: ?Sized + Display, R: Mappable<T>> Display for GenerationalRef<T, R> {
impl<T: ?Sized + Display, R: Deref<Target = T>> Display for GenerationalRef<R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.inner.deref().fmt(f)
}
}
impl<T: ?Sized + 'static, R: Mappable<T>> Deref for GenerationalRef<T, R> {
impl<T: ?Sized + 'static, R: Deref<Target = T>> Deref for GenerationalRef<R> {
type Target = T;
fn deref(&self) -> &Self::Target {
@@ -103,14 +61,13 @@ impl Drop for GenerationalRefBorrowInfo {
}
/// A mutable reference to a value in a generational box.
pub struct GenerationalRefMut<T: ?Sized + 'static, W: MappableMut<T>> {
inner: W,
phantom: PhantomData<T>,
pub struct GenerationalRefMut<W> {
pub(crate) inner: W,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow: GenerationalRefMutBorrowInfo,
pub(crate) borrow: GenerationalRefMutBorrowInfo,
}
impl<T: 'static, R: MappableMut<T>> GenerationalRefMut<T, R> {
impl<T: 'static, R: DerefMut<Target = T>> GenerationalRefMut<R> {
pub(crate) fn new(
inner: R,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
@@ -118,45 +75,13 @@ impl<T: 'static, R: MappableMut<T>> GenerationalRefMut<T, R> {
) -> Self {
Self {
inner,
phantom: PhantomData,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow,
}
}
}
impl<T: ?Sized + 'static, W: MappableMut<T>> MappableMut<T> for GenerationalRefMut<T, W> {
type Mapped<U: ?Sized + 'static> = GenerationalRefMut<U, W::Mapped<U>>;
fn map<U: ?Sized + 'static>(_self: Self, f: impl FnOnce(&mut T) -> &mut U) -> Self::Mapped<U> {
GenerationalRefMut {
inner: W::map(_self.inner, f),
phantom: PhantomData,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow: _self.borrow,
}
}
fn try_map<U: ?Sized + 'static>(
_self: Self,
f: impl FnOnce(&mut T) -> Option<&mut U>,
) -> Option<Self::Mapped<U>> {
let Self {
inner,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow,
..
} = _self;
W::try_map(inner, f).map(|inner| GenerationalRefMut {
inner,
phantom: PhantomData,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow,
})
}
}
impl<T: ?Sized + 'static, W: MappableMut<T>> Deref for GenerationalRefMut<T, W> {
impl<T: ?Sized + 'static, W: DerefMut<Target = T>> Deref for GenerationalRefMut<W> {
type Target = T;
fn deref(&self) -> &Self::Target {
@@ -164,7 +89,7 @@ impl<T: ?Sized + 'static, W: MappableMut<T>> Deref for GenerationalRefMut<T, W>
}
}
impl<T: ?Sized + 'static, W: MappableMut<T>> DerefMut for GenerationalRefMut<T, W> {
impl<T: ?Sized + 'static, W: DerefMut<Target = T>> DerefMut for GenerationalRefMut<W> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.inner.deref_mut()
}
+49 -35
View File
@@ -6,7 +6,8 @@ use std::sync::{Arc, OnceLock};
use crate::{
error::{self, ValueDroppedError},
references::{GenerationalRef, GenerationalRefMut},
AnyStorage, Mappable, MappableMut, MemoryLocation, MemoryLocationInner, Storage,
AnyStorage, GenerationalRefBorrowInfo, GenerationalRefMutBorrowInfo, MemoryLocation,
MemoryLocationInner, Storage,
};
/// A thread safe storage. This is slower than the unsync storage, but allows you to share the value between threads.
@@ -48,39 +49,9 @@ impl AnyStorage for SyncStorage {
}
}
impl<T: ?Sized> Mappable<T> for MappedRwLockReadGuard<'static, T> {
type Mapped<U: ?Sized + 'static> = MappedRwLockReadGuard<'static, U>;
fn map<U: ?Sized + 'static>(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped<U> {
MappedRwLockReadGuard::map(_self, f)
}
fn try_map<U: ?Sized + 'static>(
_self: Self,
f: impl FnOnce(&T) -> Option<&U>,
) -> Option<Self::Mapped<U>> {
MappedRwLockReadGuard::try_map(_self, f).ok()
}
}
impl<T: ?Sized> MappableMut<T> for MappedRwLockWriteGuard<'static, T> {
type Mapped<U: ?Sized + 'static> = MappedRwLockWriteGuard<'static, U>;
fn map<U: ?Sized + 'static>(_self: Self, f: impl FnOnce(&mut T) -> &mut U) -> Self::Mapped<U> {
MappedRwLockWriteGuard::map(_self, f)
}
fn try_map<U: ?Sized + 'static>(
_self: Self,
f: impl FnOnce(&mut T) -> Option<&mut U>,
) -> Option<Self::Mapped<U>> {
MappedRwLockWriteGuard::try_map(_self, f).ok()
}
}
impl<T: Sync + Send + 'static> Storage<T> for SyncStorage {
type Ref = GenerationalRef<T, MappedRwLockReadGuard<'static, T>>;
type Mut = GenerationalRefMut<T, MappedRwLockWriteGuard<'static, T>>;
type Ref<R: ?Sized + 'static> = GenerationalRef<MappedRwLockReadGuard<'static, R>>;
type Mut<W: ?Sized + 'static> = GenerationalRefMut<MappedRwLockWriteGuard<'static, W>>;
fn try_read(
&'static self,
@@ -88,7 +59,7 @@ impl<T: Sync + Send + 'static> Storage<T> for SyncStorage {
created_at: &'static std::panic::Location<'static>,
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
at: crate::GenerationalRefBorrowInfo,
) -> Result<Self::Ref, error::BorrowError> {
) -> Result<Self::Ref<T>, error::BorrowError> {
let read = self.0.try_read();
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
@@ -121,7 +92,7 @@ impl<T: Sync + Send + 'static> Storage<T> for SyncStorage {
created_at: &'static std::panic::Location<'static>,
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
at: crate::GenerationalRefMutBorrowInfo,
) -> Result<Self::Mut, error::BorrowMutError> {
) -> Result<Self::Mut<T>, error::BorrowMutError> {
let write = self.0.try_write();
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
@@ -151,4 +122,47 @@ impl<T: Sync + Send + 'static> Storage<T> for SyncStorage {
fn set(&self, value: T) {
*self.0.write() = Some(Box::new(value));
}
fn try_map<I, U: ?Sized + 'static>(
ref_: Self::Ref<I>,
f: impl FnOnce(&I) -> Option<&U>,
) -> Option<Self::Ref<U>> {
let GenerationalRef {
inner,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow,
..
} = ref_;
MappedRwLockReadGuard::try_map(inner, f)
.ok()
.map(|inner| GenerationalRef {
inner,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow: GenerationalRefBorrowInfo {
borrowed_at: borrow.borrowed_at,
borrowed_from: borrow.borrowed_from,
},
})
}
fn try_map_mut<I, U: ?Sized + 'static>(
mut_ref: Self::Mut<I>,
f: impl FnOnce(&mut I) -> Option<&mut U>,
) -> Option<Self::Mut<U>> {
let GenerationalRefMut {
inner,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow,
..
} = mut_ref;
MappedRwLockWriteGuard::try_map(inner, f)
.ok()
.map(|inner| GenerationalRefMut {
inner,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow: GenerationalRefMutBorrowInfo {
borrowed_from: borrow.borrowed_from,
},
})
}
}
+43 -35
View File
@@ -1,7 +1,7 @@
use crate::{
error,
references::{GenerationalRef, GenerationalRefMut},
AnyStorage, Mappable, MappableMut, MemoryLocation, MemoryLocationInner, Storage,
AnyStorage, GenerationalRefMutBorrowInfo, MemoryLocation, MemoryLocationInner, Storage,
};
use std::cell::{Ref, RefCell, RefMut};
@@ -14,39 +14,9 @@ impl Default for UnsyncStorage {
}
}
impl<T: ?Sized> Mappable<T> for Ref<'static, T> {
type Mapped<U: ?Sized + 'static> = Ref<'static, U>;
fn map<U: ?Sized + 'static>(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped<U> {
Ref::map(_self, f)
}
fn try_map<U: ?Sized + 'static>(
_self: Self,
f: impl FnOnce(&T) -> Option<&U>,
) -> Option<Self::Mapped<U>> {
Ref::filter_map(_self, f).ok()
}
}
impl<T: ?Sized> MappableMut<T> for RefMut<'static, T> {
type Mapped<U: ?Sized + 'static> = RefMut<'static, U>;
fn map<U: ?Sized + 'static>(_self: Self, f: impl FnOnce(&mut T) -> &mut U) -> Self::Mapped<U> {
RefMut::map(_self, f)
}
fn try_map<U: ?Sized + 'static>(
_self: Self,
f: impl FnOnce(&mut T) -> Option<&mut U>,
) -> Option<Self::Mapped<U>> {
RefMut::filter_map(_self, f).ok()
}
}
impl<T: 'static> Storage<T> for UnsyncStorage {
type Ref = GenerationalRef<T, Ref<'static, T>>;
type Mut = GenerationalRefMut<T, RefMut<'static, T>>;
type Ref<R: ?Sized + 'static> = GenerationalRef<Ref<'static, R>>;
type Mut<W: ?Sized + 'static> = GenerationalRefMut<RefMut<'static, W>>;
fn try_read(
&'static self,
@@ -54,7 +24,7 @@ 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, error::BorrowError> {
) -> Result<Self::Ref<T>, error::BorrowError> {
let borrow = self.0.try_borrow();
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
@@ -87,7 +57,7 @@ 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, error::BorrowMutError> {
) -> Result<Self::Mut<T>, error::BorrowMutError> {
let borrow = self.0.try_borrow_mut();
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
@@ -116,6 +86,44 @@ impl<T: 'static> Storage<T> for UnsyncStorage {
fn set(&self, value: T) {
*self.0.borrow_mut() = Some(Box::new(value));
}
fn try_map<I, U: ?Sized + 'static>(
_self: Self::Ref<I>,
f: impl FnOnce(&I) -> Option<&U>,
) -> Option<Self::Ref<U>> {
let GenerationalRef {
inner,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow,
..
} = _self;
Ref::filter_map(inner, f).ok().map(|inner| GenerationalRef {
inner,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow,
})
}
fn try_map_mut<I, U: ?Sized + 'static>(
mut_ref: Self::Mut<I>,
f: impl FnOnce(&mut I) -> Option<&mut U>,
) -> Option<Self::Mut<U>> {
let GenerationalRefMut {
inner,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow,
..
} = mut_ref;
RefMut::filter_map(inner, f)
.ok()
.map(|inner| GenerationalRefMut {
inner,
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrow: GenerationalRefMutBorrowInfo {
borrowed_from: borrow.borrowed_from,
},
})
}
}
thread_local! {