add check_generation feature flag

This commit is contained in:
Evan Almloff committed 2023-08-04 15:46:04 -07:00
1 parent 0032f7e2af
commit 07ed3e67dd
2 files changed
+12 -8

No files matched your search

+4
View File
@@ -11,3 +11,7 @@ bumpalo = { version = "3.6" }
[dev-dependencies] [dev-dependencies]
rand = "0.8.5" rand = "0.8.5"
[features]
default = ["check_generation"]
check_generation = []
+8 -8
View File
@@ -128,7 +128,7 @@ fn fuzz() {
println!("{:?}", value); println!("{:?}", value);
assert!(value.starts_with("hello world")); assert!(value.starts_with("hello world"));
} }
#[cfg(debug_assertions)] #[cfg(any(debug_assertions, feature = "check_generation"))]
for key in invalid_keys.iter() { for key in invalid_keys.iter() {
assert!(!key.validate()); assert!(!key.validate());
} }
@@ -146,7 +146,7 @@ fn fuzz() {
pub struct CopyHandle<T> { pub struct CopyHandle<T> {
raw: MemoryLocation, raw: MemoryLocation,
#[cfg(debug_assertions)] #[cfg(any(debug_assertions, feature = "check_generation"))]
generation: u32, generation: u32,
_marker: PhantomData<T>, _marker: PhantomData<T>,
} }
@@ -154,11 +154,11 @@ pub struct CopyHandle<T> {
impl<T: 'static> CopyHandle<T> { impl<T: 'static> CopyHandle<T> {
#[inline(always)] #[inline(always)]
fn validate(&self) -> bool { fn validate(&self) -> bool {
#[cfg(debug_assertions)] #[cfg(any(debug_assertions, feature = "check_generation"))]
{ {
self.raw.generation.get() == self.generation self.raw.generation.get() == self.generation
} }
#[cfg(not(debug_assertions))] #[cfg(not(any(debug_assertions, feature = "check_generation")))]
{ {
true true
} }
@@ -206,7 +206,7 @@ impl<T> Clone for CopyHandle<T> {
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
struct MemoryLocation { struct MemoryLocation {
data: &'static RefCell<Option<Box<dyn std::any::Any>>>, data: &'static RefCell<Option<Box<dyn std::any::Any>>>,
#[cfg(debug_assertions)] #[cfg(any(debug_assertions, feature = "check_generation"))]
generation: &'static Cell<u32>, generation: &'static Cell<u32>,
} }
@@ -214,7 +214,7 @@ impl MemoryLocation {
#[allow(unused)] #[allow(unused)]
fn drop(&self) { fn drop(&self) {
let old = self.data.borrow_mut().take(); let old = self.data.borrow_mut().take();
#[cfg(debug_assertions)] #[cfg(any(debug_assertions, feature = "check_generation"))]
if old.is_some() { if old.is_some() {
let new_generation = self.generation.get() + 1; let new_generation = self.generation.get() + 1;
self.generation.set(new_generation); self.generation.set(new_generation);
@@ -229,7 +229,7 @@ impl MemoryLocation {
assert!(old.is_none()); assert!(old.is_none());
CopyHandle { CopyHandle {
raw: *self, raw: *self,
#[cfg(debug_assertions)] #[cfg(any(debug_assertions, feature = "check_generation"))]
generation: self.generation.get(), generation: self.generation.get(),
_marker: PhantomData, _marker: PhantomData,
} }
@@ -264,7 +264,7 @@ impl Store {
let data: &'static RefCell<_> = self.bump.alloc(RefCell::new(None)); let data: &'static RefCell<_> = self.bump.alloc(RefCell::new(None));
MemoryLocation { MemoryLocation {
data, data,
#[cfg(debug_assertions)] #[cfg(any(debug_assertions, feature = "check_generation"))]
generation: self.bump.alloc(Cell::new(0)), generation: self.bump.alloc(Cell::new(0)),
} }
} }