Skip to content

Commit e566d77

Browse files
kaidokertcuviper
authored andcommitted
Simplify float to/from_bytes and enable tests
1 parent 2ecd420 commit e566d77

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

src/ops/bytes.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -171,45 +171,38 @@ macro_rules! float_to_from_bytes_impl {
171171
}
172172
}
173173

174-
#[cfg(all(
175-
not(feature = "has_float_to_from_bytes"),
176-
feature = "has_int_to_from_bytes"
177-
))]
174+
#[cfg(not(feature = "has_float_to_from_bytes"))]
178175
impl ToFromBytes for $T {
179176
type Bytes = [u8; $L];
180177

181178
#[inline]
182179
fn to_be_bytes(&self) -> Self::Bytes {
183-
<$I as ToFromBytes>::from_ne_bytes(&self.to_ne_bytes()).to_be_bytes()
180+
<$I as ToFromBytes>::to_be_bytes(&self.to_bits())
184181
}
185182

186183
#[inline]
187184
fn to_le_bytes(&self) -> Self::Bytes {
188-
<$I as ToFromBytes>::from_ne_bytes(&self.to_ne_bytes()).to_le_bytes()
185+
<$I as ToFromBytes>::to_le_bytes(&self.to_bits())
189186
}
190187

191188
#[inline]
192189
fn to_ne_bytes(&self) -> Self::Bytes {
193-
unsafe { transmute(*self) }
190+
<$I as ToFromBytes>::to_ne_bytes(&self.to_bits())
194191
}
195192

196193
#[inline]
197194
fn from_be_bytes(bytes: &Self::Bytes) -> Self {
198-
<Self as ToFromBytes>::from_ne_bytes(
199-
&<$I as ToFromBytes>::from_be_bytes(bytes).to_ne_bytes(),
200-
)
195+
Self::from_bits(<$I as ToFromBytes>::from_be_bytes(&bytes))
201196
}
202197

203198
#[inline]
204199
fn from_le_bytes(bytes: &Self::Bytes) -> Self {
205-
<Self as ToFromBytes>::from_ne_bytes(
206-
&<$I as ToFromBytes>::from_le_bytes(bytes).to_ne_bytes(),
207-
)
200+
Self::from_bits(<$I as ToFromBytes>::from_le_bytes(&bytes))
208201
}
209202

210203
#[inline]
211204
fn from_ne_bytes(bytes: &Self::Bytes) -> Self {
212-
unsafe { transmute(*bytes) }
205+
Self::from_bits(<$I as ToFromBytes>::from_ne_bytes(&bytes))
213206
}
214207
}
215208
};
@@ -356,7 +349,6 @@ mod tests {
356349
check_to_from_bytes!(i128 u128);
357350
}
358351

359-
#[cfg(feature = "has_float_to_from_bytes")]
360352
#[test]
361353
fn convert_between_float_and_bytes() {
362354
macro_rules! check_to_from_bytes {

0 commit comments

Comments
 (0)