From 143a6d60bc33dd065543eef2077e561fdb1331f0 Mon Sep 17 00:00:00 2001 From: Jason Travis Smith Date: Thu, 22 Dec 2016 13:47:56 -0500 Subject: [PATCH] Changed the name of to_bytes to as_bytes to conform to other code. An into_bytes function, which just called as_bytes, was decided as not being needed because it would require that the Transmutable trait have the requirement of being Sized. This may not be desirable in the future, so for now there is no into_bytes. It can easily be added later with a change to 9 lines of code. --- src/endian.rs | 4 +- src/transmutable.rs | 1061 ++++++++++++++++++++++--------------------- 2 files changed, 542 insertions(+), 523 deletions(-) diff --git a/src/endian.rs b/src/endian.rs index a29e38d..cd4a7cf 100644 --- a/src/endian.rs +++ b/src/endian.rs @@ -505,7 +505,7 @@ pub fn network_to_platform_order(val: T) -> T else { // Convert the value from Big endian to Little endian. - buffer = val.to_endian_bytes(Endianess::Network); + buffer = val.as_endian_bytes(Endianess::Network); T::from_endian_bytes(buffer.as_slice(), Endianess::Platform) } } @@ -526,7 +526,7 @@ pub fn platform_to_network_order(val: T) -> T else { // Convert the value from Little endian to Big endian. - buffer = val.to_endian_bytes(Endianess::Platform); + buffer = val.as_endian_bytes(Endianess::Platform); T::from_endian_bytes(buffer.as_slice(), Endianess::Network) } } diff --git a/src/transmutable.rs b/src/transmutable.rs index c0e6ae0..87b257f 100644 --- a/src/transmutable.rs +++ b/src/transmutable.rs @@ -17,18 +17,18 @@ use ::endian::Endianess; /// A type that can be converted to and from bytes. pub trait Transmutable { + /// Transmute this type to an array of bytes. + fn as_bytes(&self) -> Vec; + + /// Transmute this type to an array of bytes. + fn as_endian_bytes(&self, endianess: Endianess) -> Vec; + /// Transmute an array of bytes to this type. 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. - fn to_bytes(&self) -> Vec; - - /// Transmute this type to an array of bytes. - fn to_endian_bytes(&self, endianess: Endianess) -> Vec; - /// Get the current size of this Transmutable in bytes. fn determine_byte_size(&self) -> usize; } @@ -153,6 +153,29 @@ macro_rules! unpack_bits impl Transmutable for u8 { + fn as_bytes(&self) -> Vec + { + // 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 + { + let mut buffer: Vec; + + // 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 + } + fn from_bytes(buffer: &[u8]) -> u8 { // Convert the given bytes to this type and return it. @@ -171,29 +194,6 @@ impl Transmutable for u8 buffer[0] } - fn to_bytes(&self) -> Vec - { - // Unpack the bits that make up this value and - // return a buffer of the bytes. - unpack_bits!(*self, u8::BYTES) - } - - #[allow(unused_variables)] - fn to_endian_bytes(&self, endianess: Endianess) -> Vec - { - let mut buffer: Vec; - - // 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 - } - fn determine_byte_size(&self) -> usize { u8::BYTES @@ -202,6 +202,28 @@ impl Transmutable for u8 impl Transmutable for u16 { + fn as_bytes(&self) -> Vec + { + // Unpack the bits that make up this value and + // return a buffer of the bytes. + unpack_bits!(*self, u16::BYTES) + } + + fn as_endian_bytes(&self, endianess: Endianess) -> Vec + { + let mut buffer: Vec; + + // Create the Vector to hold the bytes + // from this type. + buffer = Vec::with_capacity(u16::BYTES); + + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, u16_to_bytes); + + // Return the byte buffer. + buffer + } + fn from_bytes(buffer: &[u8]) -> u16 { // Convert the given bytes to this type and return it. @@ -218,28 +240,6 @@ impl Transmutable for u16 handle_endianess_from_bytes!(buffer, endianess, bytes_to_u16) } - fn to_bytes(&self) -> Vec - { - // 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 - { - let mut buffer: Vec; - - // Create the Vector to hold the bytes - // from this type. - buffer = Vec::with_capacity(u16::BYTES); - - // Convert this to bytes and add it to the buffer. - handle_endianess_to_bytes!(buffer, self, endianess, u16_to_bytes); - - // Return the byte buffer. - buffer - } - fn determine_byte_size(&self) -> usize { u16::BYTES @@ -248,6 +248,28 @@ impl Transmutable for u16 impl Transmutable for u32 { + fn as_bytes(&self) -> Vec + { + // Unpack the bits that make up this value and + // return a buffer of the bytes. + unpack_bits!(*self, u32::BYTES) + } + + fn as_endian_bytes(&self, endianess: Endianess) -> Vec + { + let mut buffer: Vec; + + // Create the Vector to hold the bytes + // from this type. + buffer = Vec::with_capacity(u32::BYTES); + + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, u32_to_bytes); + + // Return the byte buffer. + buffer + } + fn from_bytes(buffer: &[u8]) -> u32 { // Convert the given bytes to this type and return it. @@ -264,28 +286,6 @@ impl Transmutable for u32 handle_endianess_from_bytes!(buffer, endianess, bytes_to_u32) } - fn to_bytes(&self) -> Vec - { - // 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 - { - let mut buffer: Vec; - - // Create the Vector to hold the bytes - // from this type. - buffer = Vec::with_capacity(u32::BYTES); - - // Convert this to bytes and add it to the buffer. - handle_endianess_to_bytes!(buffer, self, endianess, u32_to_bytes); - - // Return the byte buffer. - buffer - } - fn determine_byte_size(&self) -> usize { u32::BYTES @@ -294,6 +294,28 @@ impl Transmutable for u32 impl Transmutable for u64 { + fn as_bytes(&self) -> Vec + { + // Unpack the bits that make up this value and + // return a buffer of the bytes. + unpack_bits!(*self, u64::BYTES) + } + + fn as_endian_bytes(&self, endianess: Endianess) -> Vec + { + let mut buffer: Vec; + + // Create the Vector to hold the bytes + // from this type. + buffer = Vec::with_capacity(u64::BYTES); + + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, u64_to_bytes); + + // Return the byte buffer. + buffer + } + fn from_bytes(buffer: &[u8]) -> u64 { // Convert the given bytes to this type and return it. @@ -310,28 +332,6 @@ impl Transmutable for u64 handle_endianess_from_bytes!(buffer, endianess, bytes_to_u64) } - fn to_bytes(&self) -> Vec - { - // 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 - { - let mut buffer: Vec; - - // Create the Vector to hold the bytes - // from this type. - buffer = Vec::with_capacity(u64::BYTES); - - // Convert this to bytes and add it to the buffer. - handle_endianess_to_bytes!(buffer, self, endianess, u64_to_bytes); - - // Return the byte buffer. - buffer - } - fn determine_byte_size(&self) -> usize { u64::BYTES @@ -340,6 +340,28 @@ impl Transmutable for u64 impl Transmutable for usize { + fn as_bytes(&self) -> Vec + { + // Unpack the bits that make up this value and + // return a buffer of the bytes. + unpack_bits!(*self, usize::BYTES) + } + + fn as_endian_bytes(&self, endianess: Endianess) -> Vec + { + let mut buffer: Vec; + + // Create the Vector to hold the bytes + // from this type. + buffer = Vec::with_capacity(usize::BYTES); + + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, usize_to_bytes); + + // Return the byte buffer. + buffer + } + fn from_bytes(buffer: &[u8]) -> usize { // Convert the given bytes to this type and return it. @@ -356,28 +378,6 @@ impl Transmutable for usize handle_endianess_from_bytes!(buffer, endianess, bytes_to_usize) } - fn to_bytes(&self) -> Vec - { - // 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 - { - let mut buffer: Vec; - - // Create the Vector to hold the bytes - // from this type. - buffer = Vec::with_capacity(usize::BYTES); - - // Convert this to bytes and add it to the buffer. - handle_endianess_to_bytes!(buffer, self, endianess, usize_to_bytes); - - // Return the byte buffer. - buffer - } - fn determine_byte_size(&self) -> usize { usize::BYTES @@ -386,6 +386,29 @@ impl Transmutable for usize impl Transmutable for i8 { + fn as_bytes(&self) -> Vec + { + // Unpack the bits that make up this value and + // return a buffer of the bytes. + unpack_bits!(*self, i8::BYTES) + } + + #[allow(unused_variables)] + fn as_endian_bytes(&self, endianess: Endianess) -> Vec + { + let mut buffer: Vec; + + // Create the Vector to hold the bytes + // from this type. + buffer = Vec::with_capacity(i8::BYTES); + + // Convert this to bytes and add it to the buffer. + buffer.push(*self as u8); + + // Return the byte buffer. + buffer + } + fn from_bytes(buffer: &[u8]) -> i8 { // Convert the given bytes to this type and return it. @@ -404,29 +427,6 @@ impl Transmutable for i8 buffer[0] as i8 } - fn to_bytes(&self) -> Vec - { - // Unpack the bits that make up this value and - // return a buffer of the bytes. - unpack_bits!(*self, i8::BYTES) - } - - #[allow(unused_variables)] - fn to_endian_bytes(&self, endianess: Endianess) -> Vec - { - let mut buffer: Vec; - - // Create the Vector to hold the bytes - // from this type. - buffer = Vec::with_capacity(i8::BYTES); - - // Convert this to bytes and add it to the buffer. - buffer.push(*self as u8); - - // Return the byte buffer. - buffer - } - fn determine_byte_size(&self) -> usize { i8::BYTES @@ -435,6 +435,28 @@ impl Transmutable for i8 impl Transmutable for i16 { + fn as_bytes(&self) -> Vec + { + // Unpack the bits that make up this value and + // return a buffer of the bytes. + unpack_bits!(*self, i16::BYTES) + } + + fn as_endian_bytes(&self, endianess: Endianess) -> Vec + { + let mut buffer: Vec; + + // Create the Vector to hold the bytes + // from this type. + buffer = Vec::with_capacity(i16::BYTES); + + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, i16_to_bytes); + + // Return the byte buffer. + buffer + } + fn from_bytes(buffer: &[u8]) -> i16 { // Convert the given bytes to this type and return it. @@ -451,28 +473,6 @@ impl Transmutable for i16 handle_endianess_from_bytes!(buffer, endianess, bytes_to_i16) } - fn to_bytes(&self) -> Vec - { - // 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 - { - let mut buffer: Vec; - - // Create the Vector to hold the bytes - // from this type. - buffer = Vec::with_capacity(i16::BYTES); - - // Convert this to bytes and add it to the buffer. - handle_endianess_to_bytes!(buffer, self, endianess, i16_to_bytes); - - // Return the byte buffer. - buffer - } - fn determine_byte_size(&self) -> usize { i16::BYTES @@ -481,6 +481,28 @@ impl Transmutable for i16 impl Transmutable for i32 { + fn as_bytes(&self) -> Vec + { + // Unpack the bits that make up this value and + // return a buffer of the bytes. + unpack_bits!(*self, i32::BYTES) + } + + fn as_endian_bytes(&self, endianess: Endianess) -> Vec + { + let mut buffer: Vec; + + // Create the Vector to hold the bytes + // from this type. + buffer = Vec::with_capacity(i32::BYTES); + + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, i32_to_bytes); + + // Return the byte buffer. + buffer + } + fn from_bytes(buffer: &[u8]) -> i32 { // Convert the given bytes to this type and return it. @@ -497,28 +519,6 @@ impl Transmutable for i32 handle_endianess_from_bytes!(buffer, endianess, bytes_to_i32) } - fn to_bytes(&self) -> Vec - { - // 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 - { - let mut buffer: Vec; - - // Create the Vector to hold the bytes - // from this type. - buffer = Vec::with_capacity(i32::BYTES); - - // Convert this to bytes and add it to the buffer. - handle_endianess_to_bytes!(buffer, self, endianess, i32_to_bytes); - - // Return the byte buffer. - buffer - } - fn determine_byte_size(&self) -> usize { i32::BYTES @@ -527,6 +527,28 @@ impl Transmutable for i32 impl Transmutable for i64 { + fn as_bytes(&self) -> Vec + { + // Unpack the bits that make up this value and + // return a buffer of the bytes. + unpack_bits!(*self, i64::BYTES) + } + + fn as_endian_bytes(&self, endianess: Endianess) -> Vec + { + let mut buffer: Vec; + + // Create the Vector to hold the bytes + // from this type. + buffer = Vec::with_capacity(i64::BYTES); + + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, i64_to_bytes); + + // Return the byte buffer. + buffer + } + fn from_bytes(buffer: &[u8]) -> i64 { // Convert the given bytes to this type and return it. @@ -543,28 +565,6 @@ impl Transmutable for i64 handle_endianess_from_bytes!(buffer, endianess, bytes_to_i64) } - fn to_bytes(&self) -> Vec - { - // 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 - { - let mut buffer: Vec; - - // Create the Vector to hold the bytes - // from this type. - buffer = Vec::with_capacity(i64::BYTES); - - // Convert this to bytes and add it to the buffer. - handle_endianess_to_bytes!(buffer, self, endianess, i64_to_bytes); - - // Return the byte buffer. - buffer - } - fn determine_byte_size(&self) -> usize { i64::BYTES @@ -573,6 +573,28 @@ impl Transmutable for i64 impl Transmutable for isize { + fn as_bytes(&self) -> Vec + { + // Unpack the bits that make up this value and + // return a buffer of the bytes. + unpack_bits!(*self, isize::BYTES) + } + + fn as_endian_bytes(&self, endianess: Endianess) -> Vec + { + let mut buffer: Vec; + + // Create the Vector to hold the bytes + // from this type. + buffer = Vec::with_capacity(isize::BYTES); + + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, isize_to_bytes); + + // Return the byte buffer. + buffer + } + fn from_bytes(buffer: &[u8]) -> isize { // Convert the given bytes to this type and return it. @@ -589,28 +611,6 @@ impl Transmutable for isize handle_endianess_from_bytes!(buffer, endianess, bytes_to_isize) } - fn to_bytes(&self) -> Vec - { - // 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 - { - let mut buffer: Vec; - - // Create the Vector to hold the bytes - // from this type. - buffer = Vec::with_capacity(isize::BYTES); - - // Convert this to bytes and add it to the buffer. - handle_endianess_to_bytes!(buffer, self, endianess, isize_to_bytes); - - // Return the byte buffer. - buffer - } - fn determine_byte_size(&self) -> usize { isize::BYTES @@ -619,6 +619,33 @@ impl Transmutable for isize impl Transmutable for f32 { + fn as_bytes(&self) -> Vec + { + let value: u32; + + unsafe + { + value = ::std::mem::transmute::(*self); + } + + value.as_bytes() + } + + fn as_endian_bytes(&self, endianess: Endianess) -> Vec + { + let mut buffer: Vec; + + // Create the Vector to hold the bytes + // from this type. + buffer = Vec::with_capacity(f32::BYTES); + + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, f32_to_bytes); + + // Return the byte buffer. + buffer + } + fn from_bytes(buffer: &[u8]) -> f32 { unsafe @@ -637,33 +664,6 @@ impl Transmutable for f32 handle_endianess_from_bytes!(buffer, endianess, bytes_to_f32) } - fn to_bytes(&self) -> Vec - { - let value: u32; - - unsafe - { - value = ::std::mem::transmute::(*self); - } - - value.to_bytes() - } - - fn to_endian_bytes(&self, endianess: Endianess) -> Vec - { - let mut buffer: Vec; - - // Create the Vector to hold the bytes - // from this type. - buffer = Vec::with_capacity(f32::BYTES); - - // Convert this to bytes and add it to the buffer. - handle_endianess_to_bytes!(buffer, self, endianess, f32_to_bytes); - - // Return the byte buffer. - buffer - } - fn determine_byte_size(&self) -> usize { f32::BYTES @@ -672,6 +672,33 @@ impl Transmutable for f32 impl Transmutable for f64 { + fn as_bytes(&self) -> Vec + { + let value: u64; + + unsafe + { + value = ::std::mem::transmute::(*self); + } + + value.as_bytes() + } + + fn as_endian_bytes(&self, endianess: Endianess) -> Vec + { + let mut buffer: Vec; + + // Create the Vector to hold the bytes + // from this type. + buffer = Vec::with_capacity(f64::BYTES); + + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, f64_to_bytes); + + // Return the byte buffer. + buffer + } + fn from_bytes(buffer: &[u8]) -> f64 { unsafe @@ -690,33 +717,6 @@ impl Transmutable for f64 handle_endianess_from_bytes!(buffer, endianess, bytes_to_f64) } - fn to_bytes(&self) -> Vec - { - let value: u64; - - unsafe - { - value = ::std::mem::transmute::(*self); - } - - value.to_bytes() - } - - fn to_endian_bytes(&self, endianess: Endianess) -> Vec - { - let mut buffer: Vec; - - // Create the Vector to hold the bytes - // from this type. - buffer = Vec::with_capacity(f64::BYTES); - - // Convert this to bytes and add it to the buffer. - handle_endianess_to_bytes!(buffer, self, endianess, f64_to_bytes); - - // Return the byte buffer. - buffer - } - fn determine_byte_size(&self) -> usize { f64::BYTES @@ -725,6 +725,74 @@ impl Transmutable for f64 impl Transmutable for String { + fn as_bytes(&self) -> Vec + { + let bytes: &[u8]; + let byte_count: u64; + let mut buffer: Vec; + + // 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.as_bytes()); + + // Add each byte of the string to the buffer. + buffer.extend_from_slice(bytes); + + // Return the byte buffer. + buffer + } + + fn as_endian_bytes(&self, endianess: Endianess) -> Vec + { + let temp: String; + let mut buffer: Vec; + + // Create the Vector to hold the bytes + // from this type. + buffer = Vec::with_capacity(get_byte_size_of_string(self)); + + // 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 + { + Endianess::Big => + { + buffer.append(&mut BigEndian::string_to_bytes(temp)); + } + + Endianess::Little => + { + buffer.append(&mut LittleEndian::string_to_bytes(temp)); + } + + Endianess::Platform => + { + buffer.append(&mut PlatformEndian::string_to_bytes(temp)); + } + + Endianess::Network => + { + buffer.append(&mut NetworkEndian::string_to_bytes(temp)); + } + } + + // Return the byte buffer. + buffer + } + fn from_bytes(buffer: &[u8]) -> String { let byte_count: u64; @@ -767,74 +835,6 @@ impl Transmutable for String handle_endianess_from_bytes!(buffer, endianess, bytes_to_string) } - fn to_bytes(&self) -> Vec - { - let bytes: &[u8]; - let byte_count: u64; - let mut buffer: Vec; - - // 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 - { - let temp: String; - let mut buffer: Vec; - - // Create the Vector to hold the bytes - // from this type. - buffer = Vec::with_capacity(get_byte_size_of_string(self)); - - // 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 - { - Endianess::Big => - { - buffer.append(&mut BigEndian::string_to_bytes(temp)); - } - - Endianess::Little => - { - buffer.append(&mut LittleEndian::string_to_bytes(temp)); - } - - Endianess::Platform => - { - buffer.append(&mut PlatformEndian::string_to_bytes(temp)); - } - - Endianess::Network => - { - buffer.append(&mut NetworkEndian::string_to_bytes(temp)); - } - } - - // Return the byte buffer. - buffer - } - fn determine_byte_size(&self) -> usize { get_byte_size_of_string(self) @@ -844,6 +844,52 @@ impl Transmutable for String #[cfg(feature="convert_sigils")] impl Transmutable for Vector2 where T: Number + ByteSized + Transmutable { + fn as_bytes(&self) -> Vec + { + let byte_size: usize; + let num_bytes: usize; + let mut buffer: Vec; + + // 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 + { + let byte_size: usize; + let num_bytes: usize; + let mut buffer: Vec; + + // 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 + } + fn from_bytes(buffer: &[u8]) -> Vector2 { let byte_size: usize; @@ -890,52 +936,6 @@ impl Transmutable for Vector2 where T: Number + ByteSized + Transmutable vec } - fn to_bytes(&self) -> Vec - { - let byte_size: usize; - let num_bytes: usize; - let mut buffer: Vec; - - // 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_bytes()); - buffer.append(&mut self.y.to_bytes()); - - // Return the byte buffer. - buffer - } - - fn to_endian_bytes(&self, endianess: Endianess) -> Vec - { - let byte_size: usize; - let num_bytes: usize; - let mut buffer: Vec; - - // 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. - buffer - } - fn determine_byte_size(&self) -> usize { T::get_byte_size() * self.get_size() as usize @@ -945,6 +945,54 @@ impl Transmutable for Vector2 where T: Number + ByteSized + Transmutable #[cfg(feature="convert_sigils")] impl Transmutable for Vector3 where T: Number + ByteSized + Transmutable { + fn as_bytes(&self) -> Vec + { + let byte_size: usize; + let num_bytes: usize; + let mut buffer: Vec; + + // 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()); + buffer.append(&mut self.z.as_bytes()); + + // Return the byte buffer. + buffer + } + + fn as_endian_bytes(&self, endianess: Endianess) -> Vec + { + let byte_size: usize; + let num_bytes: usize; + let mut buffer: Vec; + + // 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. + 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)); + + // Return the byte buffer. + buffer + } + fn from_bytes(buffer: &[u8]) -> Vector3 { let byte_size: usize; @@ -994,35 +1042,6 @@ impl Transmutable for Vector3 where T: Number + ByteSized + Transmutable vec } - fn to_bytes(&self) -> Vec - { - Vec::new() - } - - fn to_endian_bytes(&self, endianess: Endianess) -> Vec - { - let byte_size: usize; - let num_bytes: usize; - let mut buffer: Vec; - - // 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. - 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)); - - // Return the byte buffer. - buffer - } - fn determine_byte_size(&self) -> usize { T::get_byte_size() * self.get_size() as usize @@ -1032,6 +1051,56 @@ impl Transmutable for Vector3 where T: Number + ByteSized + Transmutable #[cfg(feature="convert_sigils")] impl Transmutable for Vector4 where T: Number + ByteSized + Transmutable { + fn as_bytes(&self) -> Vec + { + let byte_size: usize; + let num_bytes: usize; + let mut buffer: Vec; + + // 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_bytes()); + buffer.append(&mut self.y.as_bytes()); + buffer.append(&mut self.z.as_bytes()); + buffer.append(&mut self.w.as_bytes()); + + // Return the byte buffer. + buffer + } + + fn as_endian_bytes(&self, endianess: Endianess) -> Vec + { + let byte_size: usize; + let num_bytes: usize; + let mut buffer: Vec; + + // 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 + } + fn from_bytes(buffer: &[u8]) -> Vector4 { let byte_size: usize; @@ -1084,56 +1153,6 @@ impl Transmutable for Vector4 where T: Number + ByteSized + Transmutable vec } - fn to_bytes(&self) -> Vec - { - let byte_size: usize; - let num_bytes: usize; - let mut buffer: Vec; - - // 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_bytes()); - buffer.append(&mut self.y.to_bytes()); - buffer.append(&mut self.z.to_bytes()); - buffer.append(&mut self.w.to_bytes()); - - // Return the byte buffer. - buffer - } - - fn to_endian_bytes(&self, endianess: Endianess) -> Vec - { - let byte_size: usize; - let num_bytes: usize; - let mut buffer: Vec; - - // 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. - buffer - } - fn determine_byte_size(&self) -> usize { T::get_byte_size() * self.get_size() as usize @@ -1144,6 +1163,52 @@ impl Transmutable for Vector4 where T: Number + ByteSized + Transmutable impl Transmutable for Quaternion where T: Real + ByteSized + Transmutable { + fn as_bytes(&self) -> Vec + { + let byte_size: usize; + let num_bytes: usize; + let mut buffer: Vec; + + // 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.as_bytes()); + buffer.append(&mut self.vector.as_bytes()); + + // Return the byte buffer. + buffer + } + + fn as_endian_bytes(&self, endianess: Endianess) -> Vec + { + let byte_size: usize; + let num_bytes: usize; + let mut buffer: Vec; + + // 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.as_endian_bytes(endianess)); + buffer.append(&mut self.vector.as_endian_bytes(endianess)); + + // Return the byte buffer. + buffer + } + fn from_bytes(buffer: &[u8]) -> Quaternion { let byte_size: usize; @@ -1191,52 +1256,6 @@ impl Transmutable for Quaternion quat } - fn to_bytes(&self) -> Vec - { - let byte_size: usize; - let num_bytes: usize; - let mut buffer: Vec; - - // 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_bytes()); - buffer.append(&mut self.vector.to_bytes()); - - // Return the byte buffer. - buffer - } - - fn to_endian_bytes(&self, endianess: Endianess) -> Vec - { - let byte_size: usize; - let num_bytes: usize; - let mut buffer: Vec; - - // 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. - buffer - } - fn determine_byte_size(&self) -> usize { T::get_byte_size() * 4usize