2016-08-24 13:23:56 -04:00
|
|
|
#[cfg(feature="convert_sigils")]
|
2016-01-07 05:51:53 -05:00
|
|
|
use sigils::{Zero, Number, Real};
|
2016-08-24 13:23:56 -04:00
|
|
|
#[cfg(feature="convert_sigils")]
|
2016-01-07 05:10:50 -05:00
|
|
|
use sigils::vector::{Vector, Vector2, Vector3, Vector4};
|
2016-08-24 13:23:56 -04:00
|
|
|
#[cfg(feature="convert_sigils")]
|
2016-01-07 05:51:53 -05:00
|
|
|
use sigils::quaternion::Quaternion;
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-04-14 13:33:07 -04:00
|
|
|
use ::byte_sized::{ByteSized, get_byte_size_of_string};
|
2016-01-05 17:46:31 -05:00
|
|
|
use ::converter::Converter;
|
2016-06-17 04:34:36 -04:00
|
|
|
use ::endian::{BigEndian, LittleEndian, PlatformEndian, NetworkEndian};
|
|
|
|
use ::endian::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
|
|
|
|
{
|
2016-12-22 13:47:56 -05:00
|
|
|
/// Transmute this type to an array of bytes.
|
|
|
|
fn as_bytes(&self) -> Vec<u8>;
|
|
|
|
|
|
|
|
/// Transmute this type to an array of bytes.
|
|
|
|
fn as_endian_bytes(&self, endianess: Endianess) -> Vec<u8>;
|
|
|
|
|
2016-04-14 18:08:50 -04:00
|
|
|
/// Transmute an array of bytes to this type.
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8]) -> Self;
|
|
|
|
|
|
|
|
/// Transmute an array of bytes to this type.
|
|
|
|
fn from_endian_bytes(buffer: &[u8], endianess: Endianess) -> Self;
|
2016-04-14 18:08:50 -04:00
|
|
|
|
|
|
|
/// Get the current size of this Transmutable in bytes.
|
|
|
|
fn determine_byte_size(&self) -> usize;
|
2016-01-07 17:49:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
match $endianess
|
|
|
|
{
|
2016-04-14 15:08:31 -04:00
|
|
|
Endianess::Big =>
|
2016-01-05 17:46:31 -05:00
|
|
|
{
|
2016-04-14 13:33:07 -04:00
|
|
|
$buffer.append(&mut BigEndian::$func(*$val));
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
|
|
|
|
2016-04-14 15:08:31 -04:00
|
|
|
Endianess::Little =>
|
2016-01-05 17:46:31 -05:00
|
|
|
{
|
2016-04-14 13:33:07 -04:00
|
|
|
$buffer.append(&mut LittleEndian::$func(*$val));
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
|
|
|
|
2016-04-14 15:08:31 -04:00
|
|
|
Endianess::Platform =>
|
2016-01-05 17:46:31 -05:00
|
|
|
{
|
2016-04-14 13:33:07 -04:00
|
|
|
$buffer.append(&mut PlatformEndian::$func(*$val));
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
2016-06-17 04:34:36 -04:00
|
|
|
|
|
|
|
Endianess::Network =>
|
|
|
|
{
|
|
|
|
$buffer.append(&mut NetworkEndian::$func(*$val));
|
|
|
|
}
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
{
|
2016-04-14 15:08:31 -04:00
|
|
|
Endianess::Big =>
|
2016-01-05 17:46:31 -05:00
|
|
|
{
|
|
|
|
BigEndian::$func(&$buffer)
|
|
|
|
}
|
|
|
|
|
2016-04-14 15:08:31 -04:00
|
|
|
Endianess::Little =>
|
2016-01-05 17:46:31 -05:00
|
|
|
{
|
|
|
|
LittleEndian::$func(&$buffer)
|
|
|
|
}
|
|
|
|
|
2016-04-14 15:08:31 -04:00
|
|
|
Endianess::Platform =>
|
2016-01-05 17:46:31 -05:00
|
|
|
{
|
|
|
|
PlatformEndian::$func(&$buffer)
|
|
|
|
}
|
2016-06-17 04:34:36 -04:00
|
|
|
|
|
|
|
Endianess::Network =>
|
|
|
|
{
|
|
|
|
NetworkEndian::$func(&$buffer)
|
|
|
|
}
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
2015-12-30 17:06:48 -05:00
|
|
|
}
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
/// Handles the repetative bit packing.
|
|
|
|
macro_rules! pack_bits
|
|
|
|
{
|
|
|
|
($buffer: ident, $target_type: ty, $bytes: expr) =>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let mut value: $target_type;
|
|
|
|
|
|
|
|
// Make sure that there is enough data to read
|
|
|
|
// the bytes for this type.
|
|
|
|
assert!($buffer.len() >= $bytes);
|
|
|
|
|
|
|
|
// Start with a value of zero and or it with
|
|
|
|
// the bits shifted values from the buffer.
|
|
|
|
value = 0;
|
|
|
|
for i in 0..$bytes
|
|
|
|
{
|
|
|
|
value |= ($buffer[i] as $target_type) << ((($bytes - i) - 1) * 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the determined value.
|
|
|
|
value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handles the repetative bit unpacking.
|
|
|
|
macro_rules! unpack_bits
|
|
|
|
{
|
|
|
|
($value: expr, $bytes: expr) =>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
|
|
|
// Create an array with enough space for this value
|
|
|
|
// and then bit shift the value into a buffer of bytes.
|
|
|
|
buffer = Vec::with_capacity($bytes);
|
|
|
|
for i in 0..$bytes
|
|
|
|
{
|
|
|
|
buffer.push(($value >> (($bytes - i) - 1) * 8) as u8);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the buffer of bytes that represent this value.
|
|
|
|
buffer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2016-12-22 13:47:56 -05:00
|
|
|
fn as_bytes(&self) -> Vec<u8>
|
|
|
|
{
|
|
|
|
// Unpack the bits that make up this value and
|
|
|
|
// return a buffer of the bytes.
|
|
|
|
unpack_bits!(*self, u8::BYTES)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
fn as_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
|
|
|
{
|
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
|
|
|
// Create the Vector to hold the byte.
|
|
|
|
buffer = Vec::with_capacity(u8::BYTES);
|
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
// Endianess doesn't matter here.
|
|
|
|
buffer.push(*self);
|
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
|
|
|
}
|
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8]) -> u8
|
|
|
|
{
|
|
|
|
// Convert the given bytes to this type and return it.
|
|
|
|
pack_bits!(buffer, u8, u8::BYTES)
|
|
|
|
}
|
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
#[allow(unused_variables)]
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_endian_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-12-22 13:47:56 -05:00
|
|
|
fn determine_byte_size(&self) -> usize
|
|
|
|
{
|
|
|
|
u8::BYTES
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Transmutable for u16
|
|
|
|
{
|
|
|
|
fn as_bytes(&self) -> Vec<u8>
|
2016-12-21 04:22:31 -05:00
|
|
|
{
|
|
|
|
// Unpack the bits that make up this value and
|
|
|
|
// return a buffer of the bytes.
|
2016-12-22 13:47:56 -05:00
|
|
|
unpack_bits!(*self, u16::BYTES)
|
2016-12-21 04:22:31 -05:00
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn as_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-04-14 13:33:07 -04:00
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
// Create the Vector to hold the bytes
|
|
|
|
// from this type.
|
|
|
|
buffer = Vec::with_capacity(u16::BYTES);
|
2016-01-05 17:46:31 -05:00
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
2016-12-22 13:47:56 -05:00
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, u16_to_bytes);
|
2016-04-14 13:33:07 -04:00
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
2015-12-30 17:06:48 -05:00
|
|
|
}
|
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8]) -> u16
|
|
|
|
{
|
|
|
|
// Convert the given bytes to this type and return it.
|
|
|
|
pack_bits!(buffer, u16, u16::BYTES)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_endian_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
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn determine_byte_size(&self) -> usize
|
|
|
|
{
|
|
|
|
u16::BYTES
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Transmutable for u32
|
|
|
|
{
|
|
|
|
fn as_bytes(&self) -> Vec<u8>
|
2016-12-21 04:22:31 -05:00
|
|
|
{
|
|
|
|
// Unpack the bits that make up this value and
|
|
|
|
// return a buffer of the bytes.
|
2016-12-22 13:47:56 -05:00
|
|
|
unpack_bits!(*self, u32::BYTES)
|
2016-12-21 04:22:31 -05:00
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn as_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
2016-01-05 17:46:31 -05:00
|
|
|
{
|
2016-04-14 13:33:07 -04:00
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
|
|
|
// Create the Vector to hold the bytes
|
|
|
|
// from this type.
|
2016-12-22 13:47:56 -05:00
|
|
|
buffer = Vec::with_capacity(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.
|
2016-12-22 13:47:56 -05:00
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, u32_to_bytes);
|
2016-04-14 13:33:07 -04:00
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8]) -> u32
|
|
|
|
{
|
|
|
|
// Convert the given bytes to this type and return it.
|
|
|
|
pack_bits!(buffer, u32, u32::BYTES)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_endian_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-12-22 13:47:56 -05:00
|
|
|
fn determine_byte_size(&self) -> usize
|
|
|
|
{
|
|
|
|
u32::BYTES
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Transmutable for u64
|
|
|
|
{
|
|
|
|
fn as_bytes(&self) -> Vec<u8>
|
2016-12-21 04:22:31 -05:00
|
|
|
{
|
|
|
|
// Unpack the bits that make up this value and
|
|
|
|
// return a buffer of the bytes.
|
2016-12-22 13:47:56 -05:00
|
|
|
unpack_bits!(*self, u64::BYTES)
|
2016-12-21 04:22:31 -05:00
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn as_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-04-14 13:33:07 -04:00
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
|
|
|
// Create the Vector to hold the bytes
|
|
|
|
// from this type.
|
2016-12-22 13:47:56 -05:00
|
|
|
buffer = Vec::with_capacity(u64::BYTES);
|
2016-01-05 17:46:31 -05:00
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
2016-12-22 13:47:56 -05:00
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, u64_to_bytes);
|
2016-04-14 13:33:07 -04:00
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
2015-12-30 17:06:48 -05:00
|
|
|
}
|
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8]) -> u64
|
|
|
|
{
|
|
|
|
// Convert the given bytes to this type and return it.
|
|
|
|
pack_bits!(buffer, u64, u64::BYTES)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_endian_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-12-22 13:47:56 -05:00
|
|
|
fn determine_byte_size(&self) -> usize
|
|
|
|
{
|
|
|
|
u64::BYTES
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Transmutable for usize
|
|
|
|
{
|
|
|
|
fn as_bytes(&self) -> Vec<u8>
|
2016-12-21 04:22:31 -05:00
|
|
|
{
|
|
|
|
// Unpack the bits that make up this value and
|
|
|
|
// return a buffer of the bytes.
|
2016-12-22 13:47:56 -05:00
|
|
|
unpack_bits!(*self, usize::BYTES)
|
2016-12-21 04:22:31 -05:00
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn as_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-04-14 13:33:07 -04:00
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
|
|
|
// Create the Vector to hold the bytes
|
|
|
|
// from this type.
|
2016-12-22 13:47:56 -05:00
|
|
|
buffer = Vec::with_capacity(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.
|
2016-12-22 13:47:56 -05:00
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, usize_to_bytes);
|
2016-04-14 13:33:07 -04:00
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8]) -> usize
|
|
|
|
{
|
|
|
|
// Convert the given bytes to this type and return it.
|
|
|
|
pack_bits!(buffer, usize, usize::BYTES)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_endian_bytes(buffer: &[u8], endianess: Endianess) -> usize
|
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() >= 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-12-22 13:47:56 -05:00
|
|
|
fn determine_byte_size(&self) -> usize
|
|
|
|
{
|
|
|
|
usize::BYTES
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Transmutable for i8
|
|
|
|
{
|
|
|
|
fn as_bytes(&self) -> Vec<u8>
|
2016-12-21 04:22:31 -05:00
|
|
|
{
|
|
|
|
// Unpack the bits that make up this value and
|
|
|
|
// return a buffer of the bytes.
|
2016-12-22 13:47:56 -05:00
|
|
|
unpack_bits!(*self, i8::BYTES)
|
2016-12-21 04:22:31 -05:00
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
#[allow(unused_variables)]
|
|
|
|
fn as_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
2015-12-30 17:06:48 -05:00
|
|
|
{
|
2016-04-14 13:33:07 -04:00
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
|
|
|
// Create the Vector to hold the bytes
|
|
|
|
// from this type.
|
2016-12-22 13:47:56 -05:00
|
|
|
buffer = Vec::with_capacity(i8::BYTES);
|
2016-01-05 17:46:31 -05:00
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
2016-12-22 13:47:56 -05:00
|
|
|
buffer.push(*self as u8);
|
2016-04-14 13:33:07 -04:00
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
2015-12-30 17:06:48 -05:00
|
|
|
}
|
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8]) -> i8
|
|
|
|
{
|
|
|
|
// Convert the given bytes to this type and return it.
|
|
|
|
pack_bits!(buffer, i8, i8::BYTES)
|
|
|
|
}
|
|
|
|
|
2016-01-05 17:46:31 -05:00
|
|
|
#[allow(unused_variables)]
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_endian_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-12-22 13:47:56 -05:00
|
|
|
fn determine_byte_size(&self) -> usize
|
|
|
|
{
|
|
|
|
i8::BYTES
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Transmutable for i16
|
|
|
|
{
|
|
|
|
fn as_bytes(&self) -> Vec<u8>
|
2016-12-21 04:22:31 -05:00
|
|
|
{
|
|
|
|
// Unpack the bits that make up this value and
|
|
|
|
// return a buffer of the bytes.
|
2016-12-22 13:47:56 -05:00
|
|
|
unpack_bits!(*self, i16::BYTES)
|
2016-12-21 04:22:31 -05:00
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn as_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
2016-01-05 17:46:31 -05:00
|
|
|
{
|
2016-04-14 13:33:07 -04:00
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
|
|
|
// Create the Vector to hold the bytes
|
|
|
|
// from this type.
|
2016-12-22 13:47:56 -05:00
|
|
|
buffer = Vec::with_capacity(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.
|
2016-12-22 13:47:56 -05:00
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, i16_to_bytes);
|
2016-04-14 13:33:07 -04:00
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8]) -> i16
|
|
|
|
{
|
|
|
|
// Convert the given bytes to this type and return it.
|
|
|
|
pack_bits!(buffer, i16, i16::BYTES)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_endian_bytes(buffer: &[u8], endianess: Endianess) -> i16
|
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() >= 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-12-22 13:47:56 -05:00
|
|
|
fn determine_byte_size(&self) -> usize
|
|
|
|
{
|
|
|
|
i16::BYTES
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Transmutable for i32
|
|
|
|
{
|
|
|
|
fn as_bytes(&self) -> Vec<u8>
|
2016-12-21 04:22:31 -05:00
|
|
|
{
|
|
|
|
// Unpack the bits that make up this value and
|
|
|
|
// return a buffer of the bytes.
|
2016-12-22 13:47:56 -05:00
|
|
|
unpack_bits!(*self, i32::BYTES)
|
2016-12-21 04:22:31 -05:00
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn as_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
2016-01-05 17:46:31 -05:00
|
|
|
{
|
2016-04-14 13:33:07 -04:00
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
|
|
|
// Create the Vector to hold the bytes
|
|
|
|
// from this type.
|
2016-12-22 13:47:56 -05:00
|
|
|
buffer = Vec::with_capacity(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.
|
2016-12-22 13:47:56 -05:00
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, i32_to_bytes);
|
2016-04-14 13:33:07 -04:00
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8]) -> i32
|
|
|
|
{
|
|
|
|
// Convert the given bytes to this type and return it.
|
|
|
|
pack_bits!(buffer, i32, i32::BYTES)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_endian_bytes(buffer: &[u8], endianess: Endianess) -> i32
|
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() >= 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-12-22 13:47:56 -05:00
|
|
|
fn determine_byte_size(&self) -> usize
|
|
|
|
{
|
|
|
|
i32::BYTES
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Transmutable for i64
|
|
|
|
{
|
|
|
|
fn as_bytes(&self) -> Vec<u8>
|
2016-12-21 04:22:31 -05:00
|
|
|
{
|
|
|
|
// Unpack the bits that make up this value and
|
|
|
|
// return a buffer of the bytes.
|
2016-12-22 13:47:56 -05:00
|
|
|
unpack_bits!(*self, i64::BYTES)
|
2016-12-21 04:22:31 -05:00
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn as_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
2016-01-05 17:46:31 -05:00
|
|
|
{
|
2016-04-14 13:33:07 -04:00
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
|
|
|
// Create the Vector to hold the bytes
|
|
|
|
// from this type.
|
2016-12-22 13:47:56 -05:00
|
|
|
buffer = Vec::with_capacity(i64::BYTES);
|
2016-01-05 17:46:31 -05:00
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
2016-12-22 13:47:56 -05:00
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, i64_to_bytes);
|
2016-04-14 13:33:07 -04:00
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
2015-12-30 17:06:48 -05:00
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8]) -> i64
|
|
|
|
{
|
|
|
|
// Convert the given bytes to this type and return it.
|
|
|
|
pack_bits!(buffer, i64, i64::BYTES)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_endian_bytes(buffer: &[u8], endianess: Endianess) -> i64
|
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() >= 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)
|
|
|
|
}
|
2016-01-02 14:54:03 -05:00
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn determine_byte_size(&self) -> usize
|
|
|
|
{
|
|
|
|
i64::BYTES
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Transmutable for isize
|
|
|
|
{
|
|
|
|
fn as_bytes(&self) -> Vec<u8>
|
2016-12-21 04:22:31 -05:00
|
|
|
{
|
|
|
|
// Unpack the bits that make up this value and
|
|
|
|
// return a buffer of the bytes.
|
2016-12-22 13:47:56 -05:00
|
|
|
unpack_bits!(*self, isize::BYTES)
|
2016-12-21 04:22:31 -05:00
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn as_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
2016-01-05 17:46:31 -05:00
|
|
|
{
|
2016-04-14 13:33:07 -04:00
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
|
|
|
// Create the Vector to hold the bytes
|
|
|
|
// from this type.
|
2016-12-22 13:47:56 -05:00
|
|
|
buffer = Vec::with_capacity(isize::BYTES);
|
2016-01-05 17:46:31 -05:00
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
2016-12-22 13:47:56 -05:00
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, isize_to_bytes);
|
2016-04-14 13:33:07 -04:00
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
2016-01-02 14:54:03 -05:00
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8]) -> isize
|
|
|
|
{
|
|
|
|
// Convert the given bytes to this type and return it.
|
|
|
|
pack_bits!(buffer, isize, isize::BYTES)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_endian_bytes(buffer: &[u8], endianess: Endianess) -> isize
|
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() >= 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-12-22 13:47:56 -05:00
|
|
|
fn determine_byte_size(&self) -> usize
|
2016-12-21 04:22:31 -05:00
|
|
|
{
|
2016-12-22 13:47:56 -05:00
|
|
|
isize::BYTES
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Transmutable for f32
|
|
|
|
{
|
|
|
|
fn as_bytes(&self) -> Vec<u8>
|
|
|
|
{
|
|
|
|
let value: u32;
|
|
|
|
|
|
|
|
unsafe
|
|
|
|
{
|
|
|
|
value = ::std::mem::transmute::<f32, u32>(*self);
|
|
|
|
}
|
|
|
|
|
|
|
|
value.as_bytes()
|
2016-12-21 04:22:31 -05:00
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn as_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
2016-01-05 17:46:31 -05:00
|
|
|
{
|
2016-04-14 13:33:07 -04:00
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
|
|
|
// Create the Vector to hold the bytes
|
|
|
|
// from this type.
|
2016-12-22 13:47:56 -05:00
|
|
|
buffer = Vec::with_capacity(f32::BYTES);
|
2016-01-05 17:46:31 -05:00
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
2016-12-22 13:47:56 -05:00
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, f32_to_bytes);
|
2016-04-14 13:33:07 -04:00
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8]) -> f32
|
|
|
|
{
|
|
|
|
unsafe
|
|
|
|
{
|
|
|
|
::std::mem::transmute::<u32, f32>(u32::from_bytes(buffer))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_endian_bytes(buffer: &[u8], endianess: Endianess) -> f32
|
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() >= 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-12-22 13:47:56 -05:00
|
|
|
fn determine_byte_size(&self) -> usize
|
2016-12-21 04:22:31 -05:00
|
|
|
{
|
2016-12-22 13:47:56 -05:00
|
|
|
f32::BYTES
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Transmutable for f64
|
|
|
|
{
|
|
|
|
fn as_bytes(&self) -> Vec<u8>
|
|
|
|
{
|
|
|
|
let value: u64;
|
2016-12-21 04:22:31 -05:00
|
|
|
|
|
|
|
unsafe
|
|
|
|
{
|
2016-12-22 13:47:56 -05:00
|
|
|
value = ::std::mem::transmute::<f64, u64>(*self);
|
2016-12-21 04:22:31 -05:00
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
value.as_bytes()
|
2016-12-21 04:22:31 -05:00
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn as_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
2016-01-05 17:46:31 -05:00
|
|
|
{
|
2016-04-14 13:33:07 -04:00
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
|
|
|
// Create the Vector to hold the bytes
|
|
|
|
// from this type.
|
2016-12-22 13:47:56 -05:00
|
|
|
buffer = Vec::with_capacity(f64::BYTES);
|
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.
|
2016-12-22 13:47:56 -05:00
|
|
|
handle_endianess_to_bytes!(buffer, self, endianess, f64_to_bytes);
|
2016-04-14 13:33:07 -04:00
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
2016-01-05 17:46:31 -05:00
|
|
|
}
|
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8]) -> f64
|
|
|
|
{
|
|
|
|
unsafe
|
|
|
|
{
|
|
|
|
::std::mem::transmute::<u64, f64>(u64::from_bytes(buffer))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_endian_bytes(buffer: &[u8], endianess: Endianess) -> f64
|
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() >= 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-04-14 18:08:50 -04:00
|
|
|
|
|
|
|
fn determine_byte_size(&self) -> usize
|
|
|
|
{
|
|
|
|
f64::BYTES
|
|
|
|
}
|
2016-01-02 14:54:03 -05:00
|
|
|
}
|
2016-01-05 23:42:08 -05:00
|
|
|
|
2016-04-13 23:57:40 -04:00
|
|
|
impl Transmutable for String
|
|
|
|
{
|
2016-12-22 13:47:56 -05:00
|
|
|
fn as_bytes(&self) -> Vec<u8>
|
2016-12-21 04:22:31 -05:00
|
|
|
{
|
|
|
|
let bytes: &[u8];
|
|
|
|
let byte_count: u64;
|
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
|
|
|
// Turn the string into a byte array.
|
|
|
|
bytes = self.as_bytes();
|
|
|
|
|
|
|
|
// Determine how many bytes will be written
|
|
|
|
// for this string.
|
|
|
|
byte_count = bytes.len() as u64;
|
|
|
|
|
|
|
|
// Make sure the buffer has enough space for this string.
|
|
|
|
buffer = Vec::with_capacity(bytes.len() + u64::BYTES);
|
|
|
|
|
|
|
|
// Add the count to the buffer.
|
2016-12-22 13:47:56 -05:00
|
|
|
buffer.append(&mut byte_count.as_bytes());
|
2016-12-21 04:22:31 -05:00
|
|
|
|
|
|
|
// Add each byte of the string to the buffer.
|
|
|
|
buffer.extend_from_slice(bytes);
|
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn as_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
2016-04-13 23:57:40 -04:00
|
|
|
{
|
|
|
|
let temp: String;
|
2016-04-14 13:33:07 -04:00
|
|
|
let mut buffer: Vec<u8>;
|
2016-04-13 23:57:40 -04:00
|
|
|
|
2016-04-14 13:33:07 -04:00
|
|
|
// Create the Vector to hold the bytes
|
|
|
|
// from this type.
|
|
|
|
buffer = Vec::with_capacity(get_byte_size_of_string(self));
|
2016-04-13 23:57:40 -04:00
|
|
|
|
|
|
|
// Clone the string so that we can use its data.
|
|
|
|
// We have to do this because Strings don't implement Copy.
|
|
|
|
temp = self.clone();
|
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
// We are not using the macro because *String is a str.
|
|
|
|
match endianess
|
|
|
|
{
|
2016-04-14 15:08:31 -04:00
|
|
|
Endianess::Big =>
|
2016-04-13 23:57:40 -04:00
|
|
|
{
|
2016-04-14 13:33:07 -04:00
|
|
|
buffer.append(&mut BigEndian::string_to_bytes(temp));
|
2016-04-13 23:57:40 -04:00
|
|
|
}
|
|
|
|
|
2016-04-14 15:08:31 -04:00
|
|
|
Endianess::Little =>
|
2016-04-13 23:57:40 -04:00
|
|
|
{
|
2016-04-14 13:33:07 -04:00
|
|
|
buffer.append(&mut LittleEndian::string_to_bytes(temp));
|
2016-04-13 23:57:40 -04:00
|
|
|
}
|
|
|
|
|
2016-04-14 15:08:31 -04:00
|
|
|
Endianess::Platform =>
|
2016-04-13 23:57:40 -04:00
|
|
|
{
|
2016-04-14 13:33:07 -04:00
|
|
|
buffer.append(&mut PlatformEndian::string_to_bytes(temp));
|
2016-04-13 23:57:40 -04:00
|
|
|
}
|
2016-06-17 04:34:36 -04:00
|
|
|
|
|
|
|
Endianess::Network =>
|
|
|
|
{
|
|
|
|
buffer.append(&mut NetworkEndian::string_to_bytes(temp));
|
|
|
|
}
|
2016-04-13 23:57:40 -04:00
|
|
|
}
|
2016-04-14 13:33:07 -04:00
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
2016-04-13 23:57:40 -04:00
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn from_bytes(buffer: &[u8]) -> String
|
|
|
|
{
|
|
|
|
let byte_count: u64;
|
|
|
|
let new_string: String;
|
|
|
|
|
|
|
|
// A string array should have atleast a u64 size byte count.
|
|
|
|
assert!(buffer.len() >= u64::BYTES);
|
|
|
|
|
|
|
|
// Strings start with the size of bytes to read as
|
|
|
|
// a u64. So read that in and then we know how many
|
|
|
|
// bytes make up the string.
|
|
|
|
byte_count = u64::from_bytes(&buffer[0..u64::BYTES]);
|
|
|
|
|
|
|
|
if byte_count > 0
|
|
|
|
{
|
|
|
|
match String::from_utf8(buffer[u64::BYTES..(buffer.len()-1)].to_vec())
|
|
|
|
{
|
|
|
|
Ok(string) =>
|
|
|
|
{
|
|
|
|
new_string = string;
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(error) =>
|
|
|
|
{
|
|
|
|
error!("{}", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
new_string = String::new();
|
|
|
|
}
|
|
|
|
|
|
|
|
new_string
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_endian_bytes(buffer: &[u8], endianess: Endianess) -> String
|
|
|
|
{
|
|
|
|
// Convert the given bytes to this type and return it.
|
|
|
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_string)
|
|
|
|
}
|
|
|
|
|
2016-04-14 18:08:50 -04:00
|
|
|
fn determine_byte_size(&self) -> usize
|
2016-04-13 23:57:40 -04:00
|
|
|
{
|
2016-04-14 18:08:50 -04:00
|
|
|
get_byte_size_of_string(self)
|
2016-04-13 23:57:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 13:23:56 -04:00
|
|
|
#[cfg(feature="convert_sigils")]
|
2016-01-06 23:37:18 -05:00
|
|
|
impl<T> Transmutable for Vector2<T> where T: Number + ByteSized + Transmutable
|
|
|
|
{
|
2016-12-22 13:47:56 -05:00
|
|
|
fn as_bytes(&self) -> Vec<u8>
|
|
|
|
{
|
|
|
|
let byte_size: usize;
|
|
|
|
let num_bytes: usize;
|
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
buffer = Vec::with_capacity(num_bytes);
|
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
buffer.append(&mut self.x.as_bytes());
|
|
|
|
buffer.append(&mut self.y.as_bytes());
|
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
|
|
|
{
|
|
|
|
let byte_size: usize;
|
|
|
|
let num_bytes: usize;
|
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
buffer = Vec::with_capacity(num_bytes);
|
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
buffer.append(&mut self.x.as_endian_bytes(endianess));
|
|
|
|
buffer.append(&mut self.y.as_endian_bytes(endianess));
|
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
|
|
|
}
|
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8]) -> Vector2<T>
|
2016-01-06 23:37:18 -05:00
|
|
|
{
|
|
|
|
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.
|
2016-12-21 04:22:31 -05:00
|
|
|
vec.x = T::from_bytes(&buffer[0..byte_size]);
|
|
|
|
vec.y = T::from_bytes(&buffer[byte_size..num_bytes]);
|
2016-01-06 23:37:18 -05:00
|
|
|
vec
|
|
|
|
}
|
2016-01-07 05:10:50 -05:00
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_endian_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_endian_bytes(&buffer[0..byte_size],
|
|
|
|
endianess);
|
|
|
|
vec.y = T::from_endian_bytes(&buffer[byte_size..num_bytes],
|
|
|
|
endianess);
|
|
|
|
vec
|
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn determine_byte_size(&self) -> usize
|
|
|
|
{
|
|
|
|
T::get_byte_size() * self.get_size() as usize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature="convert_sigils")]
|
|
|
|
impl<T> Transmutable for Vector3<T> where T: Number + ByteSized + Transmutable
|
|
|
|
{
|
|
|
|
fn as_bytes(&self) -> Vec<u8>
|
2016-01-07 05:10:50 -05:00
|
|
|
{
|
|
|
|
let byte_size: usize;
|
|
|
|
let num_bytes: usize;
|
2016-04-14 13:33:07 -04:00
|
|
|
let mut buffer: Vec<u8>;
|
2016-01-07 05:10:50 -05:00
|
|
|
|
|
|
|
// Determine the number of bytes requires to
|
2016-04-14 18:08:50 -04:00
|
|
|
// represent a Vector2.
|
2016-01-07 05:10:50 -05:00
|
|
|
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.
|
2016-04-14 13:33:07 -04:00
|
|
|
buffer = Vec::with_capacity(num_bytes);
|
2016-01-07 05:10:50 -05:00
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
2016-12-22 13:47:56 -05:00
|
|
|
buffer.append(&mut self.x.as_bytes());
|
|
|
|
buffer.append(&mut self.y.as_bytes());
|
|
|
|
buffer.append(&mut self.z.as_bytes());
|
2016-12-21 04:22:31 -05:00
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn as_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
2016-12-21 04:22:31 -05:00
|
|
|
{
|
|
|
|
let byte_size: usize;
|
|
|
|
let num_bytes: usize;
|
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
|
|
|
// Determine the number of bytes requires to
|
2016-12-22 13:47:56 -05:00
|
|
|
// represent a Vector3.
|
2016-12-21 04:22:31 -05:00
|
|
|
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.
|
|
|
|
buffer = Vec::with_capacity(num_bytes);
|
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
2016-12-22 13:47:56 -05:00
|
|
|
buffer.append(&mut self.x.as_endian_bytes(endianess));
|
|
|
|
buffer.append(&mut self.y.as_endian_bytes(endianess));
|
|
|
|
buffer.append(&mut self.z.as_endian_bytes(endianess));
|
2016-04-14 13:33:07 -04:00
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
2016-01-07 05:10:50 -05:00
|
|
|
}
|
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8]) -> 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]);
|
|
|
|
vec.y = T::from_bytes(&buffer[byte_size..(byte_size*2)]);
|
|
|
|
vec.z = T::from_bytes(&buffer[(byte_size*2)..num_bytes]);
|
|
|
|
vec
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_endian_bytes(buffer: &[u8], endianess: Endianess) -> Vector3<T>
|
2016-01-07 05:10:50 -05:00
|
|
|
{
|
|
|
|
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.
|
2016-12-21 04:22:31 -05:00
|
|
|
vec.x = T::from_endian_bytes(&buffer[0..byte_size],
|
|
|
|
endianess);
|
|
|
|
vec.y = T::from_endian_bytes(&buffer[byte_size..(byte_size*2)],
|
|
|
|
endianess);
|
|
|
|
vec.z = T::from_endian_bytes(&buffer[(byte_size*2)..num_bytes],
|
|
|
|
endianess);
|
2016-01-07 05:10:50 -05:00
|
|
|
vec
|
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn determine_byte_size(&self) -> usize
|
2016-12-21 04:22:31 -05:00
|
|
|
{
|
2016-12-22 13:47:56 -05:00
|
|
|
T::get_byte_size() * self.get_size() as usize
|
2016-12-21 04:22:31 -05:00
|
|
|
}
|
2016-12-22 13:47:56 -05:00
|
|
|
}
|
2016-12-21 04:22:31 -05:00
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
#[cfg(feature="convert_sigils")]
|
|
|
|
impl<T> Transmutable for Vector4<T> where T: Number + ByteSized + Transmutable
|
|
|
|
{
|
|
|
|
fn as_bytes(&self) -> Vec<u8>
|
2016-01-07 05:10:50 -05:00
|
|
|
{
|
|
|
|
let byte_size: usize;
|
|
|
|
let num_bytes: usize;
|
2016-04-14 13:33:07 -04:00
|
|
|
let mut buffer: Vec<u8>;
|
2016-01-07 05:10:50 -05:00
|
|
|
|
|
|
|
// Determine the number of bytes requires to
|
2016-12-22 13:47:56 -05:00
|
|
|
// represent a Vector4.
|
2016-01-07 05:10:50 -05:00
|
|
|
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.
|
2016-04-14 13:33:07 -04:00
|
|
|
buffer = Vec::with_capacity(num_bytes);
|
2016-01-07 05:10:50 -05:00
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
2016-12-22 13:47:56 -05:00
|
|
|
buffer.append(&mut self.x.as_bytes());
|
|
|
|
buffer.append(&mut self.y.as_bytes());
|
|
|
|
buffer.append(&mut self.z.as_bytes());
|
|
|
|
buffer.append(&mut self.w.as_bytes());
|
2016-04-14 13:33:07 -04:00
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
2016-01-07 05:10:50 -05:00
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn as_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
2016-04-14 18:08:50 -04:00
|
|
|
{
|
2016-12-22 13:47:56 -05:00
|
|
|
let byte_size: usize;
|
|
|
|
let num_bytes: usize;
|
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
buffer = Vec::with_capacity(num_bytes);
|
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
|
|
|
buffer.append(&mut self.x.as_endian_bytes(endianess));
|
|
|
|
buffer.append(&mut self.y.as_endian_bytes(endianess));
|
|
|
|
buffer.append(&mut self.z.as_endian_bytes(endianess));
|
|
|
|
buffer.append(&mut self.w.as_endian_bytes(endianess));
|
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
2016-04-14 18:08:50 -04:00
|
|
|
}
|
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8]) -> 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]);
|
|
|
|
vec.y = T::from_bytes(&buffer[byte_size..(byte_size*2)]);
|
|
|
|
vec.z = T::from_bytes(&buffer[(byte_size*2)..(byte_size*3)]);
|
|
|
|
vec.w = T::from_bytes(&buffer[(byte_size*3)..num_bytes]);
|
|
|
|
vec
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_endian_bytes(buffer: &[u8], endianess: Endianess) -> Vector4<T>
|
2016-01-07 05:10:50 -05:00
|
|
|
{
|
|
|
|
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.
|
2016-12-21 04:22:31 -05:00
|
|
|
vec.x = T::from_endian_bytes(&buffer[0..byte_size],
|
|
|
|
endianess);
|
|
|
|
vec.y = T::from_endian_bytes(&buffer[byte_size..(byte_size*2)],
|
|
|
|
endianess);
|
|
|
|
vec.z = T::from_endian_bytes(&buffer[(byte_size*2)..(byte_size*3)],
|
|
|
|
endianess);
|
|
|
|
vec.w = T::from_endian_bytes(&buffer[(byte_size*3)..num_bytes],
|
|
|
|
endianess);
|
2016-01-07 05:10:50 -05:00
|
|
|
vec
|
|
|
|
}
|
2016-01-07 05:51:53 -05:00
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn determine_byte_size(&self) -> usize
|
|
|
|
{
|
|
|
|
T::get_byte_size() * self.get_size() as usize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature="convert_sigils")]
|
|
|
|
impl<T> Transmutable for Quaternion<T>
|
|
|
|
where T: Real + ByteSized + Transmutable
|
|
|
|
{
|
|
|
|
fn as_bytes(&self) -> Vec<u8>
|
2016-01-07 05:51:53 -05:00
|
|
|
{
|
|
|
|
let byte_size: usize;
|
|
|
|
let num_bytes: usize;
|
2016-04-14 13:33:07 -04:00
|
|
|
let mut buffer: Vec<u8>;
|
2016-01-07 05:51:53 -05:00
|
|
|
|
|
|
|
// Determine the number of bytes requires to
|
2016-12-22 13:47:56 -05:00
|
|
|
// represent a Quaternion.
|
2016-01-07 05:51:53 -05:00
|
|
|
byte_size = T::get_byte_size();
|
2016-12-22 13:47:56 -05:00
|
|
|
num_bytes = byte_size * 4usize;
|
2016-01-07 05:51:53 -05:00
|
|
|
|
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
2016-04-14 13:33:07 -04:00
|
|
|
buffer = Vec::with_capacity(num_bytes);
|
2016-01-07 05:51:53 -05:00
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
2016-12-22 13:47:56 -05:00
|
|
|
buffer.append(&mut self.scalar.as_bytes());
|
|
|
|
buffer.append(&mut self.vector.as_bytes());
|
2016-12-21 04:22:31 -05:00
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
|
|
|
}
|
|
|
|
|
2016-12-22 13:47:56 -05:00
|
|
|
fn as_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
2016-12-21 04:22:31 -05:00
|
|
|
{
|
|
|
|
let byte_size: usize;
|
|
|
|
let num_bytes: usize;
|
|
|
|
let mut buffer: Vec<u8>;
|
|
|
|
|
|
|
|
// Determine the number of bytes requires to
|
2016-12-22 13:47:56 -05:00
|
|
|
// represent a Quaternion.
|
2016-12-21 04:22:31 -05:00
|
|
|
byte_size = T::get_byte_size();
|
2016-12-22 13:47:56 -05:00
|
|
|
num_bytes = byte_size * 4usize;
|
2016-12-21 04:22:31 -05:00
|
|
|
|
|
|
|
// Make sure that there is enough space to store
|
|
|
|
// the bytes from this type.
|
|
|
|
buffer = Vec::with_capacity(num_bytes);
|
|
|
|
|
|
|
|
// Convert this to bytes and add it to the buffer.
|
2016-12-22 13:47:56 -05:00
|
|
|
buffer.append(&mut self.scalar.as_endian_bytes(endianess));
|
|
|
|
buffer.append(&mut self.vector.as_endian_bytes(endianess));
|
2016-04-14 13:33:07 -04:00
|
|
|
|
|
|
|
// Return the byte buffer.
|
|
|
|
buffer
|
2016-01-07 05:51:53 -05:00
|
|
|
}
|
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_bytes(buffer: &[u8]) -> Quaternion<T>
|
2016-01-07 05:51:53 -05:00
|
|
|
{
|
|
|
|
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.
|
2016-12-21 04:22:31 -05:00
|
|
|
quat.scalar = T::from_bytes(&buffer[0..byte_size]);
|
2016-01-07 05:51:53 -05:00
|
|
|
quat.vector =
|
2016-12-21 04:22:31 -05:00
|
|
|
Vector3::<T>::from_bytes(&buffer[byte_size..num_bytes]);
|
2016-01-07 05:51:53 -05:00
|
|
|
quat
|
|
|
|
}
|
2016-04-14 18:08:50 -04:00
|
|
|
|
2016-12-21 04:22:31 -05:00
|
|
|
fn from_endian_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_endian_bytes(&buffer[0..byte_size], endianess);
|
|
|
|
quat.vector =
|
|
|
|
Vector3::<T>::from_endian_bytes(&buffer[byte_size..num_bytes],
|
|
|
|
endianess);
|
|
|
|
quat
|
|
|
|
}
|
|
|
|
|
2016-04-14 18:08:50 -04:00
|
|
|
fn determine_byte_size(&self) -> usize
|
|
|
|
{
|
|
|
|
T::get_byte_size() * 4usize
|
|
|
|
}
|
2016-01-07 05:51:53 -05:00
|
|
|
}
|