|
@@ -5,21 +5,19 @@ use parking_lot::{
|
|
|
MappedRwLockReadGuard, MappedRwLockWriteGuard, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard,
|
|
|
};
|
|
|
use std::{
|
|
|
- cell::RefCell,
|
|
|
+ cell::{Ref, RefCell, RefMut},
|
|
|
fmt::Debug,
|
|
|
marker::PhantomData,
|
|
|
+ ops::{Deref, DerefMut},
|
|
|
rc::Rc,
|
|
|
- sync::{atomic::AtomicU32, Arc},
|
|
|
+ sync::{atomic::AtomicU32, Arc, OnceLock},
|
|
|
};
|
|
|
|
|
|
-mod testing;
|
|
|
-
|
|
|
/// # Example
|
|
|
///
|
|
|
/// ```compile_fail
|
|
|
/// let data = String::from("hello world");
|
|
|
-/// let store = Store::default();
|
|
|
-/// let owner = store.owner();
|
|
|
+/// let owner = UnsyncStorage::owner();
|
|
|
/// let key = owner.insert(&data);
|
|
|
/// drop(data);
|
|
|
/// assert_eq!(*key.read(), "hello world");
|
|
@@ -29,15 +27,14 @@ fn compile_fail() {}
|
|
|
|
|
|
#[test]
|
|
|
fn reused() {
|
|
|
- let store = Store::default();
|
|
|
let first_ptr;
|
|
|
{
|
|
|
- let owner = store.owner();
|
|
|
+ let owner = UnsyncStorage::owner();
|
|
|
first_ptr = owner.insert(1).raw.data.data_ptr();
|
|
|
drop(owner);
|
|
|
}
|
|
|
{
|
|
|
- let owner = store.owner();
|
|
|
+ let owner = UnsyncStorage::owner();
|
|
|
let second_ptr = owner.insert(1234).raw.data.data_ptr();
|
|
|
assert_eq!(first_ptr, second_ptr);
|
|
|
drop(owner);
|
|
@@ -47,11 +44,10 @@ fn reused() {
|
|
|
#[test]
|
|
|
fn leaking_is_ok() {
|
|
|
let data = String::from("hello world");
|
|
|
- let store = Store::default();
|
|
|
let key;
|
|
|
{
|
|
|
// create an owner
|
|
|
- let owner = store.owner();
|
|
|
+ let owner = UnsyncStorage::owner();
|
|
|
// insert data into the store
|
|
|
key = owner.insert(data);
|
|
|
// don't drop the owner
|
|
@@ -63,11 +59,10 @@ fn leaking_is_ok() {
|
|
|
#[test]
|
|
|
fn drops() {
|
|
|
let data = String::from("hello world");
|
|
|
- let store = Store::default();
|
|
|
let key;
|
|
|
{
|
|
|
// create an owner
|
|
|
- let owner = store.owner();
|
|
|
+ let owner = UnsyncStorage::owner();
|
|
|
// insert data into the store
|
|
|
key = owner.insert(data);
|
|
|
// drop the owner
|
|
@@ -77,8 +72,7 @@ fn drops() {
|
|
|
|
|
|
#[test]
|
|
|
fn works() {
|
|
|
- let store = Store::default();
|
|
|
- let owner = store.owner();
|
|
|
+ let owner = UnsyncStorage::owner();
|
|
|
let key = owner.insert(1);
|
|
|
|
|
|
assert_eq!(*key.read(), 1);
|
|
@@ -86,8 +80,7 @@ fn works() {
|
|
|
|
|
|
#[test]
|
|
|
fn insert_while_reading() {
|
|
|
- let store = Store::default();
|
|
|
- let owner = store.owner();
|
|
|
+ let owner = UnsyncStorage::owner();
|
|
|
let key;
|
|
|
{
|
|
|
let data: String = "hello world".to_string();
|
|
@@ -101,8 +94,7 @@ fn insert_while_reading() {
|
|
|
#[test]
|
|
|
#[should_panic]
|
|
|
fn panics() {
|
|
|
- let store = Store::default();
|
|
|
- let owner = store.owner();
|
|
|
+ let owner = UnsyncStorage::owner();
|
|
|
let key = owner.insert(1);
|
|
|
drop(owner);
|
|
|
|
|
@@ -112,7 +104,6 @@ fn panics() {
|
|
|
#[test]
|
|
|
fn fuzz() {
|
|
|
fn maybe_owner_scope(
|
|
|
- store: &Store,
|
|
|
valid_keys: &mut Vec<GenerationalBox<String>>,
|
|
|
invalid_keys: &mut Vec<GenerationalBox<String>>,
|
|
|
path: &mut Vec<u8>,
|
|
@@ -125,7 +116,7 @@ fn fuzz() {
|
|
|
};
|
|
|
|
|
|
for i in 0..children {
|
|
|
- let owner = store.owner();
|
|
|
+ let owner = UnsyncStorage::owner();
|
|
|
let key = owner.insert(format!("hello world {path:?}"));
|
|
|
valid_keys.push(key);
|
|
|
path.push(i);
|
|
@@ -140,27 +131,26 @@ fn fuzz() {
|
|
|
for key in invalid_keys.iter() {
|
|
|
assert!(!key.validate());
|
|
|
}
|
|
|
- maybe_owner_scope(store, valid_keys, invalid_keys, path);
|
|
|
+ maybe_owner_scope(valid_keys, invalid_keys, path);
|
|
|
invalid_keys.push(valid_keys.pop().unwrap());
|
|
|
path.pop();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for _ in 0..10 {
|
|
|
- let store = Store::default();
|
|
|
- maybe_owner_scope(&store, &mut Vec::new(), &mut Vec::new(), &mut Vec::new());
|
|
|
+ maybe_owner_scope(&mut Vec::new(), &mut Vec::new(), &mut Vec::new());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// The core Copy state type. The generational box will be dropped when the [Owner] is dropped.
|
|
|
-pub struct GenerationalBox<T> {
|
|
|
- raw: MemoryLocation,
|
|
|
+pub struct GenerationalBox<T, S = UnsyncStorage> {
|
|
|
+ raw: MemoryLocation<S>,
|
|
|
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
|
|
generation: u32,
|
|
|
_marker: PhantomData<T>,
|
|
|
}
|
|
|
|
|
|
-impl<T: 'static> Debug for GenerationalBox<T> {
|
|
|
+impl<T: 'static, S: AnyStorage> Debug for GenerationalBox<T, S> {
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
|
|
f.write_fmt(format_args!(
|
|
@@ -174,7 +164,7 @@ impl<T: 'static> Debug for GenerationalBox<T> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl<T: 'static> GenerationalBox<T> {
|
|
|
+impl<T: 'static, S: Storage<T>> GenerationalBox<T, S> {
|
|
|
#[inline(always)]
|
|
|
fn validate(&self) -> bool {
|
|
|
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
|
@@ -191,43 +181,29 @@ impl<T: 'static> GenerationalBox<T> {
|
|
|
}
|
|
|
|
|
|
/// Try to read the value. Returns None if the value is no longer valid.
|
|
|
- pub fn try_read(&self) -> Option<MappedRwLockReadGuard<'static, T>> {
|
|
|
- self.validate()
|
|
|
- .then(|| {
|
|
|
- RwLockReadGuard::try_map(self.raw.data.read(), |any| {
|
|
|
- any.as_ref()?.downcast_ref::<T>()
|
|
|
- })
|
|
|
- .ok()
|
|
|
- })
|
|
|
- .flatten()
|
|
|
+ pub fn try_read(&self) -> Option<S::Ref> {
|
|
|
+ self.validate().then(|| self.raw.data.try_read()).flatten()
|
|
|
}
|
|
|
|
|
|
/// Read the value. Panics if the value is no longer valid.
|
|
|
- pub fn read(&self) -> MappedRwLockReadGuard<'static, T> {
|
|
|
+ pub fn read(&self) -> S::Ref {
|
|
|
self.try_read().unwrap()
|
|
|
}
|
|
|
|
|
|
/// Try to write the value. Returns None if the value is no longer valid.
|
|
|
- pub fn try_write(&self) -> Option<MappedRwLockWriteGuard<'static, T>> {
|
|
|
- self.validate()
|
|
|
- .then(|| {
|
|
|
- RwLockWriteGuard::try_map(self.raw.data.write(), |any| {
|
|
|
- any.as_mut()?.downcast_mut::<T>()
|
|
|
- })
|
|
|
- .ok()
|
|
|
- })
|
|
|
- .flatten()
|
|
|
+ pub fn try_write(&self) -> Option<S::Mut> where {
|
|
|
+ self.validate().then(|| self.raw.data.try_write()).flatten()
|
|
|
}
|
|
|
|
|
|
/// Write the value. Panics if the value is no longer valid.
|
|
|
- pub fn write(&self) -> MappedRwLockWriteGuard<'static, T> {
|
|
|
+ pub fn write(&self) -> S::Mut {
|
|
|
self.try_write().unwrap()
|
|
|
}
|
|
|
|
|
|
/// Set the value. Panics if the value is no longer valid.
|
|
|
pub fn set(&self, value: T) {
|
|
|
self.validate().then(|| {
|
|
|
- *self.raw.data.write() = Some(Box::new(value));
|
|
|
+ self.raw.data.set(value);
|
|
|
});
|
|
|
}
|
|
|
|
|
@@ -245,109 +221,282 @@ impl<T: 'static> GenerationalBox<T> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl<T> Copy for GenerationalBox<T> {}
|
|
|
+impl<T, S: Copy> Copy for GenerationalBox<T, S> {}
|
|
|
|
|
|
-impl<T> Clone for GenerationalBox<T> {
|
|
|
+impl<T, S: Copy> Clone for GenerationalBox<T, S> {
|
|
|
fn clone(&self) -> Self {
|
|
|
*self
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
-struct MemoryLocation {
|
|
|
- data: &'static RwLock<Option<Box<dyn std::any::Any>>>,
|
|
|
- #[cfg(any(debug_assertions, feature = "check_generation"))]
|
|
|
- generation: &'static AtomicU32,
|
|
|
+pub struct UnsyncStorage(&'static RefCell<Option<Box<dyn std::any::Any>>>);
|
|
|
+
|
|
|
+impl Default for UnsyncStorage {
|
|
|
+ fn default() -> Self {
|
|
|
+ Self(Box::leak(Box::new(RefCell::new(None))))
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-impl MemoryLocation {
|
|
|
- #[allow(unused)]
|
|
|
- fn drop(&self) {
|
|
|
- let old = self.data.write().take();
|
|
|
- #[cfg(any(debug_assertions, feature = "check_generation"))]
|
|
|
- if old.is_some() {
|
|
|
- drop(old);
|
|
|
- let new_generation = self.generation.load(std::sync::atomic::Ordering::Relaxed) + 1;
|
|
|
- self.generation
|
|
|
- .store(new_generation, std::sync::atomic::Ordering::Relaxed);
|
|
|
- }
|
|
|
+#[derive(Clone, Copy)]
|
|
|
+pub struct SyncStorage(&'static RwLock<Option<Box<dyn std::any::Any + Send + Sync>>>);
|
|
|
+
|
|
|
+impl Default for SyncStorage {
|
|
|
+ fn default() -> Self {
|
|
|
+ Self(Box::leak(Box::new(RwLock::new(None))))
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- fn replace<T: 'static>(&mut self, value: T) -> GenerationalBox<T> {
|
|
|
- let mut inner_mut = self.data.write();
|
|
|
+pub trait Mappable<T, U>: Deref<Target = T> {
|
|
|
+ type Mapped: Deref<Target = U>;
|
|
|
|
|
|
- let raw = Box::new(value);
|
|
|
- let old = inner_mut.replace(raw);
|
|
|
- assert!(old.is_none());
|
|
|
- GenerationalBox {
|
|
|
- raw: *self,
|
|
|
- #[cfg(any(debug_assertions, feature = "check_generation"))]
|
|
|
- generation: self.generation.load(std::sync::atomic::Ordering::Relaxed),
|
|
|
- _marker: PhantomData,
|
|
|
- }
|
|
|
+ fn map(_self: Self, f: fn(&T) -> &U) -> Self::Mapped;
|
|
|
+}
|
|
|
+
|
|
|
+impl<T, U: 'static> Mappable<T, U> for Ref<'static, T> {
|
|
|
+ type Mapped = Ref<'static, U>;
|
|
|
+
|
|
|
+ fn map(_self: Self, f: fn(&T) -> &U) -> Self::Mapped {
|
|
|
+ Ref::map(_self, f)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-/// Handles recycling generational boxes that have been dropped. Your application should have one store or one store per thread.
|
|
|
-#[derive(Clone)]
|
|
|
-pub struct Store {
|
|
|
- recycled: Arc<Mutex<Vec<MemoryLocation>>>,
|
|
|
+impl<T, U: 'static> Mappable<T, U> for MappedRwLockReadGuard<'static, T> {
|
|
|
+ type Mapped = MappedRwLockReadGuard<'static, U>;
|
|
|
+
|
|
|
+ fn map(_self: Self, f: fn(&T) -> &U) -> Self::Mapped {
|
|
|
+ MappedRwLockReadGuard::map(_self, f)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-impl Default for Store {
|
|
|
- fn default() -> Self {
|
|
|
- Self {
|
|
|
- recycled: Default::default(),
|
|
|
- }
|
|
|
+pub trait MappableMut<T, U>: DerefMut<Target = T> {
|
|
|
+ type Mapped: DerefMut<Target = U>;
|
|
|
+
|
|
|
+ fn map(_self: Self, f: fn(&mut T) -> &mut U) -> Self::Mapped;
|
|
|
+}
|
|
|
+
|
|
|
+impl<T, U: 'static> MappableMut<T, U> for RefMut<'static, T> {
|
|
|
+ type Mapped = RefMut<'static, U>;
|
|
|
+
|
|
|
+ fn map(_self: Self, f: fn(&mut T) -> &mut U) -> Self::Mapped {
|
|
|
+ RefMut::map(_self, f)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl Store {
|
|
|
- fn recycle(&self, location: MemoryLocation) {
|
|
|
- location.drop();
|
|
|
- self.recycled.lock().push(location);
|
|
|
+impl<T, U: 'static> MappableMut<T, U> for MappedRwLockWriteGuard<'static, T> {
|
|
|
+ type Mapped = MappedRwLockWriteGuard<'static, U>;
|
|
|
+
|
|
|
+ fn map(_self: Self, f: fn(&mut T) -> &mut U) -> Self::Mapped {
|
|
|
+ MappedRwLockWriteGuard::map(_self, f)
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- fn claim(&self) -> MemoryLocation {
|
|
|
- if let Some(location) = self.recycled.lock().pop() {
|
|
|
- location
|
|
|
- } else {
|
|
|
- let data: &'static RwLock<_> = Box::leak(Box::new(RwLock::new(None)));
|
|
|
- MemoryLocation {
|
|
|
- data,
|
|
|
- #[cfg(any(debug_assertions, feature = "check_generation"))]
|
|
|
- generation: Box::leak(Box::new(Default::default())),
|
|
|
- }
|
|
|
- }
|
|
|
+pub trait Storage<Data>: Copy + AnyStorage {
|
|
|
+ type Ref: Deref<Target = Data>;
|
|
|
+ type Mut: DerefMut<Target = Data>;
|
|
|
+
|
|
|
+ fn try_read(&self) -> Option<Self::Ref>;
|
|
|
+ fn read(&self) -> Self::Ref {
|
|
|
+ self.try_read()
|
|
|
+ .expect("generational box has been invalidated or the type has changed")
|
|
|
}
|
|
|
+ fn try_write(&self) -> Option<Self::Mut>;
|
|
|
+ fn write(&self) -> Self::Mut {
|
|
|
+ self.try_write()
|
|
|
+ .expect("generational box has been invalidated or the type has changed")
|
|
|
+ }
|
|
|
+
|
|
|
+ fn set(&self, value: Data);
|
|
|
+}
|
|
|
+
|
|
|
+pub trait AnyStorage: Default {
|
|
|
+ fn data_ptr(&self) -> *const ();
|
|
|
+
|
|
|
+ fn take(&self) -> bool;
|
|
|
+
|
|
|
+ fn recycle(location: &MemoryLocation<Self>);
|
|
|
+ // {
|
|
|
+ // location.drop();
|
|
|
+ // self.recycled.lock().push(location);
|
|
|
+ // }
|
|
|
+
|
|
|
+ fn claim() -> MemoryLocation<Self>;
|
|
|
+ // where
|
|
|
+ // S: Default,
|
|
|
+ // {
|
|
|
+ // if let Some(location) = self.recycled.lock().pop() {
|
|
|
+ // location
|
|
|
+ // } else {
|
|
|
+ // MemoryLocation {
|
|
|
+ // data: Default::default(),
|
|
|
+ // #[cfg(any(debug_assertions, feature = "check_generation"))]
|
|
|
+ // generation: Box::leak(Box::new(Default::default())),
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
|
|
|
/// Create a new owner. The owner will be responsible for dropping all of the generational boxes that it creates.
|
|
|
- pub fn owner(&self) -> Owner {
|
|
|
+ fn owner() -> Owner<Self> {
|
|
|
Owner {
|
|
|
- store: self.clone(),
|
|
|
owned: Default::default(),
|
|
|
+ phantom: PhantomData,
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl<T: 'static> Storage<T> for UnsyncStorage {
|
|
|
+ type Ref = Ref<'static, T>;
|
|
|
+ type Mut = RefMut<'static, T>;
|
|
|
+
|
|
|
+ fn try_read(&self) -> Option<Self::Ref> {
|
|
|
+ Ref::filter_map(self.0.borrow(), |any| any.as_ref()?.downcast_ref()).ok()
|
|
|
+ }
|
|
|
+
|
|
|
+ fn try_write(&self) -> Option<Self::Mut> {
|
|
|
+ RefMut::filter_map(self.0.borrow_mut(), |any| any.as_mut()?.downcast_mut()).ok()
|
|
|
+ }
|
|
|
+
|
|
|
+ fn set(&self, value: T) {
|
|
|
+ *self.0.borrow_mut() = Some(Box::new(value));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+thread_local! {
|
|
|
+ static UNSYNC_RUNTIME: RefCell<Vec<MemoryLocation<UnsyncStorage>>> = RefCell::new(Vec::new());
|
|
|
+}
|
|
|
+
|
|
|
+impl AnyStorage for UnsyncStorage {
|
|
|
+ fn data_ptr(&self) -> *const () {
|
|
|
+ self.0.as_ptr() as *const ()
|
|
|
+ }
|
|
|
+
|
|
|
+ fn take(&self) -> bool {
|
|
|
+ self.0.borrow_mut().take().is_some()
|
|
|
+ }
|
|
|
+
|
|
|
+ fn claim() -> MemoryLocation<Self> {
|
|
|
+ UNSYNC_RUNTIME.with(|runtime| {
|
|
|
+ if let Some(location) = runtime.borrow_mut().pop() {
|
|
|
+ location
|
|
|
+ } else {
|
|
|
+ MemoryLocation {
|
|
|
+ data: UnsyncStorage(Box::leak(Box::new(RefCell::new(None)))),
|
|
|
+ #[cfg(any(debug_assertions, feature = "check_generation"))]
|
|
|
+ generation: Box::leak(Box::new(Default::default())),
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ fn recycle(location: &MemoryLocation<Self>) {
|
|
|
+ location.drop();
|
|
|
+ UNSYNC_RUNTIME.with(|runtime| runtime.borrow_mut().push(*location));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl<T: Sync + Send + 'static> Storage<T> for SyncStorage {
|
|
|
+ type Ref = MappedRwLockReadGuard<'static, T>;
|
|
|
+ type Mut = MappedRwLockWriteGuard<'static, T>;
|
|
|
+
|
|
|
+ fn try_read(&self) -> Option<Self::Ref> {
|
|
|
+ RwLockReadGuard::try_map(self.0.read(), |any| any.as_ref()?.downcast_ref()).ok()
|
|
|
+ }
|
|
|
+
|
|
|
+ fn try_write(&self) -> Option<Self::Mut> {
|
|
|
+ RwLockWriteGuard::try_map(self.0.write(), |any| any.as_mut()?.downcast_mut()).ok()
|
|
|
+ }
|
|
|
+
|
|
|
+ fn set(&self, value: T) {
|
|
|
+ *self.0.write() = Some(Box::new(value));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static SYNC_RUNTIME: OnceLock<Arc<Mutex<Vec<MemoryLocation<SyncStorage>>>>> = OnceLock::new();
|
|
|
+
|
|
|
+fn sync_runtime() -> &'static Arc<Mutex<Vec<MemoryLocation<SyncStorage>>>> {
|
|
|
+ SYNC_RUNTIME.get_or_init(|| Arc::new(Mutex::new(Vec::new())))
|
|
|
+}
|
|
|
+
|
|
|
+impl AnyStorage for SyncStorage {
|
|
|
+ fn data_ptr(&self) -> *const () {
|
|
|
+ self.0.data_ptr() as *const ()
|
|
|
+ }
|
|
|
+
|
|
|
+ fn take(&self) -> bool {
|
|
|
+ self.0.write().take().is_some()
|
|
|
+ }
|
|
|
+
|
|
|
+ fn claim() -> MemoryLocation<Self> {
|
|
|
+ MemoryLocation {
|
|
|
+ data: SyncStorage(Box::leak(Box::new(RwLock::new(None)))),
|
|
|
+ #[cfg(any(debug_assertions, feature = "check_generation"))]
|
|
|
+ generation: Box::leak(Box::new(Default::default())),
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fn recycle(location: &MemoryLocation<Self>) {
|
|
|
+ location.drop();
|
|
|
+ sync_runtime().lock().push(*location);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#[derive(Clone, Copy)]
|
|
|
+struct MemoryLocation<S = UnsyncStorage> {
|
|
|
+ data: S,
|
|
|
+ #[cfg(any(debug_assertions, feature = "check_generation"))]
|
|
|
+ generation: &'static AtomicU32,
|
|
|
+}
|
|
|
+
|
|
|
+impl<S> MemoryLocation<S> {
|
|
|
+ #[allow(unused)]
|
|
|
+ fn drop(&self)
|
|
|
+ where
|
|
|
+ S: AnyStorage,
|
|
|
+ {
|
|
|
+ let old = self.data.take();
|
|
|
+ #[cfg(any(debug_assertions, feature = "check_generation"))]
|
|
|
+ if old {
|
|
|
+ let new_generation = self.generation.load(std::sync::atomic::Ordering::Relaxed) + 1;
|
|
|
+ self.generation
|
|
|
+ .store(new_generation, std::sync::atomic::Ordering::Relaxed);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fn replace<T: 'static>(&mut self, value: T) -> GenerationalBox<T, S>
|
|
|
+ where
|
|
|
+ S: Storage<T> + Copy,
|
|
|
+ {
|
|
|
+ self.data.set(value);
|
|
|
+ GenerationalBox {
|
|
|
+ raw: *self,
|
|
|
+ #[cfg(any(debug_assertions, feature = "check_generation"))]
|
|
|
+ generation: self.generation.load(std::sync::atomic::Ordering::Relaxed),
|
|
|
+ _marker: PhantomData,
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// Owner: Handles dropping generational boxes. The owner acts like a runtime lifetime guard. Any states that you create with an owner will be dropped when that owner is dropped.
|
|
|
-pub struct Owner {
|
|
|
- store: Store,
|
|
|
- owned: Rc<RefCell<Vec<MemoryLocation>>>,
|
|
|
+pub struct Owner<S: AnyStorage = UnsyncStorage> {
|
|
|
+ owned: Arc<Mutex<Vec<MemoryLocation<S>>>>,
|
|
|
+ phantom: PhantomData<S>,
|
|
|
}
|
|
|
|
|
|
-impl Owner {
|
|
|
+impl<S: AnyStorage + Copy> Owner<S> {
|
|
|
/// Insert a value into the store. The value will be dropped when the owner is dropped.
|
|
|
- pub fn insert<T: 'static>(&self, value: T) -> GenerationalBox<T> {
|
|
|
- let mut location = self.store.claim();
|
|
|
+ pub fn insert<T: 'static>(&self, value: T) -> GenerationalBox<T, S>
|
|
|
+ where
|
|
|
+ S: Storage<T>,
|
|
|
+ {
|
|
|
+ let mut location = S::claim();
|
|
|
let key = location.replace(value);
|
|
|
- self.owned.borrow_mut().push(location);
|
|
|
+ self.owned.lock().push(location);
|
|
|
key
|
|
|
}
|
|
|
|
|
|
/// Creates an invalid handle. This is useful for creating a handle that will be filled in later. If you use this before the value is filled in, you will get may get a panic or an out of date value.
|
|
|
- pub fn invalid<T: 'static>(&self) -> GenerationalBox<T> {
|
|
|
- let location = self.store.claim();
|
|
|
+ pub fn invalid<T: 'static>(&self) -> GenerationalBox<T, S> {
|
|
|
+ let location = S::claim();
|
|
|
GenerationalBox {
|
|
|
raw: location,
|
|
|
#[cfg(any(debug_assertions, feature = "check_generation"))]
|
|
@@ -359,10 +508,10 @@ impl Owner {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl Drop for Owner {
|
|
|
+impl<S: AnyStorage> Drop for Owner<S> {
|
|
|
fn drop(&mut self) {
|
|
|
- for location in self.owned.borrow().iter() {
|
|
|
- self.store.recycle(*location)
|
|
|
+ for location in self.owned.lock().iter() {
|
|
|
+ S::recycle(location)
|
|
|
}
|
|
|
}
|
|
|
}
|