Implement Byteable and RenderResource for [T; N] (#1872)

Implements `Byteable` and `RenderResource` for any array containing `Byteable` elements. This allows `RenderResources` to be implemented on structs with arbitrarily-sized arrays, among other things:

```rust
#[derive(RenderResources, TypeUuid)]
#[uuid = "2733ff34-8f95-459f-bf04-3274e686ac5f"]
struct Foo {
    buffer: [i32; 256],
}
```
This commit is contained in:
TehPers 2021-04-14 22:20:25 +00:00
parent df3f40afd4
commit deb9f23667
2 changed files with 29 additions and 4 deletions

View file

@ -83,10 +83,8 @@ where
T: Byteable,
{
}
unsafe impl<T> Byteable for [T; 2] where T: Byteable {}
unsafe impl<T> Byteable for [T; 3] where T: Byteable {}
unsafe impl<T> Byteable for [T; 4] where T: Byteable {}
unsafe impl<T> Byteable for [T; 16] where T: Byteable {}
unsafe impl<T, const N: usize> Byteable for [T; N] where T: Byteable {}
unsafe impl Byteable for u8 {}
unsafe impl Byteable for u16 {}
@ -233,4 +231,10 @@ mod tests {
fn test_mat4_round_trip() {
test_round_trip(Mat4::IDENTITY);
}
#[test]
fn test_array_round_trip() {
test_round_trip([-10i32; 200]);
test_round_trip([Vec2::ZERO, Vec2::ONE, Vec2::Y, Vec2::X]);
}
}

View file

@ -187,6 +187,27 @@ where
}
}
impl<T, const N: usize> RenderResource for [T; N]
where
T: Sized + Byteable,
{
fn resource_type(&self) -> Option<RenderResourceType> {
Some(RenderResourceType::Buffer)
}
fn write_buffer_bytes(&self, buffer: &mut [u8]) {
self.write_bytes(buffer);
}
fn buffer_byte_len(&self) -> Option<usize> {
Some(self.byte_len())
}
fn texture(&self) -> Option<&Handle<Texture>> {
None
}
}
impl RenderResource for GlobalTransform {
fn resource_type(&self) -> Option<RenderResourceType> {
Some(RenderResourceType::Buffer)