Added platform endianess detection and fixed String byte conversion.

This commit is contained in:
Myrddin Dundragon 2016-12-18 03:20:31 -05:00
parent 022f6ae87a
commit 3b01eed3e0
5 changed files with 231 additions and 46 deletions

View File

@ -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()));

113
examples/convert_u16.rs Normal file
View File

@ -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<u8>;
// 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<u8>;
// 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));
}

View File

@ -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());
}

View File

@ -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<T>(val: T) -> T
where T: Transmutable
{
let buffer: Vec<u8>;
// 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<T>(val: T) -> T
where T: Transmutable
{
let buffer: Vec<u8>;
// 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<T>(val: T) -> T
where T: Transmutable
{
let buffer: Vec<u8>;
// 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<T>(val: T) -> T
where T: Transmutable
{
let buffer: Vec<u8>;
// 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

View File

@ -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;