impl<T> NumBytes for T where
T: Debug
+ AsRef<[u8]>
+ AsMut<[u8]>
+ PartialEq
+ Eq
+ PartialOrd
+ Ord
+ Hash
+ Borrow<[u8]>
+ BorrowMut<[u8]>
+ ?Sized
{
}
pubtrait ToBytes { type Bytes: NumBytes;
/// Return the memory representation of this number as a byte array in big-endian byte order. /// /// # Examples /// /// ``` /// use num_traits::ToBytes; /// /// let bytes = ToBytes::to_be_bytes(&0x12345678u32); /// assert_eq!(bytes, [0x12, 0x34, 0x56, 0x78]); /// ``` fn to_be_bytes(&self) -> Self::Bytes;
/// Return the memory representation of this number as a byte array in little-endian byte order. /// /// # Examples /// /// ``` /// use num_traits::ToBytes; /// /// let bytes = ToBytes::to_le_bytes(&0x12345678u32); /// assert_eq!(bytes, [0x78, 0x56, 0x34, 0x12]); /// ``` fn to_le_bytes(&self) -> Self::Bytes;
/// Return the memory representation of this number as a byte array in native byte order. /// /// As the target platform's native endianness is used, /// portable code should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate, instead. /// /// [`to_be_bytes`]: #method.to_be_bytes /// [`to_le_bytes`]: #method.to_le_bytes /// /// # Examples /// /// ``` /// use num_traits::ToBytes; /// /// #[cfg(target_endian = "big")] /// let expected = [0x12, 0x34, 0x56, 0x78]; /// /// #[cfg(target_endian = "little")] /// let expected = [0x78, 0x56, 0x34, 0x12]; /// /// let bytes = ToBytes::to_ne_bytes(&0x12345678u32); /// assert_eq!(bytes, expected) /// ``` fn to_ne_bytes(&self) -> Self::Bytes { #[cfg(target_endian = "big")] let bytes = self.to_be_bytes(); #[cfg(target_endian = "little")] let bytes = self.to_le_bytes();
bytes
}
}
pubtrait FromBytes: Sized { type Bytes: NumBytes + ?Sized;
/// Create a number from its representation as a byte array in big endian. /// /// # Examples /// /// ``` /// use num_traits::FromBytes; /// /// let value: u32 = FromBytes::from_be_bytes(&[0x12, 0x34, 0x56, 0x78]); /// assert_eq!(value, 0x12345678); /// ``` fn from_be_bytes(bytes: &Self::Bytes) -> Self;
/// Create a number from its representation as a byte array in little endian. /// /// # Examples /// /// ``` /// use num_traits::FromBytes; /// /// let value: u32 = FromBytes::from_le_bytes(&[0x78, 0x56, 0x34, 0x12]); /// assert_eq!(value, 0x12345678); /// ``` fn from_le_bytes(bytes: &Self::Bytes) -> Self;
/// Create a number from its memory representation as a byte array in native endianness. /// /// As the target platform's native endianness is used, /// portable code likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as appropriate instead. /// /// [`from_be_bytes`]: #method.from_be_bytes /// [`from_le_bytes`]: #method.from_le_bytes /// /// # Examples /// /// ``` /// use num_traits::FromBytes; /// /// #[cfg(target_endian = "big")] /// let bytes = [0x12, 0x34, 0x56, 0x78]; /// /// #[cfg(target_endian = "little")] /// let bytes = [0x78, 0x56, 0x34, 0x12]; /// /// let value: u32 = FromBytes::from_ne_bytes(&bytes); /// assert_eq!(value, 0x12345678) /// ``` fn from_ne_bytes(bytes: &Self::Bytes) -> Self { #[cfg(target_endian = "big")] let this = Self::from_be_bytes(bytes); #[cfg(target_endian = "little")] let this = Self::from_le_bytes(bytes);
this
}
}
macro_rules! float_to_from_bytes_impl {
($T:ty, $L:expr) => { impl ToBytes for $T { type Bytes = [u8; $L];
macro_rules! check_to_from_bytes {
($( $ty:ty )+) => {$({ let n = 1; let be = <$ty as ToBytes>::to_be_bytes(&n); let le = <$ty as ToBytes>::to_le_bytes(&n); let ne = <$ty as ToBytes>::to_ne_bytes(&n);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.