Adds the ability to convert a value to the platform or network order.
This handles doing the configure check to see what the platform endianess is for converting byte orders.
This commit is contained in:
parent
4f62caf654
commit
022f6ae87a
@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "alchemy"
|
name = "alchemy"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Jason Travis Smith <Jason@CyberMagesLLC.com>"]
|
authors = ["Jason Travis Smith <Myrddin@CyberMagesLLC.com>"]
|
||||||
description = "Handles converting values to and from a binary format."
|
description = "Handles converting values to and from a binary format."
|
||||||
license = ""
|
license = ""
|
||||||
repository = "https://gitlab.com/CyberMages/alchemy.git"
|
repository = "https://gitlab.com/CyberMages/alchemy.git"
|
||||||
|
@ -4,6 +4,7 @@ use std::ptr::copy_nonoverlapping;
|
|||||||
use ::byte_sized::ByteSized;
|
use ::byte_sized::ByteSized;
|
||||||
use ::byte_sized::{U16_BYTES, U32_BYTES, U64_BYTES};
|
use ::byte_sized::{U16_BYTES, U32_BYTES, U64_BYTES};
|
||||||
use ::converter::Converter;
|
use ::converter::Converter;
|
||||||
|
use ::transmutable::Transmutable;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -30,6 +31,8 @@ pub type PlatformEndian = LittleEndian;
|
|||||||
/// common endian style for networking.
|
/// common endian style for networking.
|
||||||
pub type NetworkEndian = BigEndian;
|
pub type NetworkEndian = BigEndian;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// Handles serialization where the most
|
/// Handles serialization where the most
|
||||||
/// significant byte is stored at the lowest address.
|
/// significant byte is stored at the lowest address.
|
||||||
pub enum BigEndian
|
pub enum BigEndian
|
||||||
@ -276,6 +279,52 @@ 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
|
impl Converter for LittleEndian
|
||||||
{
|
{
|
||||||
fn bytes_to_u16(buffer: &[u8]) -> u16
|
fn bytes_to_u16(buffer: &[u8]) -> u16
|
||||||
|
@ -32,4 +32,5 @@ pub use ::byte_sized::get_byte_size_of_string;
|
|||||||
pub use ::converter::Converter;
|
pub use ::converter::Converter;
|
||||||
pub use ::endian::{BigEndian, LittleEndian, PlatformEndian, NetworkEndian};
|
pub use ::endian::{BigEndian, LittleEndian, PlatformEndian, NetworkEndian};
|
||||||
pub use ::endian::Endianess;
|
pub use ::endian::Endianess;
|
||||||
|
pub use ::endian::{network_to_platform_order, platform_to_network_order};
|
||||||
pub use ::transmutable::Transmutable;
|
pub use ::transmutable::Transmutable;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user