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 { items: T, len: usize, } impl SizedArray { /// 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 for SizedArray<&'a RuntimeArray> { type Output=T; fn index(&self, idx: usize) -> &Self::Output { debug_assert!(idx < self.len); unsafe { self.items.index(idx) } } } impl<'a, T> Index for SizedArray<&'a mut RuntimeArray> { type Output=T; fn index(&self, idx: usize) -> &Self::Output { debug_assert!(idx < self.len); unsafe { self.items.index(idx) } } } impl<'a, T> IndexMut for SizedArray<&'a mut RuntimeArray> { fn index_mut(&mut self, idx: usize) -> &mut Self::Output { debug_assert!(idx < self.len); unsafe { self.items.index_mut(idx) } } }