Allow typecast for VkMappedMemory

This commit is contained in:
hodasemi 2023-03-20 14:18:27 +01:00
parent 92b4f55192
commit dee3fd1213

View file

@ -1,5 +1,7 @@
use core::slice::{Iter, IterMut}; use core::slice::{Iter, IterMut};
use std::mem;
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut};
use std::rc::Rc;
use std::{fmt, fmt::Debug}; use std::{fmt, fmt::Debug};
pub struct VkMappedMemory<'a, T> pub struct VkMappedMemory<'a, T>
@ -8,7 +10,7 @@ where
{ {
data: &'a mut [T], data: &'a mut [T],
unmap: Option<Box<dyn Fn()>>, unmap: Option<Rc<dyn Fn()>>,
} }
impl<'a, T: Clone> VkMappedMemory<'a, T> { impl<'a, T: Clone> VkMappedMemory<'a, T> {
@ -18,9 +20,9 @@ impl<'a, T: Clone> VkMappedMemory<'a, T> {
pub fn set_unmap<F>(&mut self, f: F) pub fn set_unmap<F>(&mut self, f: F)
where where
F: Fn() + 'static, F: Fn() + Clone + 'static,
{ {
self.unmap = Some(Box::new(f)); self.unmap = Some(Rc::new(f));
} }
pub fn copy(&mut self, data: &[T]) { pub fn copy(&mut self, data: &[T]) {
@ -34,6 +36,19 @@ impl<'a, T: Clone> VkMappedMemory<'a, T> {
pub fn iter_mut(&mut self) -> IterMut<'_, T> { pub fn iter_mut(&mut self) -> IterMut<'_, T> {
self.data.iter_mut() self.data.iter_mut()
} }
pub unsafe fn reinterpret<U: Clone>(self) -> VkMappedMemory<'a, U> {
debug_assert_eq!(mem::size_of::<U>(), mem::size_of::<T>());
let mem = VkMappedMemory {
data: mem::transmute(self.data as *mut [T]),
unmap: self.unmap.clone(),
};
mem::forget(self);
mem
}
} }
impl<'a, T: Clone> Index<usize> for VkMappedMemory<'a, T> { impl<'a, T: Clone> Index<usize> for VkMappedMemory<'a, T> {