bevy/crates/bevy_core/src/bytes.rs

83 lines
1.7 KiB
Rust
Raw Normal View History

2020-04-06 03:19:02 +00:00
use glam::{Mat4, Vec4};
2020-02-18 03:53:48 +00:00
use zerocopy::AsBytes;
pub trait GetBytes {
fn get_bytes(&self) -> Vec<u8>;
fn get_bytes_ref(&self) -> Option<&[u8]>;
}
impl GetBytes for f32 {
fn get_bytes(&self) -> Vec<u8> {
self.as_bytes().to_vec()
}
fn get_bytes_ref(&self) -> Option<&[u8]> {
Some(self.as_bytes())
}
}
impl GetBytes for [f32; 2] {
fn get_bytes(&self) -> Vec<u8> {
self.as_bytes().to_vec()
}
fn get_bytes_ref(&self) -> Option<&[u8]> {
Some(self.as_bytes())
}
}
impl GetBytes for [f32; 3] {
fn get_bytes(&self) -> Vec<u8> {
self.as_bytes().to_vec()
}
fn get_bytes_ref(&self) -> Option<&[u8]> {
Some(self.as_bytes())
}
}
impl GetBytes for [f32; 4] {
fn get_bytes(&self) -> Vec<u8> {
self.as_bytes().to_vec()
}
fn get_bytes_ref(&self) -> Option<&[u8]> {
Some(self.as_bytes())
}
}
2020-02-18 03:53:48 +00:00
impl GetBytes for Vec4 {
fn get_bytes(&self) -> Vec<u8> {
let vec4_array: [f32; 4] = (*self).into();
vec4_array.as_bytes().into()
}
fn get_bytes_ref(&self) -> Option<&[u8]> {
2020-03-15 01:32:33 +00:00
Some(self.as_ref().as_bytes())
2020-02-18 03:53:48 +00:00
}
}
2020-02-22 23:01:11 +00:00
impl GetBytes for Mat4 {
fn get_bytes(&self) -> Vec<u8> {
self.as_ref()
.as_bytes()
.iter()
.cloned()
.collect::<Vec<u8>>()
}
fn get_bytes_ref(&self) -> Option<&[u8]> {
Some(self.as_ref().as_bytes())
}
}
2020-04-06 03:19:02 +00:00
impl<T> GetBytes for Option<T>
where
T: GetBytes,
{
2020-02-22 23:01:11 +00:00
fn get_bytes(&self) -> Vec<u8> {
Vec::new()
}
fn get_bytes_ref(&self) -> Option<&[u8]> {
2020-04-06 03:19:02 +00:00
self.as_ref()
.and_then(|get_bytes| get_bytes.get_bytes_ref())
2020-02-22 23:01:11 +00:00
}
}