add check_generation feature flag

This commit is contained in:
Evan Almloff 2023-08-04 15:46:04 -07:00
parent 0032f7e2af
commit 07ed3e67dd
2 changed files with 12 additions and 8 deletions

View File

@ -11,3 +11,7 @@ bumpalo = { version = "3.6" }
[dev-dependencies]
rand = "0.8.5"
[features]
default = ["check_generation"]
check_generation = []

View File

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