diff --git a/examples/convert_f32.rs b/examples/convert_f32.rs index 806dda5..f9dce31 100644 --- a/examples/convert_f32.rs +++ b/examples/convert_f32.rs @@ -41,7 +41,7 @@ pub fn use_transmutable() // Initialize the variables. num = 6.291985f32; buffer = Vec::new(); - endianess = Endianess::PLATFORM; + endianess = Endianess::Platform; println!("Converting the value {} into and out of an array of bytes.", num); println!("Buffer starts as: {}", stringify_array(buffer.as_slice())); diff --git a/examples/convert_u16.rs b/examples/convert_u16.rs new file mode 100644 index 0000000..f81c3de --- /dev/null +++ b/examples/convert_u16.rs @@ -0,0 +1,113 @@ +#![feature(convert)] + +extern crate alchemy; + + +use alchemy::U16_BYTES; +use alchemy::{Converter, Endianess, PlatformEndian, Transmutable}; +use alchemy::platform_to_network_order; + + + +fn use_converter() +{ + let num: u16; + let final_num: u16; + let mut buffer: Vec; + + // Initialize the variables. + num = 32832u16; + buffer = Vec::new(); + + println!("Converting the value {} into and out of an array of bytes.", num); + println!("Buffer starts as: {}", stringify_array(buffer.as_slice())); + + // Convert the short point number into an array of bytes. + buffer = PlatformEndian::u16_to_bytes(num); + + println!("Buffer contains: {}", stringify_array(buffer.as_slice())); + + // Convert the array of bytes into a short number. + final_num = PlatformEndian::bytes_to_u16(buffer.as_slice()); + println!("The buffer converts back to: {}", final_num); +} + +pub fn use_transmutable() +{ + let num: u16; + let final_num: u16; + let endianess: Endianess; + let mut buffer: Vec; + + // Initialize the variables. + num = 32832u16; + 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.as_slice())); + + // Convert the short point number into an array of bytes. + buffer = num.to_bytes(endianess); + + println!("Buffer contains: {}", stringify_array(buffer.as_slice())); + + // Convert the array of bytes into a short number. + final_num = u16::from_bytes(buffer.as_slice(), endianess); + println!("The buffer converts back to: {}", final_num); +} + +/// This just help pretty up the printing of an array of bytes. +fn stringify_array(buffer: &[u8]) -> String +{ + let mut result: String; + let mut count: usize; + + // Create a new string that starts with just + // the array opening bracket. + result = String::new(); + result.push_str("["); + + // Loop through the buffer keeping track + // of our place in it. + count = 0usize; + for byte in buffer + { + // Handle priting the last value differently. + if count >= buffer.len() - 1 + { + result.push_str(byte.to_string().as_str()); + } + else + { + result.push_str(byte.to_string().as_str()); + result.push_str(", "); + } + + // Mark that we are going to look at + // the next byte in the array. + count += 1; + } + + // Add the array closing bracket and + // return the new String. + result.push_str("]"); + result +} + + +pub fn main() +{ + println!("Using converter:"); + use_converter(); + + println!(""); + + println!("Using transmutable:"); + use_transmutable(); + + println!(""); + + println!("This turns into a network value of: {}", + platform_to_network_order(32832u16)); +} diff --git a/examples/determine_endianess.rs b/examples/determine_endianess.rs new file mode 100644 index 0000000..1936ccc --- /dev/null +++ b/examples/determine_endianess.rs @@ -0,0 +1,13 @@ +extern crate alchemy; + + + +use alchemy::{get_platform_endianess, get_network_endianess}; + + + +pub fn main() +{ + println!("Platform Endian: {}", get_platform_endianess()); + println!("Network Endian: {}", get_network_endianess()); +} diff --git a/src/endian.rs b/src/endian.rs index 234bcfc..187638a 100644 --- a/src/endian.rs +++ b/src/endian.rs @@ -227,7 +227,7 @@ impl Converter for BigEndian // 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 = BigEndian::bytes_to_u64(buffer); + byte_count = BigEndian::bytes_to_u64(&buffer[0..U64_BYTES]); if byte_count > 0 { @@ -281,50 +281,6 @@ impl Converter for BigEndian -/// Turns a Network order value to a Platform order value. -pub fn network_to_platform_order(val: T) -> T - where T: Transmutable -{ - let buffer: Vec; - - // Determine what endianess the Platform is using. - if cfg!(target_endian="big") - { - // Network endianess is Big endian, so they are the same. - // Just return the value. - val - } - else - { - // Convert the value from Big endian to Little endian. - buffer = val.to_bytes(Endianess::Network); - T::from_bytes(buffer.as_slice(), Endianess::Platform) - } -} - -/// Turns a Platform order value to a Network order value. -pub fn platform_to_network_order(val: T) -> T - where T: Transmutable -{ - let buffer: Vec; - - // Determine what endianess the Platform is using. - if cfg!(target_endian="big") - { - // Network endianess is Big endian, so they are the same. - // Just return the value. - val - } - else - { - // Convert the value from Little endian to Big endian. - buffer = val.to_bytes(Endianess::Platform); - T::from_bytes(buffer.as_slice(), Endianess::Network) - } -} - - - impl Converter for LittleEndian { fn bytes_to_u16(buffer: &[u8]) -> u16 @@ -494,6 +450,108 @@ impl Converter for LittleEndian +impl ::std::fmt::Debug for Endianess +{ + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result + { + ::std::fmt::Display::fmt(self, f) + } +} + +impl ::std::fmt::Display for Endianess +{ + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result + { + match *self + { + Endianess::Big => + { + write!(f, "Big Endian") + } + + Endianess::Little => + { + write!(f, "Little Endian") + } + + Endianess::Network => + { + write!(f, "Network Endian") + } + + Endianess::Platform => + { + write!(f, "Big Endian") + } + } + } +} + + + +/// Turns a Network order value to a Platform order value. +pub fn network_to_platform_order(val: T) -> T + where T: Transmutable +{ + let buffer: Vec; + + // Determine what endianess the Platform is using. + if cfg!(target_endian="big") + { + // Network endianess is Big endian, so they are the same. + // Just return the value. + val + } + else + { + // Convert the value from Big endian to Little endian. + buffer = val.to_bytes(Endianess::Network); + T::from_bytes(buffer.as_slice(), Endianess::Platform) + } +} + +/// Turns a Platform order value to a Network order value. +pub fn platform_to_network_order(val: T) -> T + where T: Transmutable +{ + let buffer: Vec; + + // Determine what endianess the Platform is using. + if cfg!(target_endian="big") + { + // Network endianess is Big endian, so they are the same. + // Just return the value. + val + } + else + { + // Convert the value from Little endian to Big endian. + buffer = val.to_bytes(Endianess::Platform); + T::from_bytes(buffer.as_slice(), Endianess::Network) + } +} + +/// Returns the Endianess used for network transmission. +pub fn get_network_endianess() -> Endianess +{ + Endianess::Big +} + +/// Returns the Endianess of the current platform. +pub fn get_platform_endianess() -> Endianess +{ + if cfg!(target_endian="big") + { + Endianess::Big + } + else + { + Endianess::Little + } +} + + + /// Determine the amount of bytes required to /// represent the given number. fn determine_size(num: u64) -> u8 diff --git a/src/lib.rs b/src/lib.rs index 5de11c6..e726316 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,4 +33,5 @@ pub use ::converter::Converter; pub use ::endian::{BigEndian, LittleEndian, PlatformEndian, NetworkEndian}; pub use ::endian::Endianess; pub use ::endian::{network_to_platform_order, platform_to_network_order}; +pub use ::endian::{get_network_endianess, get_platform_endianess}; pub use ::transmutable::Transmutable;