2016-01-07 05:51:53 -05:00
|
|
|
use sigils::{Zero, Number, Real};
|
2016-01-07 05:10:50 -05:00
|
|
|
use sigils::vector::{Vector, Vector2, Vector3, Vector4};
|
2016-01-07 05:51:53 -05:00
|
|
|
use sigils::quaternion::Quaternion;
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-06 23:37:18 -05:00
|
|
|
use ::byte_sized::ByteSized;
|
2016-01-05 17:46:31 -05:00
|
|
|
use ::converter::Converter;
|
|
|
|
use ::endian::{BigEndian, LittleEndian, PlatformEndian, Endianess};
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
|
|
|
|
|
2016-01-07 17:49:21 -05:00
|
|
|
// From and Into are not used because we need to also
|
|
|
|
// know the endianess to use for converting.
|
|
|
|
/// A type that can be converted to and from bytes.
|
|
|
|
pub trait Transmutable
|
|
|
|
{
|
|
|
|
/// Transmute this type to an array of bytes.
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess);
|
|
|
|
|
|
|
|
/// Transmute an array of bytes to this type.
|
|
|
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-01-05 23:42:08 -05:00
|
|
|
/// Handles the repetative endianess matching
|
|
|
|
/// for the primitive number types when converting
|
|
|
|
/// a number to bytes.
|
2016-01-05 17:46:31 -05:00
|
|
|
macro_rules! handle_endianess_to_bytes
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-01-05 17:46:31 -05:00
|
|
|
($buffer: ident, $val: ident, $endianess: ident, $func: ident) =>
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-01-05 17:46:31 -05:00
|
|
|
{
|
|
|
|
let mut mut_buffer;
|
|
|
|
|
|
|
|
mut_buffer = $buffer;
|
|
|
|
match $endianess
|
|
|
|
{
|
|
|
|
Endianess::BIG =>
|
|
|
|
{
|
|
|
|
BigEndian::$func(&mut mut_buffer, *$val);
|
|
|
|
}
|
|
|
|
|
|
|
|
Endianess::LITTLE =>
|
|
|
|
{
|
|
|
|
LittleEndian::$func(&mut mut_buffer, *$val);
|
|
|
|
}
|
|
|
|
|
|
|
|
Endianess::PLATFORM =>
|
|
|
|
{
|
|
|
|
PlatformEndian::$func(&mut mut_buffer, *$val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-30 17:06:48 -05:00
|
|
|
}
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-05 23:42:08 -05:00
|
|
|
/// Handles the repetative endianess matching
|
|
|
|
/// for the primitive number types when converting
|
|
|
|
/// a number from bytes.
|
2016-01-05 17:46:31 -05:00
|
|
|
macro_rules! handle_endianess_from_bytes
|
|
|
|
{
|
|
|
|
($buffer: ident, $endianess: ident, $func: ident) =>
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-01-05 17:46:31 -05:00
|
|
|
match $endianess
|
|
|
|
{
|
|
|
|
Endianess::BIG =>
|
|
|
|
{
|
|
|
|
BigEndian::$func(&$buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
Endianess::LITTLE =>
|
|
|
|
{
|
|
|
|
LittleEndian::$func(&$buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
Endianess::PLATFORM =>
|
|
|
|
{
|
|
|
|
PlatformEndian::$func(&$buffer)
|
|
|
|
}
|
|
|
|
}
|
2015-12-30 17:06:48 -05:00
|
|
|
}
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
impl Transmutable for u8
|
|
|
|
{
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-01-05 17:46:31 -05:00
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
assert!(buffer.len() >= u8::BYTES);
|
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
// Endianess doesn't matter here.
|
|
|
|
buffer[0] = *self;
|
2015-12-30 17:06:48 -05:00
|
|
|
}
|
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
#[allow(unused_variables)]
|
|
|
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> u8
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-01-05 17:46:31 -05:00
|
|
|
// Make sure that there is enough data to read
|
|
|
|
// the bytes for this type.
|
|
|
|
assert!(buffer.len() >= u8::BYTES);
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
// Convert the given bytes to this type and return it.
|
|
|
|
// Endianess doesn't matter here.
|
|
|
|
buffer[0]
|
2015-12-30 17:06:48 -05:00
|
|
|
}
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
impl Transmutable for u16
|
|
|
|
{
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-01-05 17:46:31 -05:00
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
assert!(buffer.len() >= u16::BYTES);
|
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, u16_to_bytes);
|
2015-12-30 17:06:48 -05:00
|
|
|
}
|
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> u16
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-01-05 17:46:31 -05:00
|
|
|
// Make sure that there is enough data to read
|
|
|
|
// the bytes for this type.
|
|
|
|
assert!(buffer.len() >= u16::BYTES);
|
|
|
|
|
2016-01-06 23:37:18 -05:00
|
|
|
// Convert the given bytes to this type and return it.
|
2016-01-05 17:46:31 -05:00
|
|
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_u16)
|
2015-12-30 17:06:48 -05:00
|
|
|
}
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Transmutable for u32
|
|
|
|
{
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
|
|
|
{
|
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
assert!(buffer.len() >= u32::BYTES);
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, u32_to_bytes);
|
|
|
|
}
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> u32
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-01-05 17:46:31 -05:00
|
|
|
// Make sure that there is enough data to read
|
|
|
|
// the bytes for this type.
|
|
|
|
assert!(buffer.len() >= u32::BYTES);
|
|
|
|
|
2016-01-06 23:37:18 -05:00
|
|
|
// Convert the given bytes to this type and return it.
|
2016-01-05 17:46:31 -05:00
|
|
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_u32)
|
2015-12-30 17:06:48 -05:00
|
|
|
}
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
impl Transmutable for u64
|
|
|
|
{
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-01-05 17:46:31 -05:00
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
assert!(buffer.len() >= u64::BYTES);
|
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, u64_to_bytes);
|
2015-12-30 17:06:48 -05:00
|
|
|
}
|
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> u64
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-01-05 17:46:31 -05:00
|
|
|
// Make sure that there is enough data to read
|
|
|
|
// the bytes for this type.
|
|
|
|
assert!(buffer.len() >= u64::BYTES);
|
|
|
|
|
2016-01-06 23:37:18 -05:00
|
|
|
// Convert the given bytes to this type and return it.
|
2016-01-05 17:46:31 -05:00
|
|
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_u64)
|
2015-12-30 17:06:48 -05:00
|
|
|
}
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
impl Transmutable for usize
|
|
|
|
{
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-01-05 17:46:31 -05:00
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
assert!(buffer.len() >= usize::BYTES);
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, usize_to_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> usize
|
|
|
|
{
|
|
|
|
// Make sure that there is enough data to read
|
|
|
|
// the bytes for this type.
|
|
|
|
assert!(buffer.len() >= usize::BYTES);
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-06 23:37:18 -05:00
|
|
|
// Convert the given bytes to this type and return it.
|
2016-01-05 17:46:31 -05:00
|
|
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_usize)
|
2015-12-30 17:06:48 -05:00
|
|
|
}
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
impl Transmutable for i8
|
|
|
|
{
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-01-05 17:46:31 -05:00
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
assert!(buffer.len() >= i8::BYTES);
|
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
buffer[0] = *self as u8;
|
2015-12-30 17:06:48 -05:00
|
|
|
}
|
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
#[allow(unused_variables)]
|
|
|
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> i8
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-01-05 17:46:31 -05:00
|
|
|
// Make sure that there is enough data to read
|
|
|
|
// the bytes for this type.
|
|
|
|
assert!(buffer.len() >= i8::BYTES);
|
|
|
|
|
|
|
|
// Convert the given bytes to this type and return it.
|
|
|
|
// Endianess doesn't matter here.
|
|
|
|
buffer[0] as i8
|
2015-12-30 17:06:48 -05:00
|
|
|
}
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
impl Transmutable for i16
|
|
|
|
{
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
|
|
|
{
|
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
assert!(buffer.len() >= i16::BYTES);
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, i16_to_bytes);
|
|
|
|
}
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> i16
|
|
|
|
{
|
|
|
|
// Make sure that there is enough data to read
|
|
|
|
// the bytes for this type.
|
|
|
|
assert!(buffer.len() >= i16::BYTES);
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-06 23:37:18 -05:00
|
|
|
// Convert the given bytes to this type and return it.
|
2016-01-05 17:46:31 -05:00
|
|
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_i16)
|
|
|
|
}
|
|
|
|
}
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
impl Transmutable for i32
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-01-05 17:46:31 -05:00
|
|
|
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
|
|
|
{
|
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
assert!(buffer.len() >= i32::BYTES);
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, i32_to_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> i32
|
|
|
|
{
|
|
|
|
// Make sure that there is enough data to read
|
|
|
|
// the bytes for this type.
|
|
|
|
assert!(buffer.len() >= i32::BYTES);
|
|
|
|
|
2016-01-06 23:37:18 -05:00
|
|
|
// Convert the given bytes to this type and return it.
|
2016-01-05 17:46:31 -05:00
|
|
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_i32)
|
|
|
|
}
|
2015-12-30 17:06:48 -05:00
|
|
|
}
|
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
impl Transmutable for i64
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-01-05 17:46:31 -05:00
|
|
|
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
|
|
|
{
|
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
assert!(buffer.len() >= i64::BYTES);
|
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, i64_to_bytes);
|
|
|
|
}
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> i64
|
|
|
|
{
|
|
|
|
// Make sure that there is enough data to read
|
|
|
|
// the bytes for this type.
|
|
|
|
assert!(buffer.len() >= i64::BYTES);
|
|
|
|
|
2016-01-06 23:37:18 -05:00
|
|
|
// Convert the given bytes to this type and return it.
|
2016-01-05 17:46:31 -05:00
|
|
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_i64)
|
|
|
|
}
|
2015-12-30 17:06:48 -05:00
|
|
|
}
|
2016-01-02 14:54:03 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
impl Transmutable for isize
|
|
|
|
{
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
|
|
|
{
|
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
assert!(buffer.len() >= isize::BYTES);
|
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, isize_to_bytes);
|
|
|
|
}
|
2016-01-02 14:54:03 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> isize
|
|
|
|
{
|
|
|
|
// Make sure that there is enough data to read
|
|
|
|
// the bytes for this type.
|
|
|
|
assert!(buffer.len() >= isize::BYTES);
|
2016-01-02 14:54:03 -05:00
|
|
|
|
2016-01-06 23:37:18 -05:00
|
|
|
// Convert the given bytes to this type and return it.
|
2016-01-05 17:46:31 -05:00
|
|
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_isize)
|
|
|
|
}
|
|
|
|
}
|
2016-01-02 14:54:03 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
impl Transmutable for f32
|
2016-01-02 14:54:03 -05:00
|
|
|
{
|
2016-01-05 17:46:31 -05:00
|
|
|
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
|
|
|
{
|
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
assert!(buffer.len() >= 4);
|
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, f32_to_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> f32
|
|
|
|
{
|
|
|
|
// Make sure that there is enough data to read
|
|
|
|
// the bytes for this type.
|
|
|
|
assert!(buffer.len() >= 4);
|
|
|
|
|
2016-01-06 23:37:18 -05:00
|
|
|
// Convert the given bytes to this type and return it.
|
2016-01-05 17:46:31 -05:00
|
|
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_f32)
|
|
|
|
}
|
2016-01-02 14:54:03 -05:00
|
|
|
}
|
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
impl Transmutable for f64
|
2016-01-02 14:54:03 -05:00
|
|
|
{
|
2016-01-05 17:46:31 -05:00
|
|
|
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
|
|
|
{
|
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
assert!(buffer.len() >= 8);
|
2016-01-02 14:54:03 -05:00
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, f64_to_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> f64
|
|
|
|
{
|
|
|
|
// Make sure that there is enough data to read
|
|
|
|
// the bytes for this type.
|
|
|
|
assert!(buffer.len() >= 8);
|
|
|
|
|
2016-01-06 23:37:18 -05:00
|
|
|
// Convert the given bytes to this type and return it.
|
2016-01-05 17:46:31 -05:00
|
|
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_f64)
|
|
|
|
}
|
2016-01-02 14:54:03 -05:00
|
|
|
}
|
2016-01-05 23:42:08 -05:00
|
|
|
|
2016-01-06 23:37:18 -05:00
|
|
|
impl<T> Transmutable for Vector2<T> where T: Number + ByteSized + Transmutable
|
|
|
|
{
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
|
|
|
{
|
|
|
|
let byte_size: usize;
|
|
|
|
let num_bytes: usize;
|
|
|
|
|
|
|
|
// Determine the number of bytes requires to
|
|
|
|
// represent a Vector2.
|
|
|
|
byte_size = T::get_byte_size();
|
|
|
|
num_bytes = byte_size * self.get_size() as usize;
|
2016-01-05 23:42:08 -05:00
|
|
|
|
2016-01-06 23:37:18 -05:00
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
assert!(buffer.len() >= num_bytes);
|
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
self.x.to_bytes(&mut buffer[0..byte_size], endianess);
|
|
|
|
self.y.to_bytes(&mut buffer[byte_size..num_bytes], endianess);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Vector2<T>
|
|
|
|
{
|
|
|
|
let byte_size: usize;
|
|
|
|
let num_bytes: usize;
|
|
|
|
let mut vec: Vector2<T>;
|
|
|
|
|
|
|
|
// Determine the number of bytes requires to
|
|
|
|
// represent a Vector2.
|
|
|
|
vec = Vector2::<T>::zero();
|
|
|
|
byte_size = T::get_byte_size();
|
|
|
|
num_bytes = byte_size * vec.get_size() as usize;
|
|
|
|
|
|
|
|
// Make sure that there is enough data to read
|
|
|
|
// the bytes for this type.
|
|
|
|
assert!(buffer.len() >= num_bytes);
|
|
|
|
|
|
|
|
// Convert the given bytes to this type and return it.
|
|
|
|
vec.x = T::from_bytes(&buffer[0..byte_size], endianess);
|
|
|
|
vec.y = T::from_bytes(&buffer[byte_size..num_bytes], endianess);
|
|
|
|
vec
|
|
|
|
}
|
|
|
|
}
|
2016-01-07 05:10:50 -05:00
|
|
|
|
|
|
|
impl<T> Transmutable for Vector3<T> where T: Number + ByteSized + Transmutable
|
|
|
|
{
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
|
|
|
{
|
|
|
|
let byte_size: usize;
|
|
|
|
let num_bytes: usize;
|
|
|
|
|
|
|
|
// Determine the number of bytes requires to
|
|
|
|
// represent a Vector3.
|
|
|
|
byte_size = T::get_byte_size();
|
|
|
|
num_bytes = byte_size * self.get_size() as usize;
|
|
|
|
|
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
assert!(buffer.len() >= num_bytes);
|
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
self.x.to_bytes(&mut buffer[0..byte_size], endianess);
|
|
|
|
self.y.to_bytes(&mut buffer[byte_size..(byte_size*2)], endianess);
|
|
|
|
self.z.to_bytes(&mut buffer[(byte_size*2)..num_bytes], endianess);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Vector3<T>
|
|
|
|
{
|
|
|
|
let byte_size: usize;
|
|
|
|
let num_bytes: usize;
|
|
|
|
let mut vec: Vector3<T>;
|
|
|
|
|
|
|
|
// Determine the number of bytes requires to
|
|
|
|
// represent a Vector3.
|
|
|
|
vec = Vector3::<T>::zero();
|
|
|
|
byte_size = T::get_byte_size();
|
|
|
|
num_bytes = byte_size * vec.get_size() as usize;
|
|
|
|
|
|
|
|
// Make sure that there is enough data to read
|
|
|
|
// the bytes for this type.
|
|
|
|
assert!(buffer.len() >= num_bytes);
|
|
|
|
|
|
|
|
// Convert the given bytes to this type and return it.
|
|
|
|
vec.x = T::from_bytes(&buffer[0..byte_size], endianess);
|
|
|
|
vec.y = T::from_bytes(&buffer[byte_size..(byte_size*2)], endianess);
|
|
|
|
vec.z = T::from_bytes(&buffer[(byte_size*2)..num_bytes], endianess);
|
|
|
|
vec
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Transmutable for Vector4<T> where T: Number + ByteSized + Transmutable
|
|
|
|
{
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
|
|
|
{
|
|
|
|
let byte_size: usize;
|
|
|
|
let num_bytes: usize;
|
|
|
|
|
|
|
|
// Determine the number of bytes requires to
|
|
|
|
// represent a Vector4.
|
|
|
|
byte_size = T::get_byte_size();
|
|
|
|
num_bytes = byte_size * self.get_size() as usize;
|
|
|
|
|
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
assert!(buffer.len() >= num_bytes);
|
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
self.x.to_bytes(&mut buffer[0..byte_size], endianess);
|
|
|
|
self.y.to_bytes(&mut buffer[byte_size..(byte_size*2)], endianess);
|
|
|
|
self.z.to_bytes(&mut buffer[(byte_size*2)..(byte_size*3)], endianess);
|
|
|
|
self.w.to_bytes(&mut buffer[(byte_size*3)..num_bytes], endianess);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Vector4<T>
|
|
|
|
{
|
|
|
|
let byte_size: usize;
|
|
|
|
let num_bytes: usize;
|
|
|
|
let mut vec: Vector4<T>;
|
|
|
|
|
|
|
|
// Determine the number of bytes requires to
|
|
|
|
// represent a Vector4.
|
|
|
|
vec = Vector4::<T>::zero();
|
|
|
|
byte_size = T::get_byte_size();
|
|
|
|
num_bytes = byte_size * vec.get_size() as usize;
|
|
|
|
|
|
|
|
// Make sure that there is enough data to read
|
|
|
|
// the bytes for this type.
|
|
|
|
assert!(buffer.len() >= num_bytes);
|
|
|
|
|
|
|
|
// Convert the given bytes to this type and return it.
|
|
|
|
vec.x = T::from_bytes(&buffer[0..byte_size], endianess);
|
|
|
|
vec.y = T::from_bytes(&buffer[byte_size..(byte_size*2)], endianess);
|
|
|
|
vec.z = T::from_bytes(&buffer[(byte_size*2)..(byte_size*3)], endianess);
|
|
|
|
vec.w = T::from_bytes(&buffer[(byte_size*3)..num_bytes], endianess);
|
|
|
|
vec
|
|
|
|
}
|
|
|
|
}
|
2016-01-07 05:51:53 -05:00
|
|
|
|
|
|
|
impl<T> Transmutable for Quaternion<T>
|
|
|
|
where T: Real + ByteSized + Transmutable
|
|
|
|
{
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
|
|
|
{
|
|
|
|
let byte_size: usize;
|
|
|
|
let num_bytes: usize;
|
|
|
|
|
|
|
|
// Determine the number of bytes requires to
|
|
|
|
// represent a Quaternion.
|
|
|
|
byte_size = T::get_byte_size();
|
|
|
|
num_bytes = byte_size * 4usize;
|
|
|
|
|
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
assert!(buffer.len() >= num_bytes);
|
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
self.scalar.to_bytes(&mut buffer[0..byte_size], endianess);
|
|
|
|
self.vector.to_bytes(&mut buffer[byte_size..num_bytes], endianess);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Quaternion<T>
|
|
|
|
{
|
|
|
|
let byte_size: usize;
|
|
|
|
let num_bytes: usize;
|
|
|
|
let mut quat: Quaternion<T>;
|
|
|
|
|
|
|
|
// Determine the number of bytes requires to
|
|
|
|
// represent a Quaternion.
|
|
|
|
quat = Quaternion::<T>::zero();
|
|
|
|
byte_size = T::get_byte_size();
|
|
|
|
num_bytes = byte_size * 4usize;
|
|
|
|
|
|
|
|
// Make sure that there is enough data to read
|
|
|
|
// the bytes for this type.
|
|
|
|
assert!(buffer.len() >= num_bytes);
|
|
|
|
|
|
|
|
// Convert the given bytes to this type and return it.
|
|
|
|
quat.scalar = T::from_bytes(&buffer[0..byte_size], endianess);
|
|
|
|
quat.vector =
|
|
|
|
Vector3::<T>::from_bytes(&buffer[byte_size..num_bytes], endianess);
|
|
|
|
quat
|
|
|
|
}
|
|
|
|
}
|