Added the ability to convert the number directly to an array.
Sometimes it is better to not deal with endianess.
This commit is contained in:
parent
47e1c707eb
commit
04c94db9f5
@ -505,8 +505,8 @@ pub fn network_to_platform_order<T>(val: T) -> T
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Convert the value from Big endian to Little endian.
|
// Convert the value from Big endian to Little endian.
|
||||||
buffer = val.to_bytes(Endianess::Network);
|
buffer = val.to_endian_bytes(Endianess::Network);
|
||||||
T::from_bytes(buffer.as_slice(), Endianess::Platform)
|
T::from_endian_bytes(buffer.as_slice(), Endianess::Platform)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -526,8 +526,8 @@ pub fn platform_to_network_order<T>(val: T) -> T
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Convert the value from Little endian to Big endian.
|
// Convert the value from Little endian to Big endian.
|
||||||
buffer = val.to_bytes(Endianess::Platform);
|
buffer = val.to_endian_bytes(Endianess::Platform);
|
||||||
T::from_bytes(buffer.as_slice(), Endianess::Network)
|
T::from_endian_bytes(buffer.as_slice(), Endianess::Network)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,10 +18,16 @@ use ::endian::Endianess;
|
|||||||
pub trait Transmutable
|
pub trait Transmutable
|
||||||
{
|
{
|
||||||
/// Transmute an array of bytes to this type.
|
/// Transmute an array of bytes to this type.
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Self;
|
fn from_bytes(buffer: &[u8]) -> Self;
|
||||||
|
|
||||||
|
/// Transmute an array of bytes to this type.
|
||||||
|
fn from_endian_bytes(buffer: &[u8], endianess: Endianess) -> Self;
|
||||||
|
|
||||||
/// Transmute this type to an array of bytes.
|
/// Transmute this type to an array of bytes.
|
||||||
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>;
|
fn to_bytes(&self) -> Vec<u8>;
|
||||||
|
|
||||||
|
/// Transmute this type to an array of bytes.
|
||||||
|
fn to_endian_bytes(&self, endianess: Endianess) -> Vec<u8>;
|
||||||
|
|
||||||
/// Get the current size of this Transmutable in bytes.
|
/// Get the current size of this Transmutable in bytes.
|
||||||
fn determine_byte_size(&self) -> usize;
|
fn determine_byte_size(&self) -> usize;
|
||||||
@ -95,12 +101,66 @@ macro_rules! handle_endianess_from_bytes
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
impl Transmutable for u8
|
impl Transmutable for u8
|
||||||
{
|
{
|
||||||
|
fn from_bytes(buffer: &[u8]) -> u8
|
||||||
|
{
|
||||||
|
// Convert the given bytes to this type and return it.
|
||||||
|
pack_bits!(buffer, u8, u8::BYTES)
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> u8
|
fn from_endian_bytes(buffer: &[u8], endianess: Endianess) -> u8
|
||||||
{
|
{
|
||||||
// Make sure that there is enough data to read
|
// Make sure that there is enough data to read
|
||||||
// the bytes for this type.
|
// the bytes for this type.
|
||||||
@ -111,8 +171,15 @@ impl Transmutable for u8
|
|||||||
buffer[0]
|
buffer[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn to_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)]
|
#[allow(unused_variables)]
|
||||||
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
fn to_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let mut buffer: Vec<u8>;
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
@ -135,7 +202,13 @@ impl Transmutable for u8
|
|||||||
|
|
||||||
impl Transmutable for u16
|
impl Transmutable for u16
|
||||||
{
|
{
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> u16
|
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
|
||||||
{
|
{
|
||||||
// Make sure that there is enough data to read
|
// Make sure that there is enough data to read
|
||||||
// the bytes for this type.
|
// the bytes for this type.
|
||||||
@ -145,7 +218,14 @@ impl Transmutable for u16
|
|||||||
handle_endianess_from_bytes!(buffer, endianess, bytes_to_u16)
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_u16)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
fn to_bytes(&self) -> Vec<u8>
|
||||||
|
{
|
||||||
|
// Unpack the bits that make up this value and
|
||||||
|
// return a buffer of the bytes.
|
||||||
|
unpack_bits!(*self, u16::BYTES)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let mut buffer: Vec<u8>;
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
@ -168,7 +248,13 @@ impl Transmutable for u16
|
|||||||
|
|
||||||
impl Transmutable for u32
|
impl Transmutable for u32
|
||||||
{
|
{
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> u32
|
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
|
||||||
{
|
{
|
||||||
// Make sure that there is enough data to read
|
// Make sure that there is enough data to read
|
||||||
// the bytes for this type.
|
// the bytes for this type.
|
||||||
@ -178,7 +264,14 @@ impl Transmutable for u32
|
|||||||
handle_endianess_from_bytes!(buffer, endianess, bytes_to_u32)
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_u32)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
fn to_bytes(&self) -> Vec<u8>
|
||||||
|
{
|
||||||
|
// Unpack the bits that make up this value and
|
||||||
|
// return a buffer of the bytes.
|
||||||
|
unpack_bits!(*self, u32::BYTES)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let mut buffer: Vec<u8>;
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
@ -201,7 +294,13 @@ impl Transmutable for u32
|
|||||||
|
|
||||||
impl Transmutable for u64
|
impl Transmutable for u64
|
||||||
{
|
{
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> u64
|
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
|
||||||
{
|
{
|
||||||
// Make sure that there is enough data to read
|
// Make sure that there is enough data to read
|
||||||
// the bytes for this type.
|
// the bytes for this type.
|
||||||
@ -211,7 +310,14 @@ impl Transmutable for u64
|
|||||||
handle_endianess_from_bytes!(buffer, endianess, bytes_to_u64)
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_u64)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
fn to_bytes(&self) -> Vec<u8>
|
||||||
|
{
|
||||||
|
// Unpack the bits that make up this value and
|
||||||
|
// return a buffer of the bytes.
|
||||||
|
unpack_bits!(*self, u64::BYTES)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let mut buffer: Vec<u8>;
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
@ -234,7 +340,13 @@ impl Transmutable for u64
|
|||||||
|
|
||||||
impl Transmutable for usize
|
impl Transmutable for usize
|
||||||
{
|
{
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> usize
|
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
|
||||||
{
|
{
|
||||||
// Make sure that there is enough data to read
|
// Make sure that there is enough data to read
|
||||||
// the bytes for this type.
|
// the bytes for this type.
|
||||||
@ -244,7 +356,14 @@ impl Transmutable for usize
|
|||||||
handle_endianess_from_bytes!(buffer, endianess, bytes_to_usize)
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_usize)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
fn to_bytes(&self) -> Vec<u8>
|
||||||
|
{
|
||||||
|
// Unpack the bits that make up this value and
|
||||||
|
// return a buffer of the bytes.
|
||||||
|
unpack_bits!(*self, usize::BYTES)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let mut buffer: Vec<u8>;
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
@ -267,8 +386,14 @@ impl Transmutable for usize
|
|||||||
|
|
||||||
impl Transmutable for i8
|
impl Transmutable for i8
|
||||||
{
|
{
|
||||||
|
fn from_bytes(buffer: &[u8]) -> i8
|
||||||
|
{
|
||||||
|
// Convert the given bytes to this type and return it.
|
||||||
|
pack_bits!(buffer, i8, i8::BYTES)
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> i8
|
fn from_endian_bytes(buffer: &[u8], endianess: Endianess) -> i8
|
||||||
{
|
{
|
||||||
// Make sure that there is enough data to read
|
// Make sure that there is enough data to read
|
||||||
// the bytes for this type.
|
// the bytes for this type.
|
||||||
@ -279,8 +404,15 @@ impl Transmutable for i8
|
|||||||
buffer[0] as i8
|
buffer[0] as i8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn to_bytes(&self) -> Vec<u8>
|
||||||
|
{
|
||||||
|
// Unpack the bits that make up this value and
|
||||||
|
// return a buffer of the bytes.
|
||||||
|
unpack_bits!(*self, i8::BYTES)
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
fn to_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let mut buffer: Vec<u8>;
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
@ -303,7 +435,13 @@ impl Transmutable for i8
|
|||||||
|
|
||||||
impl Transmutable for i16
|
impl Transmutable for i16
|
||||||
{
|
{
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> i16
|
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
|
||||||
{
|
{
|
||||||
// Make sure that there is enough data to read
|
// Make sure that there is enough data to read
|
||||||
// the bytes for this type.
|
// the bytes for this type.
|
||||||
@ -313,7 +451,14 @@ impl Transmutable for i16
|
|||||||
handle_endianess_from_bytes!(buffer, endianess, bytes_to_i16)
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_i16)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
fn to_bytes(&self) -> Vec<u8>
|
||||||
|
{
|
||||||
|
// Unpack the bits that make up this value and
|
||||||
|
// return a buffer of the bytes.
|
||||||
|
unpack_bits!(*self, i16::BYTES)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let mut buffer: Vec<u8>;
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
@ -336,7 +481,13 @@ impl Transmutable for i16
|
|||||||
|
|
||||||
impl Transmutable for i32
|
impl Transmutable for i32
|
||||||
{
|
{
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> i32
|
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
|
||||||
{
|
{
|
||||||
// Make sure that there is enough data to read
|
// Make sure that there is enough data to read
|
||||||
// the bytes for this type.
|
// the bytes for this type.
|
||||||
@ -346,7 +497,14 @@ impl Transmutable for i32
|
|||||||
handle_endianess_from_bytes!(buffer, endianess, bytes_to_i32)
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_i32)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
fn to_bytes(&self) -> Vec<u8>
|
||||||
|
{
|
||||||
|
// Unpack the bits that make up this value and
|
||||||
|
// return a buffer of the bytes.
|
||||||
|
unpack_bits!(*self, i32::BYTES)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let mut buffer: Vec<u8>;
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
@ -369,7 +527,13 @@ impl Transmutable for i32
|
|||||||
|
|
||||||
impl Transmutable for i64
|
impl Transmutable for i64
|
||||||
{
|
{
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> i64
|
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
|
||||||
{
|
{
|
||||||
// Make sure that there is enough data to read
|
// Make sure that there is enough data to read
|
||||||
// the bytes for this type.
|
// the bytes for this type.
|
||||||
@ -379,7 +543,14 @@ impl Transmutable for i64
|
|||||||
handle_endianess_from_bytes!(buffer, endianess, bytes_to_i64)
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_i64)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
fn to_bytes(&self) -> Vec<u8>
|
||||||
|
{
|
||||||
|
// Unpack the bits that make up this value and
|
||||||
|
// return a buffer of the bytes.
|
||||||
|
unpack_bits!(*self, i64::BYTES)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let mut buffer: Vec<u8>;
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
@ -402,7 +573,13 @@ impl Transmutable for i64
|
|||||||
|
|
||||||
impl Transmutable for isize
|
impl Transmutable for isize
|
||||||
{
|
{
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> isize
|
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
|
||||||
{
|
{
|
||||||
// Make sure that there is enough data to read
|
// Make sure that there is enough data to read
|
||||||
// the bytes for this type.
|
// the bytes for this type.
|
||||||
@ -412,7 +589,14 @@ impl Transmutable for isize
|
|||||||
handle_endianess_from_bytes!(buffer, endianess, bytes_to_isize)
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_isize)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
fn to_bytes(&self) -> Vec<u8>
|
||||||
|
{
|
||||||
|
// Unpack the bits that make up this value and
|
||||||
|
// return a buffer of the bytes.
|
||||||
|
unpack_bits!(*self, isize::BYTES)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let mut buffer: Vec<u8>;
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
@ -435,7 +619,15 @@ impl Transmutable for isize
|
|||||||
|
|
||||||
impl Transmutable for f32
|
impl Transmutable for f32
|
||||||
{
|
{
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> f32
|
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
|
||||||
{
|
{
|
||||||
// Make sure that there is enough data to read
|
// Make sure that there is enough data to read
|
||||||
// the bytes for this type.
|
// the bytes for this type.
|
||||||
@ -445,7 +637,19 @@ impl Transmutable for f32
|
|||||||
handle_endianess_from_bytes!(buffer, endianess, bytes_to_f32)
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_f32)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
fn to_bytes(&self) -> Vec<u8>
|
||||||
|
{
|
||||||
|
let value: u32;
|
||||||
|
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
value = ::std::mem::transmute::<f32, u32>(*self);
|
||||||
|
}
|
||||||
|
|
||||||
|
value.to_bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let mut buffer: Vec<u8>;
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
@ -468,7 +672,15 @@ impl Transmutable for f32
|
|||||||
|
|
||||||
impl Transmutable for f64
|
impl Transmutable for f64
|
||||||
{
|
{
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> f64
|
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
|
||||||
{
|
{
|
||||||
// Make sure that there is enough data to read
|
// Make sure that there is enough data to read
|
||||||
// the bytes for this type.
|
// the bytes for this type.
|
||||||
@ -478,7 +690,19 @@ impl Transmutable for f64
|
|||||||
handle_endianess_from_bytes!(buffer, endianess, bytes_to_f64)
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_f64)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
fn to_bytes(&self) -> Vec<u8>
|
||||||
|
{
|
||||||
|
let value: u64;
|
||||||
|
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
value = ::std::mem::transmute::<f64, u64>(*self);
|
||||||
|
}
|
||||||
|
|
||||||
|
value.to_bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let mut buffer: Vec<u8>;
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
@ -501,13 +725,75 @@ impl Transmutable for f64
|
|||||||
|
|
||||||
impl Transmutable for String
|
impl Transmutable for String
|
||||||
{
|
{
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> String
|
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.
|
// Convert the given bytes to this type and return it.
|
||||||
handle_endianess_from_bytes!(buffer, endianess, bytes_to_string)
|
handle_endianess_from_bytes!(buffer, endianess, bytes_to_string)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
fn to_bytes(&self) -> Vec<u8>
|
||||||
|
{
|
||||||
|
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.
|
||||||
|
buffer.append(&mut byte_count.to_bytes());
|
||||||
|
|
||||||
|
// Add each byte of the string to the buffer.
|
||||||
|
buffer.extend_from_slice(bytes);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let temp: String;
|
let temp: String;
|
||||||
let mut buffer: Vec<u8>;
|
let mut buffer: Vec<u8>;
|
||||||
@ -558,7 +844,7 @@ impl Transmutable for String
|
|||||||
#[cfg(feature="convert_sigils")]
|
#[cfg(feature="convert_sigils")]
|
||||||
impl<T> Transmutable for Vector2<T> where T: Number + ByteSized + Transmutable
|
impl<T> Transmutable for Vector2<T> where T: Number + ByteSized + Transmutable
|
||||||
{
|
{
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Vector2<T>
|
fn from_bytes(buffer: &[u8]) -> Vector2<T>
|
||||||
{
|
{
|
||||||
let byte_size: usize;
|
let byte_size: usize;
|
||||||
let num_bytes: usize;
|
let num_bytes: usize;
|
||||||
@ -575,12 +861,36 @@ impl<T> Transmutable for Vector2<T> where T: Number + ByteSized + Transmutable
|
|||||||
assert!(buffer.len() >= num_bytes);
|
assert!(buffer.len() >= num_bytes);
|
||||||
|
|
||||||
// Convert the given bytes to this type and return it.
|
// Convert the given bytes to this type and return it.
|
||||||
vec.x = T::from_bytes(&buffer[0..byte_size], endianess);
|
vec.x = T::from_bytes(&buffer[0..byte_size]);
|
||||||
vec.y = T::from_bytes(&buffer[byte_size..num_bytes], endianess);
|
vec.y = T::from_bytes(&buffer[byte_size..num_bytes]);
|
||||||
vec
|
vec
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_bytes(&self) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let byte_size: usize;
|
let byte_size: usize;
|
||||||
let num_bytes: usize;
|
let num_bytes: usize;
|
||||||
@ -596,8 +906,31 @@ impl<T> Transmutable for Vector2<T> where T: Number + ByteSized + Transmutable
|
|||||||
buffer = Vec::with_capacity(num_bytes);
|
buffer = Vec::with_capacity(num_bytes);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
buffer.append(&mut self.x.to_bytes(endianess));
|
buffer.append(&mut self.x.to_bytes());
|
||||||
buffer.append(&mut self.y.to_bytes(endianess));
|
buffer.append(&mut self.y.to_bytes());
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_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.to_endian_bytes(endianess));
|
||||||
|
buffer.append(&mut self.y.to_endian_bytes(endianess));
|
||||||
|
|
||||||
// Return the byte buffer.
|
// Return the byte buffer.
|
||||||
buffer
|
buffer
|
||||||
@ -612,7 +945,7 @@ impl<T> Transmutable for Vector2<T> where T: Number + ByteSized + Transmutable
|
|||||||
#[cfg(feature="convert_sigils")]
|
#[cfg(feature="convert_sigils")]
|
||||||
impl<T> Transmutable for Vector3<T> where T: Number + ByteSized + Transmutable
|
impl<T> Transmutable for Vector3<T> where T: Number + ByteSized + Transmutable
|
||||||
{
|
{
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Vector3<T>
|
fn from_bytes(buffer: &[u8]) -> Vector3<T>
|
||||||
{
|
{
|
||||||
let byte_size: usize;
|
let byte_size: usize;
|
||||||
let num_bytes: usize;
|
let num_bytes: usize;
|
||||||
@ -629,13 +962,44 @@ impl<T> Transmutable for Vector3<T> where T: Number + ByteSized + Transmutable
|
|||||||
assert!(buffer.len() >= num_bytes);
|
assert!(buffer.len() >= num_bytes);
|
||||||
|
|
||||||
// Convert the given bytes to this type and return it.
|
// Convert the given bytes to this type and return it.
|
||||||
vec.x = T::from_bytes(&buffer[0..byte_size], endianess);
|
vec.x = T::from_bytes(&buffer[0..byte_size]);
|
||||||
vec.y = T::from_bytes(&buffer[byte_size..(byte_size*2)], endianess);
|
vec.y = T::from_bytes(&buffer[byte_size..(byte_size*2)]);
|
||||||
vec.z = T::from_bytes(&buffer[(byte_size*2)..num_bytes], endianess);
|
vec.z = T::from_bytes(&buffer[(byte_size*2)..num_bytes]);
|
||||||
vec
|
vec
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
fn from_endian_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_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);
|
||||||
|
vec
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_bytes(&self) -> Vec<u8>
|
||||||
|
{
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_endian_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let byte_size: usize;
|
let byte_size: usize;
|
||||||
let num_bytes: usize;
|
let num_bytes: usize;
|
||||||
@ -651,9 +1015,9 @@ impl<T> Transmutable for Vector3<T> where T: Number + ByteSized + Transmutable
|
|||||||
buffer = Vec::with_capacity(num_bytes);
|
buffer = Vec::with_capacity(num_bytes);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
buffer.append(&mut self.x.to_bytes(endianess));
|
buffer.append(&mut self.x.to_endian_bytes(endianess));
|
||||||
buffer.append(&mut self.y.to_bytes(endianess));
|
buffer.append(&mut self.y.to_endian_bytes(endianess));
|
||||||
buffer.append(&mut self.z.to_bytes(endianess));
|
buffer.append(&mut self.z.to_endian_bytes(endianess));
|
||||||
|
|
||||||
// Return the byte buffer.
|
// Return the byte buffer.
|
||||||
buffer
|
buffer
|
||||||
@ -668,7 +1032,7 @@ impl<T> Transmutable for Vector3<T> where T: Number + ByteSized + Transmutable
|
|||||||
#[cfg(feature="convert_sigils")]
|
#[cfg(feature="convert_sigils")]
|
||||||
impl<T> Transmutable for Vector4<T> where T: Number + ByteSized + Transmutable
|
impl<T> Transmutable for Vector4<T> where T: Number + ByteSized + Transmutable
|
||||||
{
|
{
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Vector4<T>
|
fn from_bytes(buffer: &[u8]) -> Vector4<T>
|
||||||
{
|
{
|
||||||
let byte_size: usize;
|
let byte_size: usize;
|
||||||
let num_bytes: usize;
|
let num_bytes: usize;
|
||||||
@ -685,14 +1049,42 @@ impl<T> Transmutable for Vector4<T> where T: Number + ByteSized + Transmutable
|
|||||||
assert!(buffer.len() >= num_bytes);
|
assert!(buffer.len() >= num_bytes);
|
||||||
|
|
||||||
// Convert the given bytes to this type and return it.
|
// Convert the given bytes to this type and return it.
|
||||||
vec.x = T::from_bytes(&buffer[0..byte_size], endianess);
|
vec.x = T::from_bytes(&buffer[0..byte_size]);
|
||||||
vec.y = T::from_bytes(&buffer[byte_size..(byte_size*2)], endianess);
|
vec.y = T::from_bytes(&buffer[byte_size..(byte_size*2)]);
|
||||||
vec.z = T::from_bytes(&buffer[(byte_size*2)..(byte_size*3)], endianess);
|
vec.z = T::from_bytes(&buffer[(byte_size*2)..(byte_size*3)]);
|
||||||
vec.w = T::from_bytes(&buffer[(byte_size*3)..num_bytes], endianess);
|
vec.w = T::from_bytes(&buffer[(byte_size*3)..num_bytes]);
|
||||||
vec
|
vec
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
fn from_endian_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_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);
|
||||||
|
vec
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_bytes(&self) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let byte_size: usize;
|
let byte_size: usize;
|
||||||
let num_bytes: usize;
|
let num_bytes: usize;
|
||||||
@ -708,10 +1100,35 @@ impl<T> Transmutable for Vector4<T> where T: Number + ByteSized + Transmutable
|
|||||||
buffer = Vec::with_capacity(num_bytes);
|
buffer = Vec::with_capacity(num_bytes);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
buffer.append(&mut self.x.to_bytes(endianess));
|
buffer.append(&mut self.x.to_bytes());
|
||||||
buffer.append(&mut self.y.to_bytes(endianess));
|
buffer.append(&mut self.y.to_bytes());
|
||||||
buffer.append(&mut self.z.to_bytes(endianess));
|
buffer.append(&mut self.z.to_bytes());
|
||||||
buffer.append(&mut self.w.to_bytes(endianess));
|
buffer.append(&mut self.w.to_bytes());
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_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 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.to_endian_bytes(endianess));
|
||||||
|
buffer.append(&mut self.y.to_endian_bytes(endianess));
|
||||||
|
buffer.append(&mut self.z.to_endian_bytes(endianess));
|
||||||
|
buffer.append(&mut self.w.to_endian_bytes(endianess));
|
||||||
|
|
||||||
// Return the byte buffer.
|
// Return the byte buffer.
|
||||||
buffer
|
buffer
|
||||||
@ -727,7 +1144,7 @@ impl<T> Transmutable for Vector4<T> where T: Number + ByteSized + Transmutable
|
|||||||
impl<T> Transmutable for Quaternion<T>
|
impl<T> Transmutable for Quaternion<T>
|
||||||
where T: Real + ByteSized + Transmutable
|
where T: Real + ByteSized + Transmutable
|
||||||
{
|
{
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Quaternion<T>
|
fn from_bytes(buffer: &[u8]) -> Quaternion<T>
|
||||||
{
|
{
|
||||||
let byte_size: usize;
|
let byte_size: usize;
|
||||||
let num_bytes: usize;
|
let num_bytes: usize;
|
||||||
@ -744,13 +1161,37 @@ impl<T> Transmutable for Quaternion<T>
|
|||||||
assert!(buffer.len() >= num_bytes);
|
assert!(buffer.len() >= num_bytes);
|
||||||
|
|
||||||
// Convert the given bytes to this type and return it.
|
// Convert the given bytes to this type and return it.
|
||||||
quat.scalar = T::from_bytes(&buffer[0..byte_size], endianess);
|
quat.scalar = T::from_bytes(&buffer[0..byte_size]);
|
||||||
quat.vector =
|
quat.vector =
|
||||||
Vector3::<T>::from_bytes(&buffer[byte_size..num_bytes], endianess);
|
Vector3::<T>::from_bytes(&buffer[byte_size..num_bytes]);
|
||||||
quat
|
quat
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_bytes(&self) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let byte_size: usize;
|
let byte_size: usize;
|
||||||
let num_bytes: usize;
|
let num_bytes: usize;
|
||||||
@ -766,8 +1207,31 @@ impl<T> Transmutable for Quaternion<T>
|
|||||||
buffer = Vec::with_capacity(num_bytes);
|
buffer = Vec::with_capacity(num_bytes);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
buffer.append(&mut self.scalar.to_bytes(endianess));
|
buffer.append(&mut self.scalar.to_bytes());
|
||||||
buffer.append(&mut self.vector.to_bytes(endianess));
|
buffer.append(&mut self.vector.to_bytes());
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_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 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.
|
||||||
|
buffer = Vec::with_capacity(num_bytes);
|
||||||
|
|
||||||
|
// Convert this to bytes and add it to the buffer.
|
||||||
|
buffer.append(&mut self.scalar.to_endian_bytes(endianess));
|
||||||
|
buffer.append(&mut self.vector.to_endian_bytes(endianess));
|
||||||
|
|
||||||
// Return the byte buffer.
|
// Return the byte buffer.
|
||||||
buffer
|
buffer
|
||||||
|
Loading…
x
Reference in New Issue
Block a user