From 07ed3e67dd89cb1e7fb99f985e56197db843b4db Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Fri, 4 Aug 2023 15:46:04 -0700 Subject: [PATCH] add check_generation feature flag --- packages/copy/Cargo.toml | 4 ++++ packages/copy/src/lib.rs | 16 ++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/copy/Cargo.toml b/packages/copy/Cargo.toml index 6e623f67f..9c5e84065 100644 --- a/packages/copy/Cargo.toml +++ b/packages/copy/Cargo.toml @@ -11,3 +11,7 @@ bumpalo = { version = "3.6" } [dev-dependencies] rand = "0.8.5" + +[features] +default = ["check_generation"] +check_generation = [] \ No newline at end of file diff --git a/packages/copy/src/lib.rs b/packages/copy/src/lib.rs index a10540429..8e8b8053f 100644 --- a/packages/copy/src/lib.rs +++ b/packages/copy/src/lib.rs @@ -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 { raw: MemoryLocation, - #[cfg(debug_assertions)] + #[cfg(any(debug_assertions, feature = "check_generation"))] generation: u32, _marker: PhantomData, } @@ -154,11 +154,11 @@ pub struct CopyHandle { impl CopyHandle { #[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 Clone for CopyHandle { #[derive(Clone, Copy)] struct MemoryLocation { data: &'static RefCell>>, - #[cfg(debug_assertions)] + #[cfg(any(debug_assertions, feature = "check_generation"))] generation: &'static Cell, } @@ -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)), } }