summaryrefslogtreecommitdiff
path: root/engine/src/util.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-08-16 22:10:00 +0200
committerHampusM <hampus@hampusmat.com>2024-08-16 22:10:00 +0200
commitc4686c2992417545e7a05a6a40ee9f1a8bbf3b96 (patch)
tree552951049d580283ab1beb5b512db44994b893ee /engine/src/util.rs
parent7d218b2525f90dfedcae02f3b3d0d2f7b9c99bd2 (diff)
perf(engine): create OpenGL objects as needed instead of each frame
Diffstat (limited to 'engine/src/util.rs')
-rw-r--r--engine/src/util.rs37
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
+ }
+}