From e4d821333c269ecb3367f99e4888fef0589eaa5b Mon Sep 17 00:00:00 2001 From: Jason Travis Smith Date: Fri, 17 Jun 2016 04:34:36 -0400 Subject: [PATCH] Adding the concept of NetworkEndian. NetworkEndian describes the byte order to use for network communication. Generally, this is always BigEndian. --- src/endian.rs | 16 +++++++++++++++- src/lib.rs | 3 ++- src/transmutable.rs | 18 +++++++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/endian.rs b/src/endian.rs index 3e38035..a4478dc 100644 --- a/src/endian.rs +++ b/src/endian.rs @@ -22,6 +22,13 @@ pub type PlatformEndian = BigEndian; #[cfg(not(target_endian="big"))] pub type PlatformEndian = LittleEndian; +/// Referes to NetworkEndian. This could be either +/// of the available endians as long as it is consistent +/// across the network. However, RFC1700 decided network +/// byte order would be BigEndian. As such, it is now the +/// common endian style for networking. +pub type NetworkEndian = BigEndian; + /// Handles serialization where the most /// significant byte is stored at the lowest address. pub enum BigEndian @@ -48,7 +55,14 @@ pub enum Endianess /// Referes to PlatformEndian. This can be anyone /// of the other available endians depending on /// the platform you are on. - Platform + Platform, + + /// Referes to NetworkEndian. This could be either + /// of the available endians as long as it is consistent + /// across the network. However, RFC1700 decided network + /// byte order would be BigEndian. As such, it is now the + /// common endian style for networking. + Network } diff --git a/src/lib.rs b/src/lib.rs index 5a16bcf..19d558e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,5 +27,6 @@ pub use ::byte_sized::{I8_BYTES, I16_BYTES, I32_BYTES, I64_BYTES, ISIZE_BYTES}; pub use ::byte_sized::{F32_BYTES, F64_BYTES}; pub use ::byte_sized::get_byte_size_of_string; pub use ::converter::Converter; -pub use ::endian::{BigEndian, LittleEndian, PlatformEndian, Endianess}; +pub use ::endian::{BigEndian, LittleEndian, PlatformEndian, NetworkEndian}; +pub use ::endian::Endianess; pub use ::transmutable::Transmutable; diff --git a/src/transmutable.rs b/src/transmutable.rs index 8cfaa3a..befa01d 100644 --- a/src/transmutable.rs +++ b/src/transmutable.rs @@ -4,7 +4,8 @@ use sigils::quaternion::Quaternion; use ::byte_sized::{ByteSized, get_byte_size_of_string}; use ::converter::Converter; -use ::endian::{BigEndian, LittleEndian, PlatformEndian, Endianess}; +use ::endian::{BigEndian, LittleEndian, PlatformEndian, NetworkEndian}; +use ::endian::Endianess; @@ -49,6 +50,11 @@ macro_rules! handle_endianess_to_bytes { $buffer.append(&mut PlatformEndian::$func(*$val)); } + + Endianess::Network => + { + $buffer.append(&mut NetworkEndian::$func(*$val)); + } } } } @@ -77,6 +83,11 @@ macro_rules! handle_endianess_from_bytes { PlatformEndian::$func(&$buffer) } + + Endianess::Network => + { + NetworkEndian::$func(&$buffer) + } } } } @@ -524,6 +535,11 @@ impl Transmutable for String { buffer.append(&mut PlatformEndian::string_to_bytes(temp)); } + + Endianess::Network => + { + buffer.append(&mut NetworkEndian::string_to_bytes(temp)); + } } // Return the byte buffer.