From a453f210b35b6200bb7603b1a378db264892c478 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 3 Apr 2022 13:25:41 +0200 Subject: [PATCH] get drop working --- src/lib.rs | 11 ++++++++++- src/strategies.rs | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index aa45424..377db9d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -130,7 +130,7 @@ fn map_ptr(ptr: *mut T, map: impl FnOnce(usize) -> usize) -> *mut T { #[cfg(test)] mod tests { - use crate::strategies::test_strategies::HasDebug; + use crate::strategies::test_strategies::{HasDebug, PanicsInDrop}; use crate::StuffedPtr; #[test] @@ -160,4 +160,13 @@ mod tests { "StuffedPtr::Extra { extra: hello! }" ); } + + #[test] + #[should_panic] + fn needs_drop() { + let extra = PanicsInDrop; + let stuffed_ptr: StuffedPtr<(), PanicsInDrop> = StuffedPtr::new_extra(extra); + // the panicking drop needs to be called here! + drop(stuffed_ptr); + } } diff --git a/src/strategies.rs b/src/strategies.rs index 6d5142c..1201dca 100644 --- a/src/strategies.rs +++ b/src/strategies.rs @@ -21,6 +21,27 @@ pub mod test_strategies { use crate::StuffingStrategy; use std::fmt::{Debug, Formatter}; + macro_rules! impl_usize_max_zst { + ($ty:ident) => { + // this one lives in usize::MAX + unsafe impl StuffingStrategy for $ty { + type Extra = Self; + + fn is_extra(data: usize) -> bool { + data == usize::MAX + } + + fn stuff_extra(_inner: Self::Extra) -> usize { + usize::MAX + } + + fn extract_extra(_data: usize) -> Self::Extra { + $ty + } + } + }; + } + pub struct HasDebug; impl Debug for HasDebug { @@ -29,19 +50,15 @@ pub mod test_strategies { } } - unsafe impl StuffingStrategy for HasDebug { - type Extra = Self; + impl_usize_max_zst!(HasDebug); - fn is_extra(data: usize) -> bool { - data == usize::MAX - } + pub struct PanicsInDrop; - fn stuff_extra(_inner: Self::Extra) -> usize { - usize::MAX - } - - fn extract_extra(_data: usize) -> Self::Extra { - Self + impl Drop for PanicsInDrop { + fn drop(&mut self) { + panic!("oh no!!!"); } } + + impl_usize_max_zst!(PanicsInDrop); }