Added platform endianess detection and fixed String byte conversion.

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

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