Files
fdtd-coremem/crates/spirv_backend/src/support.rs

52 lines
1.4 KiB
Rust

use core::ops::{Index, IndexMut};
use spirv_std::RuntimeArray;
/// this is intended to wrap an unsized array with a length and provide safe indexing operations
/// into it. it behaves quite similar to a native rust slice, and ideally we'd just use that but
/// spirv support for slices is poor.
pub struct SizedArray<T> {
items: T,
len: usize,
}
impl<T> SizedArray<T> {
/// construct the slice from some other collection and the number of items in said collection.
/// safety: caller must validate that it's safe to index all `len` elements in the underlying
/// collection.
pub unsafe fn new(items: T, len: usize) -> Self {
Self { items, len }
}
}
impl<'a, T> Index<usize> for SizedArray<&'a RuntimeArray<T>> {
type Output=T;
fn index(&self, idx: usize) -> &Self::Output {
debug_assert!(idx < self.len);
unsafe {
self.items.index(idx)
}
}
}
impl<'a, T> Index<usize> for SizedArray<&'a mut RuntimeArray<T>> {
type Output=T;
fn index(&self, idx: usize) -> &Self::Output {
debug_assert!(idx < self.len);
unsafe {
self.items.index(idx)
}
}
}
impl<'a, T> IndexMut<usize> for SizedArray<&'a mut RuntimeArray<T>> {
fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
debug_assert!(idx < self.len);
unsafe {
self.items.index_mut(idx)
}
}
}