From d65a2c783da7cab757e815dccb957420f9dad9ec Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Wed, 3 Jun 2020 11:12:39 -0700 Subject: [PATCH] bytes: impl for Vec --- crates/bevy_core/src/bytes.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/crates/bevy_core/src/bytes.rs b/crates/bevy_core/src/bytes.rs index 6b67049e95..6b217cd5cd 100644 --- a/crates/bevy_core/src/bytes.rs +++ b/crates/bevy_core/src/bytes.rs @@ -43,7 +43,7 @@ where } } -unsafe impl Byteable for [T] where Self: Sized {} +unsafe impl Byteable for [T] where Self: Sized, T: Byteable {} unsafe impl Byteable for [T; 2] where T: Byteable {} unsafe impl Byteable for [T; 3] where T: Byteable {} unsafe impl Byteable for [T; 4] where T: Byteable {} @@ -78,7 +78,8 @@ impl Bytes for Vec3 { array.write_bytes(buffer); } fn byte_len(&self) -> usize { - std::mem::size_of::() + // cant use self here because Vec3 is a simd type / technically a vec4 + std::mem::size_of::<[f32;3]>() } } @@ -115,3 +116,16 @@ where self.as_ref().map_or(0, |val| val.byte_len()) } } + +impl Bytes for Vec +where + T: Sized + Byteable, +{ + fn write_bytes(&self, buffer: &mut [u8]) { + let bytes = self.as_slice().as_bytes(); + buffer[0..self.byte_len()].copy_from_slice(bytes) + } + fn byte_len(&self) -> usize { + self.as_slice().as_bytes().len() + } +} \ No newline at end of file