mirror of
https://github.com/vulkano-rs/vulkano.git
synced 2024-11-21 22:34:43 +00:00
add UnalignedSlice
This commit is contained in:
parent
7827b6b22a
commit
8bb1603ed3
@ -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 _,
|
||||
)
|
||||
|
@ -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<_>>>()
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user