add UnalignedSlice

This commit is contained in:
martin 2024-04-18 10:01:03 -05:00
parent 7827b6b22a
commit 8bb1603ed3
2 changed files with 23 additions and 7 deletions

View File

@ -94,7 +94,7 @@ fn properties_output(members: &[PropertiesMember]) -> TokenStream {
quote! {
properties_ffi.#ffi_member.map(|s|
unsafe {
std::slice::from_raw_parts(
UnalignedSlice::new(
s #ffi_member_field .#ffi_name .cast_const(),
s #ffi_member_field .#len_field_name as _,
)

View File

@ -17,11 +17,29 @@ use crate::{
shader::ShaderStages,
DeviceSize, Version,
};
use std::ffi::c_char;
use std::{ffi::c_char, mem};
// Generated by build.rs
include!(concat!(env!("OUT_DIR"), "/properties.rs"));
/// Wrapper for holding and accessing a potentially unaligned sequence of pointers
struct UnalignedSlice<T> {
ptr: *const T,
len: usize,
}
impl<T> UnalignedSlice<T> {
pub unsafe fn new(ptr: *const T, len: usize) -> UnalignedSlice<T> {
UnalignedSlice { ptr, len }
}
pub fn iter(&self) -> impl Iterator<Item = &T> {
(0..self.len)
.into_iter()
.map(|idx| unsafe { &*self.ptr.add(idx * mem::size_of::<*const T>()) })
}
}
// A bit of a hack...
// TODO: integrate into autogen?
pub(crate) trait FromVulkan<F>
@ -307,12 +325,10 @@ impl FromVulkan<ash::vk::ImageLayout> for ImageLayout {
}
}
impl<U: for<'a> FromVulkan<&'a T>, T> FromVulkan<&[T]> for Vec<U> {
impl<U: for<'a> FromVulkan<&'a T>, T> FromVulkan<UnalignedSlice<T>> for Vec<U> {
#[inline]
fn from_vulkan(val: &[T]) -> Option<Vec<U>> {
val.iter()
.map(|it| U::from_vulkan(it))
.collect::<Option<Vec<_>>>()
fn from_vulkan(val: UnalignedSlice<T>) -> Option<Vec<U>> {
val.iter().map(U::from_vulkan).collect::<Option<Vec<_>>>()
}
}