Skip to content

Commit

Permalink
Simplify float to/from_bytes and enable tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kaidokert authored and cuviper committed Feb 10, 2023
1 parent e498aff commit b156801
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions src/ops/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,45 +171,38 @@ macro_rules! float_to_from_bytes_impl {
}
}

#[cfg(all(
not(feature = "has_float_to_from_bytes"),
feature = "has_int_to_from_bytes"
))]
#[cfg(not(feature = "has_float_to_from_bytes"))]
impl ToFromBytes for $T {
type Bytes = [u8; $L];

#[inline]
fn to_be_bytes(&self) -> Self::Bytes {
<$I as ToFromBytes>::from_ne_bytes(&self.to_ne_bytes()).to_be_bytes()
<$I as ToFromBytes>::to_be_bytes(&self.to_bits())
}

#[inline]
fn to_le_bytes(&self) -> Self::Bytes {
<$I as ToFromBytes>::from_ne_bytes(&self.to_ne_bytes()).to_le_bytes()
<$I as ToFromBytes>::to_le_bytes(&self.to_bits())
}

#[inline]
fn to_ne_bytes(&self) -> Self::Bytes {
unsafe { transmute(*self) }
<$I as ToFromBytes>::to_ne_bytes(&self.to_bits())
}

#[inline]
fn from_be_bytes(bytes: &Self::Bytes) -> Self {
<Self as ToFromBytes>::from_ne_bytes(
&<$I as ToFromBytes>::from_be_bytes(bytes).to_ne_bytes(),
)
Self::from_bits(<$I as ToFromBytes>::from_be_bytes(&bytes))
}

#[inline]
fn from_le_bytes(bytes: &Self::Bytes) -> Self {
<Self as ToFromBytes>::from_ne_bytes(
&<$I as ToFromBytes>::from_le_bytes(bytes).to_ne_bytes(),
)
Self::from_bits(<$I as ToFromBytes>::from_le_bytes(&bytes))
}

#[inline]
fn from_ne_bytes(bytes: &Self::Bytes) -> Self {
unsafe { transmute(*bytes) }
Self::from_bits(<$I as ToFromBytes>::from_ne_bytes(&bytes))
}
}
};
Expand Down Expand Up @@ -356,7 +349,6 @@ mod tests {
check_to_from_bytes!(i128 u128);
}

#[cfg(feature = "has_float_to_from_bytes")]
#[test]
fn convert_between_float_and_bytes() {
macro_rules! check_to_from_bytes {
Expand Down

0 comments on commit b156801

Please sign in to comment.