diff options
Diffstat (limited to 'engine/src/util.rs')
-rw-r--r-- | engine/src/util.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/engine/src/util.rs b/engine/src/util.rs index 532bdee..8e93982 100644 --- a/engine/src/util.rs +++ b/engine/src/util.rs @@ -9,6 +9,9 @@ macro_rules! try_option { }; } +use std::mem::ManuallyDrop; +use std::ops::{Deref, DerefMut}; + pub(crate) use try_option; macro_rules! or { @@ -93,3 +96,37 @@ macro_rules! builder { } pub(crate) use builder; + +/// Wrapper that ensures the contained value will never be dropped. +#[derive(Debug)] +pub struct NeverDrop<Value> +{ + value: ManuallyDrop<Value>, +} + +impl<Value> NeverDrop<Value> +{ + #[must_use] + pub fn new(value: Value) -> Self + { + Self { value: ManuallyDrop::new(value) } + } +} + +impl<Value> Deref for NeverDrop<Value> +{ + type Target = Value; + + fn deref(&self) -> &Self::Target + { + &*self.value + } +} + +impl<Value> DerefMut for NeverDrop<Value> +{ + fn deref_mut(&mut self) -> &mut Self::Target + { + &mut *self.value + } +} |