From 4291a1ef5dc521e95fcff4de90ff4baaff6dc531 Mon Sep 17 00:00:00 2001 From: Jason Travis Smith Date: Thu, 14 Apr 2016 13:33:07 -0400 Subject: [PATCH] Any conversion to bytes now uses a Vec. This was changed to make everything easier to use. The mutable buffers were not working well for combined types. It was move to a growable array to make these more advanced combined types, other structures, easier to implement. --- examples/convert_f32.rs | 24 ++-- examples/convert_quaternion.rs | 14 +- examples/convert_vector2.rs | 14 +- src/converter.rs | 40 +++--- src/endian.rs | 149 ++++++++++++++------ src/transmutable.rs | 244 +++++++++++++++++++++------------ 6 files changed, 313 insertions(+), 172 deletions(-) diff --git a/examples/convert_f32.rs b/examples/convert_f32.rs index e698501..806dda5 100644 --- a/examples/convert_f32.rs +++ b/examples/convert_f32.rs @@ -12,22 +12,22 @@ fn use_converter() { let num: f32; let final_num: f32; - let mut buffer: [u8; F32_BYTES]; + let mut buffer: Vec; // Initialize the variables. num = 6.291985f32; - buffer = [0u8; F32_BYTES]; + buffer = Vec::new(); println!("Converting the value {} into and out of an array of bytes.", num); - println!("Buffer starts as: {}", stringify_array(&buffer)); + println!("Buffer starts as: {}", stringify_array(buffer.as_slice())); // Convert the floating point number into an array of bytes. - PlatformEndian::f32_to_bytes(&mut buffer, num); + buffer = PlatformEndian::f32_to_bytes(num); - println!("Buffer contains: {}", stringify_array(&buffer)); + println!("Buffer contains: {}", stringify_array(buffer.as_slice())); // Convert the array of bytes into a floating point number. - final_num = PlatformEndian::bytes_to_f32(&buffer); + final_num = PlatformEndian::bytes_to_f32(buffer.as_slice()); println!("The buffer converts back to: {}", final_num); } @@ -36,23 +36,23 @@ pub fn use_transmutable() let num: f32; let final_num: f32; let endianess: Endianess; - let mut buffer: [u8; F32_BYTES]; + let mut buffer: Vec; // Initialize the variables. num = 6.291985f32; - buffer = [0u8; F32_BYTES]; + buffer = Vec::new(); endianess = Endianess::PLATFORM; println!("Converting the value {} into and out of an array of bytes.", num); - println!("Buffer starts as: {}", stringify_array(&buffer)); + println!("Buffer starts as: {}", stringify_array(buffer.as_slice())); // Convert the floating point number into an array of bytes. - num.to_bytes(&mut buffer, endianess); + buffer = num.to_bytes(endianess); - println!("Buffer contains: {}", stringify_array(&buffer)); + println!("Buffer contains: {}", stringify_array(buffer.as_slice())); // Convert the array of bytes into a floating point number. - final_num = f32::from_bytes(&buffer, endianess); + final_num = f32::from_bytes(buffer.as_slice(), endianess); println!("The buffer converts back to: {}", final_num); } diff --git a/examples/convert_quaternion.rs b/examples/convert_quaternion.rs index a9344cf..2cf1f1a 100644 --- a/examples/convert_quaternion.rs +++ b/examples/convert_quaternion.rs @@ -1,5 +1,3 @@ -#![feature(convert)] - extern crate alchemy; extern crate sigils; @@ -62,25 +60,25 @@ pub fn main() let quat: Quaternion; let final_quat: Quaternion; let endianess: Endianess; - let mut buffer: [u8; SIZE_OF_QUATERNION]; + let mut buffer: Vec; // Initialize the variables. quat = Quaternion::::from_values(6.29f64, 1.9f64, 8.5f64, 7.11f64); - buffer = [0u8; SIZE_OF_QUATERNION]; + buffer = Vec::with_capacity(SIZE_OF_QUATERNION); endianess = Endianess::PLATFORM; println!("Transmuting a Quaternion:"); println!("Converting the value {} into and out of an array of bytes.", quat); - println!("Buffer starts as: {}", stringify_array(&buffer)); + println!("Buffer starts as: {}", stringify_array(buffer.as_slice())); // Convert the Vector2 into an array of bytes. - quat.to_bytes(&mut buffer, endianess); + buffer = quat.to_bytes(endianess); - println!("Buffer contains: {}", stringify_array(&buffer)); + println!("Buffer contains: {}", stringify_array(buffer.as_slice())); // Convert the array of bytes into a Vector2. - final_quat = Quaternion::from_bytes(&buffer, endianess); + final_quat = Quaternion::from_bytes(buffer.as_slice(), endianess); println!("The buffer converts back to: {}", final_quat); } diff --git a/examples/convert_vector2.rs b/examples/convert_vector2.rs index b528d82..0ecafa8 100644 --- a/examples/convert_vector2.rs +++ b/examples/convert_vector2.rs @@ -1,5 +1,3 @@ -#![feature(convert)] - extern crate alchemy; extern crate sigils; @@ -62,25 +60,25 @@ pub fn main() let vec: Vector2; let final_vec: Vector2; let endianess: Endianess; - let mut buffer: [u8; SIZE_OF_VECTOR_2]; + let mut buffer: Vec; // Initialize the variables. vec = Vector2::::new(629u64, 1985u64); - buffer = [0u8; SIZE_OF_VECTOR_2]; + buffer = Vec::with_capacity(SIZE_OF_VECTOR_2); endianess = Endianess::PLATFORM; println!("Transmuting a Vector2:"); println!("Converting the value {} into and out of an array of bytes.", vec); - println!("Buffer starts as: {}", stringify_array(&buffer)); + println!("Buffer starts as: {}", stringify_array(buffer.as_slice())); // Convert the Vector2 into an array of bytes. - vec.to_bytes(&mut buffer, endianess); + buffer = vec.to_bytes(endianess); - println!("Buffer contains: {}", stringify_array(&buffer)); + println!("Buffer contains: {}", stringify_array(buffer.as_slice())); // Convert the array of bytes into a Vector2. - final_vec = Vector2::from_bytes(&buffer, endianess); + final_vec = Vector2::from_bytes(buffer.as_slice(), endianess); println!("The buffer converts back to: {}", final_vec); } diff --git a/src/converter.rs b/src/converter.rs index fb37712..b6e68be 100644 --- a/src/converter.rs +++ b/src/converter.rs @@ -1,5 +1,7 @@ use std::mem; +use ::byte_sized::ByteSized; + /// Describes types that can convert Numbers into bytes, @@ -87,9 +89,9 @@ pub trait Converter /// # Panics /// This will panic if the buffer does not have /// enough space to store the converted value. - fn i16_to_bytes(buffer: &mut [u8], num: i16) + fn i16_to_bytes(num: i16) -> Vec { - Self::u16_to_bytes(buffer, num as u16) + Self::u16_to_bytes(num as u16) } /// Converts a signed 32-bit integer to bytes @@ -98,9 +100,9 @@ pub trait Converter /// # Panics /// This will panic if the buffer does not have /// enough space to store the converted value. - fn i32_to_bytes(buffer: &mut [u8], num: i32) + fn i32_to_bytes(num: i32) -> Vec { - Self::u32_to_bytes(buffer, num as u32) + Self::u32_to_bytes(num as u32) } /// Converts a signed 64-bit integer to bytes @@ -109,9 +111,9 @@ pub trait Converter /// # Panics /// This will panic if the buffer does not have /// enough space to store the converted value. - fn i64_to_bytes(buffer: &mut [u8], num: i64) + fn i64_to_bytes(num: i64) -> Vec { - Self::u64_to_bytes(buffer, num as u64) + Self::u64_to_bytes(num as u64) } /// Converts a signed integer to bytes @@ -124,14 +126,12 @@ pub trait Converter /// This will panic if the number of bytes /// passed in is less than the byte size of the given number /// or more than eight. - fn isize_to_bytes(buffer: &mut [u8], num: isize) + fn isize_to_bytes(num: isize) -> Vec { let temp_num: usize; - assert!(buffer.len() >= 1 && buffer.len() <= 8); - - temp_num = remove_sign(num as i64, buffer.len() as u8) as usize; - Self::usize_to_bytes(buffer, temp_num) + temp_num = remove_sign(num as i64, isize::BYTES as u8) as usize; + Self::usize_to_bytes(temp_num) } /// Converts a 32-bit floating point number to bytes @@ -140,11 +140,11 @@ pub trait Converter /// # Panics /// This will panic if the buffer does not have /// enough space to store the converted value. - fn f32_to_bytes(buffer: &mut [u8], num: f32) + fn f32_to_bytes(num: f32) -> Vec { unsafe { - Self::u32_to_bytes(buffer, mem::transmute::(num)); + Self::u32_to_bytes(mem::transmute::(num)) } } @@ -154,11 +154,11 @@ pub trait Converter /// # Panics /// This will panic if the buffer does not have /// enough space to store the converted value. - fn f64_to_bytes(buffer: &mut [u8], num: f64) + fn f64_to_bytes(num: f64) -> Vec { unsafe { - Self::u64_to_bytes(buffer, mem::transmute::(num)); + Self::u64_to_bytes(mem::transmute::(num)) } } @@ -202,7 +202,7 @@ pub trait Converter /// # Panics /// This will panic if the buffer does not have /// enough space to store the converted value. - fn u16_to_bytes(buffer: &mut [u8], num: u16); + fn u16_to_bytes(num: u16) -> Vec; /// Converts an unsigned 32-bit integer to bytes /// and places them into the given buffer. @@ -210,7 +210,7 @@ pub trait Converter /// # Panics /// This will panic if the buffer does not have /// enough space to store the converted value. - fn u32_to_bytes(buffer: &mut [u8], num: u32); + fn u32_to_bytes(num: u32) -> Vec; /// Converts an unsigned 64-bit integer to bytes /// and places them into the given buffer. @@ -218,7 +218,7 @@ pub trait Converter /// # Panics /// This will panic if the buffer does not have /// enough space to store the converted value. - fn u64_to_bytes(buffer: &mut [u8], num: u64); + fn u64_to_bytes(num: u64) -> Vec; /// Converts an unsigned integer to bytes /// and places them into the given buffer. @@ -230,7 +230,7 @@ pub trait Converter /// This will panic if the number of bytes /// passed in is less than the byte size of the given number /// or more than eight. - fn usize_to_bytes(buffer: &mut [u8], num: usize); + fn usize_to_bytes(num: usize) -> Vec; /// Converts a String to bytes @@ -250,7 +250,7 @@ pub trait Converter /// # Panics /// This will panic if the buffer does not have /// enough information to convert. - fn string_to_bytes(buffer: &mut [u8], string: String); + fn string_to_bytes(string: String) -> Vec; } diff --git a/src/endian.rs b/src/endian.rs index ee2064b..75416cb 100644 --- a/src/endian.rs +++ b/src/endian.rs @@ -77,20 +77,13 @@ macro_rules! write_bytes ($buffer: expr, $valueType: ident, $numBytes: expr, $num: expr, $convertFunc: ident) => ({ - assert!($buffer.len() >= $valueType::BYTES, - "Not enough room in the buffer to write to."); unsafe { - let size: usize; let bytes: [u8; $numBytes]; - size = $valueType::BYTES; - bytes = mem::transmute::<_, [u8; $numBytes]>($num.$convertFunc()); - copy_nonoverlapping::((&bytes).as_ptr(), - $buffer.as_mut_ptr(), - size); + $buffer.extend_from_slice(&bytes); } }) } @@ -134,38 +127,75 @@ impl Converter for BigEndian } - fn u16_to_bytes(buffer: &mut [u8], num: u16) + fn u16_to_bytes(num: u16) -> Vec { + let mut buffer: Vec; + + // Create the Vector to hold the bytes + // from this type. + buffer = Vec::with_capacity(u16::BYTES); + + // Write the bytes to the Vector. write_bytes!(buffer, u16, U16_BYTES, num, to_be); + + // Return the byte buffer. + buffer } - fn u32_to_bytes(buffer: &mut [u8], num: u32) + fn u32_to_bytes(num: u32) -> Vec { + let mut buffer: Vec; + + // Create the Vector to hold the bytes + // from this type. + buffer = Vec::with_capacity(u32::BYTES); + + // Write the bytes to the Vector. write_bytes!(buffer, u32, U32_BYTES, num, to_be); + + // Return the byte buffer. + buffer } - fn u64_to_bytes(buffer: &mut [u8], num: u64) + fn u64_to_bytes(num: u64) -> Vec { + let mut buffer: Vec; + + // Create the Vector to hold the bytes + // from this type. + buffer = Vec::with_capacity(u64::BYTES); + + // Write the bytes to the Vector. write_bytes!(buffer, u64, U64_BYTES, num, to_be); + + // Return the byte buffer. + buffer } - fn usize_to_bytes(buffer: &mut [u8], num: usize) + fn usize_to_bytes(num: usize) -> Vec { let bytes: [u8; 8]; let num_bytes: u8; + let mut buffer: Vec; - num_bytes = buffer.len() as u8; + num_bytes = usize::BYTES as u8; + + // Create a buffer with enough space for this type. + buffer = Vec::with_capacity(usize::BYTES); assert!(determine_size(num as u64) <= num_bytes && num_bytes <= 8); - assert!(buffer.len() >= 1 && buffer.len() <= 8); unsafe { bytes = mem::transmute::(num.to_be()); - copy_nonoverlapping::( - bytes.as_ptr().offset((8 - num_bytes) as isize), - buffer.as_mut_ptr(), num_bytes as usize); + buffer.extend_from_slice(&bytes); + //copy_nonoverlapping::( + // bytes.as_ptr().offset((8 - num_bytes) as isize), + // buffer.as_mut_ptr(), num_bytes as usize); } + + // Return the byte buffer. + buffer } fn bytes_to_string(buffer: &[u8]) -> String @@ -204,10 +234,11 @@ impl Converter for BigEndian new_string } - fn string_to_bytes(buffer: &mut [u8], string: String) + fn string_to_bytes(string: String) -> Vec { let bytes: &[u8]; let byte_count: u64; + let mut buffer: Vec; // Turn the string into a byte array. bytes = string.as_bytes(); @@ -217,16 +248,16 @@ impl Converter for BigEndian byte_count = bytes.len() as u64; // Make sure the buffer has enough space for this string. - assert!(buffer.len() as u64 >= byte_count + U64_BYTES as u64); + buffer = Vec::with_capacity(bytes.len() + U64_BYTES); // Add the count to the buffer. - BigEndian::u64_to_bytes(&mut buffer[0..U64_BYTES], byte_count); + buffer.append(&mut BigEndian::u64_to_bytes(byte_count)); // Add each byte of the string to the buffer. - for (i, byte) in bytes.iter().enumerate() - { - buffer[U64_BYTES + i] = byte.clone(); - } + buffer.extend_from_slice(bytes); + + // Return the byte buffer. + buffer } } @@ -264,37 +295,74 @@ impl Converter for LittleEndian } - fn u16_to_bytes(buffer: &mut [u8], num: u16) + fn u16_to_bytes(num: u16) -> Vec { + let mut buffer: Vec; + + // Create the Vector to hold the bytes + // from this type. + buffer = Vec::with_capacity(u16::BYTES); + + // Write the bytes to the Vector. write_bytes!(buffer, u16, U16_BYTES, num, to_le); + + // Return the byte buffer. + buffer } - fn u32_to_bytes(buffer: &mut [u8], num: u32) + fn u32_to_bytes(num: u32) -> Vec { + let mut buffer: Vec; + + // Create the Vector to hold the bytes + // from this type. + buffer = Vec::with_capacity(u32::BYTES); + + // Write the bytes to the Vector. write_bytes!(buffer, u32, U32_BYTES, num, to_le); + + // Return the byte buffer. + buffer } - fn u64_to_bytes(buffer: &mut [u8], num: u64) + fn u64_to_bytes(num: u64) -> Vec { + let mut buffer: Vec; + + // Create the Vector to hold the bytes + // from this type. + buffer = Vec::with_capacity(u64::BYTES); + + // Write the bytes to the Vector. write_bytes!(buffer, u64, U64_BYTES, num, to_le); + + // Return the byte buffer. + buffer } - fn usize_to_bytes(buffer: &mut [u8], num: usize) + fn usize_to_bytes(num: usize) -> Vec { let bytes: [u8; 8]; let num_bytes: u8; + let mut buffer: Vec; - num_bytes = buffer.len() as u8; + num_bytes = usize::BYTES as u8; + + // Create a buffer with enough space for this type. + buffer = Vec::with_capacity(usize::BYTES); assert!(determine_size(num as u64) <= num_bytes && num_bytes <= 8); - assert!(buffer.len() >= 1 && buffer.len() <= 8); unsafe { bytes = mem::transmute::(num.to_le()); - copy_nonoverlapping::(bytes.as_ptr(), buffer.as_mut_ptr(), - num_bytes as usize); + buffer.extend_from_slice(&bytes); + //copy_nonoverlapping::(bytes.as_ptr(), buffer.as_mut_ptr(), + // num_bytes as usize); } + + // Return the byte buffer. + buffer } fn bytes_to_string(buffer: &[u8]) -> String @@ -333,29 +401,30 @@ impl Converter for LittleEndian new_string } - fn string_to_bytes(buffer: &mut [u8], string: String) + fn string_to_bytes(string: String) -> Vec { let bytes: &[u8]; let byte_count: u64; + let mut buffer: Vec; // Turn the string into a byte array. bytes = string.as_bytes(); // Determine how many bytes will be written // for this string. - byte_count = bytes.len() as u64 + U64_BYTES as u64; + byte_count = bytes.len() as u64; // Make sure the buffer has enough space for this string. - assert!(buffer.len() as u64 >= byte_count); + buffer = Vec::with_capacity(bytes.len() + U64_BYTES); // Add the count to the buffer. - LittleEndian::u64_to_bytes(&mut buffer[0..U64_BYTES], byte_count); + buffer.append(&mut LittleEndian::u64_to_bytes(byte_count)); // Add each byte of the string to the buffer. - for (i, byte) in bytes.iter().enumerate() - { - buffer[U64_BYTES + i] = byte.clone(); - } + buffer.extend_from_slice(bytes); + + // Return the byte buffer. + buffer } } diff --git a/src/transmutable.rs b/src/transmutable.rs index 14f5f59..e81e786 100644 --- a/src/transmutable.rs +++ b/src/transmutable.rs @@ -2,7 +2,7 @@ use sigils::{Zero, Number, Real}; use sigils::vector::{Vector, Vector2, Vector3, Vector4}; use sigils::quaternion::Quaternion; -use ::byte_sized::ByteSized; +use ::byte_sized::{ByteSized, get_byte_size_of_string}; use ::converter::Converter; use ::endian::{BigEndian, LittleEndian, PlatformEndian, Endianess}; @@ -14,7 +14,7 @@ use ::endian::{BigEndian, LittleEndian, PlatformEndian, Endianess}; pub trait Transmutable { /// Transmute this type to an array of bytes. - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess); + fn to_bytes(&self, endianess: Endianess) -> Vec; /// Transmute an array of bytes to this type. fn from_bytes(buffer: &[u8], endianess: Endianess) -> Self; @@ -30,24 +30,21 @@ macro_rules! handle_endianess_to_bytes ($buffer: ident, $val: ident, $endianess: ident, $func: ident) => { { - let mut mut_buffer; - - mut_buffer = $buffer; match $endianess { Endianess::BIG => { - BigEndian::$func(&mut mut_buffer, *$val); + $buffer.append(&mut BigEndian::$func(*$val)); } Endianess::LITTLE => { - LittleEndian::$func(&mut mut_buffer, *$val); + $buffer.append(&mut LittleEndian::$func(*$val)); } Endianess::PLATFORM => { - PlatformEndian::$func(&mut mut_buffer, *$val); + $buffer.append(&mut PlatformEndian::$func(*$val)); } } } @@ -86,15 +83,19 @@ macro_rules! handle_endianess_from_bytes impl Transmutable for u8 { #[allow(unused_variables)] - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + fn to_bytes(&self, endianess: Endianess) -> Vec { - // Make sure that there is enough space to store - // the bytes from this type. - assert!(buffer.len() >= u8::BYTES); + 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[0] = *self; + buffer.push(*self); + + // Return the byte buffer. + buffer } #[allow(unused_variables)] @@ -112,14 +113,19 @@ impl Transmutable for u8 impl Transmutable for u16 { - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + fn to_bytes(&self, endianess: Endianess) -> Vec { - // Make sure that there is enough space to store - // the bytes from this type. - assert!(buffer.len() >= u16::BYTES); + 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], endianess: Endianess) -> u16 @@ -135,14 +141,19 @@ impl Transmutable for u16 impl Transmutable for u32 { - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + fn to_bytes(&self, endianess: Endianess) -> Vec { - // Make sure that there is enough space to store - // the bytes from this type. - assert!(buffer.len() >= u32::BYTES); + 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], endianess: Endianess) -> u32 @@ -158,14 +169,19 @@ impl Transmutable for u32 impl Transmutable for u64 { - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + fn to_bytes(&self, endianess: Endianess) -> Vec { - // Make sure that there is enough space to store - // the bytes from this type. - assert!(buffer.len() >= u64::BYTES); + 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], endianess: Endianess) -> u64 @@ -181,14 +197,19 @@ impl Transmutable for u64 impl Transmutable for usize { - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + fn to_bytes(&self, endianess: Endianess) -> Vec { - // Make sure that there is enough space to store - // the bytes from this type. - assert!(buffer.len() >= usize::BYTES); + 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], endianess: Endianess) -> usize @@ -205,14 +226,19 @@ impl Transmutable for usize impl Transmutable for i8 { #[allow(unused_variables)] - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + fn to_bytes(&self, endianess: Endianess) -> Vec { - // Make sure that there is enough space to store - // the bytes from this type. - assert!(buffer.len() >= i8::BYTES); + 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[0] = *self as u8; + buffer.push(*self as u8); + + // Return the byte buffer. + buffer } #[allow(unused_variables)] @@ -230,14 +256,19 @@ impl Transmutable for i8 impl Transmutable for i16 { - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + fn to_bytes(&self, endianess: Endianess) -> Vec { - // Make sure that there is enough space to store - // the bytes from this type. - assert!(buffer.len() >= i16::BYTES); + 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], endianess: Endianess) -> i16 @@ -253,14 +284,19 @@ impl Transmutable for i16 impl Transmutable for i32 { - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + fn to_bytes(&self, endianess: Endianess) -> Vec { - // Make sure that there is enough space to store - // the bytes from this type. - assert!(buffer.len() >= i32::BYTES); + 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], endianess: Endianess) -> i32 @@ -276,14 +312,19 @@ impl Transmutable for i32 impl Transmutable for i64 { - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + fn to_bytes(&self, endianess: Endianess) -> Vec { - // Make sure that there is enough space to store - // the bytes from this type. - assert!(buffer.len() >= i64::BYTES); + 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], endianess: Endianess) -> i64 @@ -299,14 +340,19 @@ impl Transmutable for i64 impl Transmutable for isize { - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + fn to_bytes(&self, endianess: Endianess) -> Vec { - // Make sure that there is enough space to store - // the bytes from this type. - assert!(buffer.len() >= isize::BYTES); + 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], endianess: Endianess) -> isize @@ -322,14 +368,19 @@ impl Transmutable for isize impl Transmutable for f32 { - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + fn to_bytes(&self, endianess: Endianess) -> Vec { - // Make sure that there is enough space to store - // the bytes from this type. - assert!(buffer.len() >= 4); + 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], endianess: Endianess) -> f32 @@ -345,14 +396,19 @@ impl Transmutable for f32 impl Transmutable for f64 { - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + fn to_bytes(&self, endianess: Endianess) -> Vec { - // Make sure that there is enough space to store - // the bytes from this type. - assert!(buffer.len() >= 8); + 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], endianess: Endianess) -> f64 @@ -368,13 +424,14 @@ impl Transmutable for f64 impl Transmutable for String { - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + fn to_bytes(&self, endianess: Endianess) -> Vec { let temp: String; - let mut mut_buffer; + let mut buffer: Vec; - // Create a mutable handle to the buffer. - mut_buffer = buffer; + // 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. @@ -386,19 +443,22 @@ impl Transmutable for String { Endianess::BIG => { - BigEndian::string_to_bytes(&mut mut_buffer, temp); + buffer.append(&mut BigEndian::string_to_bytes(temp)); } Endianess::LITTLE => { - LittleEndian::string_to_bytes(&mut mut_buffer, temp); + buffer.append(&mut LittleEndian::string_to_bytes(temp)); } Endianess::PLATFORM => { - PlatformEndian::string_to_bytes(&mut mut_buffer, temp); + buffer.append(&mut PlatformEndian::string_to_bytes(temp)); } } + + // Return the byte buffer. + buffer } fn from_bytes(buffer: &[u8], endianess: Endianess) -> String @@ -410,10 +470,11 @@ impl Transmutable for String impl Transmutable for Vector2 where T: Number + ByteSized + Transmutable { - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + fn to_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. @@ -422,11 +483,14 @@ impl Transmutable for Vector2 where T: Number + ByteSized + Transmutable // Make sure that there is enough space to store // the bytes from this type. - assert!(buffer.len() >= num_bytes); + buffer = Vec::with_capacity(num_bytes); // Convert this to bytes and add it to the buffer. - self.x.to_bytes(&mut buffer[0..byte_size], endianess); - self.y.to_bytes(&mut buffer[byte_size..num_bytes], endianess); + buffer.append(&mut self.x.to_bytes(endianess)); + buffer.append(&mut self.y.to_bytes(endianess)); + + // Return the byte buffer. + buffer } fn from_bytes(buffer: &[u8], endianess: Endianess) -> Vector2 @@ -454,10 +518,11 @@ impl Transmutable for Vector2 where T: Number + ByteSized + Transmutable impl Transmutable for Vector3 where T: Number + ByteSized + Transmutable { - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + fn to_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. @@ -466,12 +531,15 @@ impl Transmutable for Vector3 where T: Number + ByteSized + Transmutable // Make sure that there is enough space to store // the bytes from this type. - assert!(buffer.len() >= num_bytes); + buffer = Vec::with_capacity(num_bytes); // Convert this to bytes and add it to the buffer. - self.x.to_bytes(&mut buffer[0..byte_size], endianess); - self.y.to_bytes(&mut buffer[byte_size..(byte_size*2)], endianess); - self.z.to_bytes(&mut buffer[(byte_size*2)..num_bytes], endianess); + buffer.append(&mut self.x.to_bytes(endianess)); + buffer.append(&mut self.y.to_bytes(endianess)); + buffer.append(&mut self.z.to_bytes(endianess)); + + // Return the byte buffer. + buffer } fn from_bytes(buffer: &[u8], endianess: Endianess) -> Vector3 @@ -500,10 +568,11 @@ impl Transmutable for Vector3 where T: Number + ByteSized + Transmutable impl Transmutable for Vector4 where T: Number + ByteSized + Transmutable { - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + fn to_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. @@ -512,13 +581,16 @@ impl Transmutable for Vector4 where T: Number + ByteSized + Transmutable // Make sure that there is enough space to store // the bytes from this type. - assert!(buffer.len() >= num_bytes); + buffer = Vec::with_capacity(num_bytes); // Convert this to bytes and add it to the buffer. - self.x.to_bytes(&mut buffer[0..byte_size], endianess); - self.y.to_bytes(&mut buffer[byte_size..(byte_size*2)], endianess); - self.z.to_bytes(&mut buffer[(byte_size*2)..(byte_size*3)], endianess); - self.w.to_bytes(&mut buffer[(byte_size*3)..num_bytes], endianess); + buffer.append(&mut self.x.to_bytes(endianess)); + buffer.append(&mut self.y.to_bytes(endianess)); + buffer.append(&mut self.z.to_bytes(endianess)); + buffer.append(&mut self.w.to_bytes(endianess)); + + // Return the byte buffer. + buffer } fn from_bytes(buffer: &[u8], endianess: Endianess) -> Vector4 @@ -549,10 +621,11 @@ impl Transmutable for Vector4 where T: Number + ByteSized + Transmutable impl Transmutable for Quaternion where T: Real + ByteSized + Transmutable { - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + fn to_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. @@ -561,11 +634,14 @@ impl Transmutable for Quaternion // Make sure that there is enough space to store // the bytes from this type. - assert!(buffer.len() >= num_bytes); + buffer = Vec::with_capacity(num_bytes); // Convert this to bytes and add it to the buffer. - self.scalar.to_bytes(&mut buffer[0..byte_size], endianess); - self.vector.to_bytes(&mut buffer[byte_size..num_bytes], endianess); + buffer.append(&mut self.scalar.to_bytes(endianess)); + buffer.append(&mut self.vector.to_bytes(endianess)); + + // Return the byte buffer. + buffer } fn from_bytes(buffer: &[u8], endianess: Endianess) -> Quaternion