From e7f2f252fabc316b7c9a9728e345fb21b0ad617e Mon Sep 17 00:00:00 2001 From: Jason Travis Smith Date: Tue, 5 Jan 2016 17:46:31 -0500 Subject: [PATCH] Now supporting primitive type conversion with a trait! This changes the old Transmutable trait to Converter and adds a new Transmutable trait to all the primitive number types. --- src/converter.rs | 257 +++++++++++++++++++ src/endian.rs | 24 +- src/lib.rs | 7 +- src/transmutable.rs | 591 +++++++++++++++++++++++++------------------- 4 files changed, 621 insertions(+), 258 deletions(-) create mode 100644 src/converter.rs diff --git a/src/converter.rs b/src/converter.rs new file mode 100644 index 0000000..9f3b658 --- /dev/null +++ b/src/converter.rs @@ -0,0 +1,257 @@ +use std::mem; + + + +/// Describes types that can convert Numbers into bytes, +/// or bytes into Numbers. +pub trait Converter +{ + /// Converts an array of bytes to a signed 16-bit integer. + /// + /// # Panics + /// This will panic if the buffer does not have + /// enough information to convert. + fn bytes_to_i16(buffer: &[u8]) -> i16 + { + Self::bytes_to_u16(buffer) as i16 + } + + /// Converts an array of bytes to a signed 32-bit integer. + /// + /// # Panics + /// This will panic if the buffer does not have + /// enough information to convert. + fn bytes_to_i32(buffer: &[u8]) -> i32 + { + Self::bytes_to_u32(buffer) as i32 + } + + /// Converts an array of bytes to a signed 64-bit integer. + /// + /// # Panics + /// This will panic if the buffer does not have + /// enough information to convert. + fn bytes_to_i64(buffer: &[u8]) -> i64 + { + Self::bytes_to_u64(buffer) as i64 + } + + /// Converts an array of bytes to a signed integer. + /// + /// # Panics + /// This will panic if the buffer does not have + /// enough information to convert. + /// + /// This will panic if the number of bytes + /// passed in is less than one or more than eight. + fn bytes_to_isize(buffer: &[u8]) -> isize + { + let temp_num: u64; + + assert!(buffer.len() >= 1 && buffer.len() <= 8); + + temp_num = Self::bytes_to_usize(buffer) as u64; + add_sign(temp_num, buffer.len() as u8) as isize + } + + /// Converts an array of bytes to a 32-bit floating point number. + /// + /// # Panics + /// This will panic if the buffer does not have + /// enough information to convert. + fn bytes_to_f32(buffer: &[u8]) -> f32 + { + unsafe + { + mem::transmute::(Self::bytes_to_u32(buffer)) + } + } + + /// Converts an array of bytes to a 64-bit floating point number. + /// + /// # Panics + /// This will panic if the buffer does not have + /// enough information to convert. + fn bytes_to_f64(buffer: &[u8]) -> f64 + { + unsafe + { + mem::transmute::(Self::bytes_to_u64(buffer)) + } + } + + + /// Converts a signed 16-bit integer to bytes + /// and places them into the given buffer. + /// + /// # 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) + { + Self::u16_to_bytes(buffer, num as u16) + } + + /// Converts a signed 32-bit integer to bytes + /// and places them into the given buffer. + /// + /// # 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) + { + Self::u32_to_bytes(buffer, num as u32) + } + + /// Converts a signed 64-bit integer to bytes + /// and places them into the given buffer. + /// + /// # 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) + { + Self::u64_to_bytes(buffer, num as u64) + } + + /// Converts a signed integer to bytes + /// and places them into the given buffer. + /// + /// # Panics + /// This will panic if the buffer does not have + /// enough space to store the converted value. + /// + /// 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) + { + 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) + } + + /// Converts a 32-bit floating point number to bytes + /// and places them into the given buffer. + /// + /// # 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) + { + unsafe + { + Self::u32_to_bytes(buffer, mem::transmute::(num)); + } + } + + /// Converts a 64-bit floating point number to bytes + /// and places them into the given buffer. + /// + /// # 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) + { + unsafe + { + Self::u64_to_bytes(buffer, mem::transmute::(num)); + } + } + + + + /// Converts an array of bytes to an unsigned 16-bit integer. + /// + /// # Panics + /// This will panic if the buffer does not have + /// enough information to convert. + fn bytes_to_u16(buffer: &[u8]) -> u16; + + /// Converts an array of bytes to an unsigned 32-bit integer. + /// + /// # Panics + /// This will panic if the buffer does not have + /// enough information to convert. + fn bytes_to_u32(buffer: &[u8]) -> u32; + + /// Converts an array of bytes to an unsigned 64-bit integer. + /// + /// # Panics + /// This will panic if the buffer does not have + /// enough information to convert. + fn bytes_to_u64(buffer: &[u8]) -> u64; + + /// Converts an array of bytes to an unsigned integer. + /// + /// # Panics + /// This will panic if the buffer does not have + /// enough information to convert. + /// + /// This will panic if the number of bytes + /// passed in is less than one or more than eight. + fn bytes_to_usize(buffer: &[u8]) -> usize; + + + /// Converts an unsigned 16-bit integer to bytes + /// and places them into the given buffer. + /// + /// # 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); + + /// Converts an unsigned 32-bit integer to bytes + /// and places them into the given buffer. + /// + /// # 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); + + /// Converts an unsigned 64-bit integer to bytes + /// and places them into the given buffer. + /// + /// # 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); + + /// Converts an unsigned integer to bytes + /// and places them into the given buffer. + /// + /// # Panics + /// This will panic if the buffer does not have + /// enough space to store the converted value. + /// + /// 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); +} + + +/// Switches an unsigned value into its signed equivalent. +/// +/// NOTE: This seems messy for larger unsigned values. +fn add_sign(val: u64, num_bytes: u8) -> i64 +{ + let shift: u8; + + shift = (8 - num_bytes) * 8; + (val << shift) as i64 >> shift +} + +/// Switches a signed value into its unsigned equivalent. +/// +/// NOTE: This seems messy for values under zero. +fn remove_sign(val: i64, num_bytes: u8) -> u64 +{ + let shift: u8; + + shift = (8 - num_bytes) * 8; + (val << shift) as u64 >> shift +} diff --git a/src/endian.rs b/src/endian.rs index 5bc292e..7d878de 100644 --- a/src/endian.rs +++ b/src/endian.rs @@ -1,7 +1,8 @@ use std::mem; use std::ptr::copy_nonoverlapping; -use super::transmutable::Transmutable; +use ::converter::Converter; + /// Handles serialization where the most @@ -27,6 +28,23 @@ pub type PlatformEndian = BigEndian; #[cfg(not(target_endian="big"))] pub type PlatformEndian = LittleEndian; +/// Create an enumeration of the different +/// available endianesses. +pub enum Endianess +{ + /// Referes to BigEndian. + BIG, + + /// Referes to LittleEndian. + LITTLE, + + /// Referes to PlatformEndian. This can be anyone + /// of the other available endians depending on + /// the platform you are on. + PLATFORM +} + + /// Handles reading bytes from a given buffer /// and turning them into the requested type. @@ -75,7 +93,7 @@ macro_rules! write_bytes -impl Transmutable for BigEndian +impl Converter for BigEndian { fn bytes_to_u16(buffer: &[u8]) -> u16 { @@ -147,7 +165,7 @@ impl Transmutable for BigEndian } } -impl Transmutable for LittleEndian +impl Converter for LittleEndian { fn bytes_to_u16(buffer: &[u8]) -> u16 { diff --git a/src/lib.rs b/src/lib.rs index f2ef538..c455a73 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,9 +4,12 @@ extern crate sigils; -mod transmutable; +mod converter; mod endian; +mod transmutable; + +pub use ::converter::Converter; +pub use ::endian::{BigEndian, LittleEndian, PlatformEndian, Endianess}; pub use ::transmutable::Transmutable; -pub use ::endian::{BigEndian, LittleEndian, PlatformEndian}; diff --git a/src/transmutable.rs b/src/transmutable.rs index d1be697..d499e1d 100644 --- a/src/transmutable.rs +++ b/src/transmutable.rs @@ -1,275 +1,360 @@ -use std::mem; +use std::{u8, u16, u32, u64, usize}; +use std::{i8, i16, i32, i64, isize}; + +use ::converter::Converter; +use ::endian::{BigEndian, LittleEndian, PlatformEndian, Endianess}; -/// Describes types that can transmute Numbers into bytes, -/// or bytes into Numbers. This means that they can also -/// transmute higher order mathematic types like Vectors, -/// Quaternions, Matrices, and such. + +/// Handles the repetative +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); + } + + Endianess::LITTLE => + { + LittleEndian::$func(&mut mut_buffer, *$val); + } + + Endianess::PLATFORM => + { + PlatformEndian::$func(&mut mut_buffer, *$val); + } + } + } + } +} + +macro_rules! handle_endianess_from_bytes +{ + ($buffer: ident, $endianess: ident, $func: ident) => + { + match $endianess + { + Endianess::BIG => + { + BigEndian::$func(&$buffer) + } + + Endianess::LITTLE => + { + LittleEndian::$func(&$buffer) + } + + Endianess::PLATFORM => + { + PlatformEndian::$func(&$buffer) + } + } + } +} + + + +// From and Into are not used because we need to also +// know the endianess to use for converting. +/// A type that can be converted to and from bytes. pub trait Transmutable { - /// Converts an array of bytes to a signed 16-bit integer. - /// - /// # Panics - /// This will panic if the buffer does not have - /// enough information to convert. - fn bytes_to_i16(buffer: &[u8]) -> i16 - { - Self::bytes_to_u16(buffer) as i16 - } + /// Transmute this type to an array of bytes. + fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess); - /// Converts an array of bytes to a signed 32-bit integer. - /// - /// # Panics - /// This will panic if the buffer does not have - /// enough information to convert. - fn bytes_to_i32(buffer: &[u8]) -> i32 - { - Self::bytes_to_u32(buffer) as i32 - } - - /// Converts an array of bytes to a signed 64-bit integer. - /// - /// # Panics - /// This will panic if the buffer does not have - /// enough information to convert. - fn bytes_to_i64(buffer: &[u8]) -> i64 - { - Self::bytes_to_u64(buffer) as i64 - } - - /// Converts an array of bytes to a signed integer. - /// - /// # Panics - /// This will panic if the buffer does not have - /// enough information to convert. - /// - /// This will panic if the number of bytes - /// passed in is less than one or more than eight. - fn bytes_to_isize(buffer: &[u8]) -> isize - { - let temp_num: u64; - - assert!(buffer.len() >= 1 && buffer.len() <= 8); - - temp_num = Self::bytes_to_usize(buffer) as u64; - add_sign(temp_num, buffer.len() as u8) as isize - } - - /// Converts an array of bytes to a 32-bit floating point number. - /// - /// # Panics - /// This will panic if the buffer does not have - /// enough information to convert. - fn bytes_to_f32(buffer: &[u8]) -> f32 - { - unsafe - { - mem::transmute::(Self::bytes_to_u32(buffer)) - } - } - - /// Converts an array of bytes to a 64-bit floating point number. - /// - /// # Panics - /// This will panic if the buffer does not have - /// enough information to convert. - fn bytes_to_f64(buffer: &[u8]) -> f64 - { - unsafe - { - mem::transmute::(Self::bytes_to_u64(buffer)) - } - } - - - /// Converts a signed 16-bit integer to bytes - /// and places them into the given buffer. - /// - /// # 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) - { - Self::u16_to_bytes(buffer, num as u16) - } - - /// Converts a signed 32-bit integer to bytes - /// and places them into the given buffer. - /// - /// # 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) - { - Self::u32_to_bytes(buffer, num as u32) - } - - /// Converts a signed 64-bit integer to bytes - /// and places them into the given buffer. - /// - /// # 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) - { - Self::u64_to_bytes(buffer, num as u64) - } - - /// Converts a signed integer to bytes - /// and places them into the given buffer. - /// - /// # Panics - /// This will panic if the buffer does not have - /// enough space to store the converted value. - /// - /// 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) - { - 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) - } - - /// Converts a 32-bit floating point number to bytes - /// and places them into the given buffer. - /// - /// # 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) - { - unsafe - { - Self::u32_to_bytes(buffer, mem::transmute::(num)); - } - } - - /// Converts a 64-bit floating point number to bytes - /// and places them into the given buffer. - /// - /// # 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) - { - unsafe - { - Self::u64_to_bytes(buffer, mem::transmute::(num)); - } - } - - - - /// Converts an array of bytes to an unsigned 16-bit integer. - /// - /// # Panics - /// This will panic if the buffer does not have - /// enough information to convert. - fn bytes_to_u16(buffer: &[u8]) -> u16; - - /// Converts an array of bytes to an unsigned 32-bit integer. - /// - /// # Panics - /// This will panic if the buffer does not have - /// enough information to convert. - fn bytes_to_u32(buffer: &[u8]) -> u32; - - /// Converts an array of bytes to an unsigned 64-bit integer. - /// - /// # Panics - /// This will panic if the buffer does not have - /// enough information to convert. - fn bytes_to_u64(buffer: &[u8]) -> u64; - - /// Converts an array of bytes to an unsigned integer. - /// - /// # Panics - /// This will panic if the buffer does not have - /// enough information to convert. - /// - /// This will panic if the number of bytes - /// passed in is less than one or more than eight. - fn bytes_to_usize(buffer: &[u8]) -> usize; - - - /// Converts an unsigned 16-bit integer to bytes - /// and places them into the given buffer. - /// - /// # 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); - - /// Converts an unsigned 32-bit integer to bytes - /// and places them into the given buffer. - /// - /// # 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); - - /// Converts an unsigned 64-bit integer to bytes - /// and places them into the given buffer. - /// - /// # 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); - - /// Converts an unsigned integer to bytes - /// and places them into the given buffer. - /// - /// # Panics - /// This will panic if the buffer does not have - /// enough space to store the converted value. - /// - /// 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); + /// Transmute an array of bytes to this type. + fn from_bytes(buffer: &[u8], endianess: Endianess) -> Self; } -/// Switches an unsigned value into its signed equivalent. -/// -/// NOTE: This seems messy for larger unsigned values. -fn add_sign(val: u64, num_bytes: u8) -> i64 + +impl Transmutable for u8 { - let shift: u8; + #[allow(unused_variables)] + fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + { + // Make sure that there is enough space to store + // the bytes from this type. + assert!(buffer.len() >= u8::BYTES); - shift = (8 - num_bytes) * 8; - (val << shift) as i64 >> shift + // Convert this to bytes and add it to the buffer. + // Endianess doesn't matter here. + buffer[0] = *self; + } + + #[allow(unused_variables)] + fn from_bytes(buffer: &[u8], endianess: Endianess) -> u8 + { + // Make sure that there is enough data to read + // the bytes for this type. + assert!(buffer.len() >= u8::BYTES); + + // Convert the given bytes to this type and return it. + // Endianess doesn't matter here. + buffer[0] + } } -/// Switches a signed value into its unsigned equivalent. -/// -/// NOTE: This seems messy for values under zero. -fn remove_sign(val: i64, num_bytes: u8) -> u64 +impl Transmutable for u16 { - let shift: u8; + fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + { + // Make sure that there is enough space to store + // the bytes from this type. + assert!(buffer.len() >= u16::BYTES); - shift = (8 - num_bytes) * 8; - (val << shift) as u64 >> shift + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, u16_to_bytes); + } + + fn from_bytes(buffer: &[u8], endianess: Endianess) -> u16 + { + // Make sure that there is enough data to read + // the bytes for this type. + assert!(buffer.len() >= u16::BYTES); + + // Convert the given bytes to this type and return is. + handle_endianess_from_bytes!(buffer, endianess, bytes_to_u16) + } } - - - -pub enum Endianess +impl Transmutable for u32 { - BIG, - LITTLE, - PLATFORM + fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + { + // Make sure that there is enough space to store + // the bytes from this type. + assert!(buffer.len() >= u32::BYTES); + + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, u32_to_bytes); + } + + fn from_bytes(buffer: &[u8], endianess: Endianess) -> u32 + { + // Make sure that there is enough data to read + // the bytes for this type. + assert!(buffer.len() >= u32::BYTES); + + // Convert the given bytes to this type and return is. + handle_endianess_from_bytes!(buffer, endianess, bytes_to_u32) + } } -pub trait fff +impl Transmutable for u64 { - fn to_bytes(&self, buffer: &mut [u8]); + fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + { + // Make sure that there is enough space to store + // the bytes from this type. + assert!(buffer.len() >= u64::BYTES); - fn from_bytes(buffer: &[u8]) -> Self; + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, u64_to_bytes); + } + + fn from_bytes(buffer: &[u8], endianess: Endianess) -> u64 + { + // Make sure that there is enough data to read + // the bytes for this type. + assert!(buffer.len() >= u64::BYTES); + + // Convert the given bytes to this type and return is. + handle_endianess_from_bytes!(buffer, endianess, bytes_to_u64) + } +} + +impl Transmutable for usize +{ + fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + { + // Make sure that there is enough space to store + // the bytes from this type. + assert!(buffer.len() >= usize::BYTES); + + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, usize_to_bytes); + } + + fn from_bytes(buffer: &[u8], endianess: Endianess) -> usize + { + // Make sure that there is enough data to read + // the bytes for this type. + assert!(buffer.len() >= usize::BYTES); + + // Convert the given bytes to this type and return is. + handle_endianess_from_bytes!(buffer, endianess, bytes_to_usize) + } +} + +impl Transmutable for i8 +{ + #[allow(unused_variables)] + fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + { + // Make sure that there is enough space to store + // the bytes from this type. + assert!(buffer.len() >= i8::BYTES); + + // Convert this to bytes and add it to the buffer. + buffer[0] = *self as u8; + } + + #[allow(unused_variables)] + fn from_bytes(buffer: &[u8], endianess: Endianess) -> i8 + { + // Make sure that there is enough data to read + // the bytes for this type. + assert!(buffer.len() >= i8::BYTES); + + // Convert the given bytes to this type and return it. + // Endianess doesn't matter here. + buffer[0] as i8 + } +} + +impl Transmutable for i16 +{ + fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + { + // Make sure that there is enough space to store + // the bytes from this type. + assert!(buffer.len() >= i16::BYTES); + + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, i16_to_bytes); + } + + fn from_bytes(buffer: &[u8], endianess: Endianess) -> i16 + { + // Make sure that there is enough data to read + // the bytes for this type. + assert!(buffer.len() >= i16::BYTES); + + // Convert the given bytes to this type and return is. + handle_endianess_from_bytes!(buffer, endianess, bytes_to_i16) + } +} + +impl Transmutable for i32 +{ + fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + { + // Make sure that there is enough space to store + // the bytes from this type. + assert!(buffer.len() >= i32::BYTES); + + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, i32_to_bytes); + } + + fn from_bytes(buffer: &[u8], endianess: Endianess) -> i32 + { + // Make sure that there is enough data to read + // the bytes for this type. + assert!(buffer.len() >= i32::BYTES); + + // Convert the given bytes to this type and return is. + handle_endianess_from_bytes!(buffer, endianess, bytes_to_i32) + } +} + +impl Transmutable for i64 +{ + fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + { + // Make sure that there is enough space to store + // the bytes from this type. + assert!(buffer.len() >= i64::BYTES); + + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, i64_to_bytes); + } + + fn from_bytes(buffer: &[u8], endianess: Endianess) -> i64 + { + // Make sure that there is enough data to read + // the bytes for this type. + assert!(buffer.len() >= i64::BYTES); + + // Convert the given bytes to this type and return is. + handle_endianess_from_bytes!(buffer, endianess, bytes_to_i64) + } +} + +impl Transmutable for isize +{ + fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + { + // Make sure that there is enough space to store + // the bytes from this type. + assert!(buffer.len() >= isize::BYTES); + + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, isize_to_bytes); + } + + fn from_bytes(buffer: &[u8], endianess: Endianess) -> isize + { + // Make sure that there is enough data to read + // the bytes for this type. + assert!(buffer.len() >= isize::BYTES); + + // Convert the given bytes to this type and return is. + handle_endianess_from_bytes!(buffer, endianess, bytes_to_isize) + } +} + +impl Transmutable for f32 +{ + fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + { + // Make sure that there is enough space to store + // the bytes from this type. + assert!(buffer.len() >= 4); + + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, f32_to_bytes); + } + + fn from_bytes(buffer: &[u8], endianess: Endianess) -> f32 + { + // Make sure that there is enough data to read + // the bytes for this type. + assert!(buffer.len() >= 4); + + // Convert the given bytes to this type and return is. + handle_endianess_from_bytes!(buffer, endianess, bytes_to_f32) + } +} + +impl Transmutable for f64 +{ + fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess) + { + // Make sure that there is enough space to store + // the bytes from this type. + assert!(buffer.len() >= 8); + + // Convert this to bytes and add it to the buffer. + handle_endianess_to_bytes!(buffer, self, endianess, f64_to_bytes); + } + + fn from_bytes(buffer: &[u8], endianess: Endianess) -> f64 + { + // Make sure that there is enough data to read + // the bytes for this type. + assert!(buffer.len() >= 8); + + // Convert the given bytes to this type and return is. + handle_endianess_from_bytes!(buffer, endianess, bytes_to_f64) + } }