Skip to content

Commit 2505954

Browse files
kaidokertcuviper
authored andcommitted
Split ToFrom to separate To and From traits
1 parent 447e05d commit 2505954

File tree

2 files changed

+85
-41
lines changed

2 files changed

+85
-41
lines changed

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub use crate::float::FloatConst;
3535
pub use crate::cast::{cast, AsPrimitive, FromPrimitive, NumCast, ToPrimitive};
3636
pub use crate::identities::{one, zero, One, Zero};
3737
pub use crate::int::PrimInt;
38-
pub use crate::ops::bytes::ToFromBytes;
38+
pub use crate::ops::bytes::{FromBytes, ToBytes};
3939
pub use crate::ops::checked::{
4040
CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl, CheckedShr, CheckedSub,
4141
};

src/ops/bytes.rs

+84-40
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,23 @@ use core::fmt::Debug;
44
use core::hash::Hash;
55
use core::mem::transmute;
66

7-
pub trait ToFromBytes {
8-
type Bytes: Debug
7+
pub trait NumBytes:
8+
Debug
9+
+ AsRef<[u8]>
10+
+ AsMut<[u8]>
11+
+ PartialEq
12+
+ Eq
13+
+ PartialOrd
14+
+ Ord
15+
+ Hash
16+
+ Borrow<[u8]>
17+
+ BorrowMut<[u8]>
18+
+ Default
19+
{
20+
}
21+
22+
impl<T> NumBytes for T where
23+
T: Debug
924
+ AsRef<[u8]>
1025
+ AsMut<[u8]>
1126
+ PartialEq
@@ -15,14 +30,19 @@ pub trait ToFromBytes {
1530
+ Hash
1631
+ Borrow<[u8]>
1732
+ BorrowMut<[u8]>
18-
+ Default;
33+
+ Default
34+
{
35+
}
36+
37+
pub trait ToBytes {
38+
type Bytes: NumBytes;
1939

2040
/// Return the memory representation of this number as a byte array in big-endian byte order.
2141
///
2242
/// # Examples
2343
///
2444
/// ```
25-
/// use num_traits::ToFromBytes;
45+
/// use num_traits::ToBytes;
2646
///
2747
/// # #[cfg(has_int_to_from_bytes)]
2848
/// # fn main() {
@@ -39,7 +59,7 @@ pub trait ToFromBytes {
3959
/// # Examples
4060
///
4161
/// ```
42-
/// use num_traits::ToFromBytes;
62+
/// use num_traits::ToBytes;
4363
///
4464
/// # #[cfg(has_int_to_from_bytes)]
4565
/// # fn main() {
@@ -62,7 +82,7 @@ pub trait ToFromBytes {
6282
/// # Examples
6383
///
6484
/// ```
65-
/// use num_traits::ToFromBytes;
85+
/// use num_traits::ToBytes;
6686
///
6787
/// # #[cfg(has_int_to_from_bytes)]
6888
/// # fn main() {
@@ -79,15 +99,19 @@ pub trait ToFromBytes {
7999
/// # fn main() {}
80100
/// ```
81101
fn to_ne_bytes(&self) -> Self::Bytes;
102+
}
103+
104+
pub trait FromBytes {
105+
type Bytes: NumBytes;
82106

83107
/// Create a number from its representation as a byte array in big endian.
84108
///
85109
/// # Examples
86110
///
87111
/// ```
88-
/// use num_traits::ToFromBytes;
112+
/// use num_traits::FromBytes;
89113
///
90-
/// let value = <u32 as ToFromBytes>::from_be_bytes(&[0x12, 0x34, 0x56, 0x78]);
114+
/// let value = <u32 as FromBytes>::from_be_bytes(&[0x12, 0x34, 0x56, 0x78]);
91115
/// assert_eq!(value, 0x12345678);
92116
/// ```
93117
fn from_be_bytes(bytes: &Self::Bytes) -> Self;
@@ -97,9 +121,9 @@ pub trait ToFromBytes {
97121
/// # Examples
98122
///
99123
/// ```
100-
/// use num_traits::ToFromBytes;
124+
/// use num_traits::FromBytes;
101125
///
102-
/// let value = <u32 as ToFromBytes>::from_le_bytes(&[0x78, 0x56, 0x34, 0x12]);
126+
/// let value = <u32 as FromBytes>::from_le_bytes(&[0x78, 0x56, 0x34, 0x12]);
103127
/// assert_eq!(value, 0x12345678);
104128
/// ```
105129
fn from_le_bytes(bytes: &Self::Bytes) -> Self;
@@ -115,7 +139,7 @@ pub trait ToFromBytes {
115139
/// # Examples
116140
///
117141
/// ```
118-
/// use num_traits::ToFromBytes;
142+
/// use num_traits::FromBytes;
119143
///
120144
/// # #[cfg(has_int_to_from_bytes)]
121145
/// # fn main() {
@@ -125,7 +149,7 @@ pub trait ToFromBytes {
125149
/// #[cfg(not(target_endian = "big"))]
126150
/// let bytes = [0x78, 0x56, 0x34, 0x12];
127151
///
128-
/// let value = <u32 as ToFromBytes>::from_ne_bytes(&bytes);
152+
/// let value = <u32 as FromBytes>::from_ne_bytes(&bytes);
129153
/// assert_eq!(value, 0x12345678)
130154
/// # }
131155
/// # #[cfg(not(has_int_to_from_bytes))]
@@ -137,7 +161,7 @@ pub trait ToFromBytes {
137161
macro_rules! float_to_from_bytes_impl {
138162
($T:ty, $I:ty, $L:expr) => {
139163
#[cfg(feature = "has_float_to_from_bytes")]
140-
impl ToFromBytes for $T {
164+
impl ToBytes for $T {
141165
type Bytes = [u8; $L];
142166

143167
#[inline]
@@ -154,6 +178,11 @@ macro_rules! float_to_from_bytes_impl {
154178
fn to_ne_bytes(&self) -> Self::Bytes {
155179
<$T>::to_ne_bytes(*self)
156180
}
181+
}
182+
183+
#[cfg(feature = "has_float_to_from_bytes")]
184+
impl FromBytes for $T {
185+
type Bytes = [u8; $L];
157186

158187
#[inline]
159188
fn from_be_bytes(bytes: &Self::Bytes) -> Self {
@@ -172,37 +201,42 @@ macro_rules! float_to_from_bytes_impl {
172201
}
173202

174203
#[cfg(not(feature = "has_float_to_from_bytes"))]
175-
impl ToFromBytes for $T {
204+
impl ToBytes for $T {
176205
type Bytes = [u8; $L];
177206

178207
#[inline]
179208
fn to_be_bytes(&self) -> Self::Bytes {
180-
<$I as ToFromBytes>::to_be_bytes(&self.to_bits())
209+
<$I as ToBytes>::to_be_bytes(&self.to_bits())
181210
}
182211

183212
#[inline]
184213
fn to_le_bytes(&self) -> Self::Bytes {
185-
<$I as ToFromBytes>::to_le_bytes(&self.to_bits())
214+
<$I as ToBytes>::to_le_bytes(&self.to_bits())
186215
}
187216

188217
#[inline]
189218
fn to_ne_bytes(&self) -> Self::Bytes {
190-
<$I as ToFromBytes>::to_ne_bytes(&self.to_bits())
219+
<$I as ToBytes>::to_ne_bytes(&self.to_bits())
191220
}
221+
}
222+
223+
#[cfg(not(feature = "has_float_to_from_bytes"))]
224+
impl FromBytes for $T {
225+
type Bytes = [u8; $L];
192226

193227
#[inline]
194228
fn from_be_bytes(bytes: &Self::Bytes) -> Self {
195-
Self::from_bits(<$I as ToFromBytes>::from_be_bytes(&bytes))
229+
Self::from_bits(<$I as FromBytes>::from_be_bytes(&bytes))
196230
}
197231

198232
#[inline]
199233
fn from_le_bytes(bytes: &Self::Bytes) -> Self {
200-
Self::from_bits(<$I as ToFromBytes>::from_le_bytes(&bytes))
234+
Self::from_bits(<$I as FromBytes>::from_le_bytes(&bytes))
201235
}
202236

203237
#[inline]
204238
fn from_ne_bytes(bytes: &Self::Bytes) -> Self {
205-
Self::from_bits(<$I as ToFromBytes>::from_ne_bytes(&bytes))
239+
Self::from_bits(<$I as FromBytes>::from_ne_bytes(&bytes))
206240
}
207241
}
208242
};
@@ -211,7 +245,7 @@ macro_rules! float_to_from_bytes_impl {
211245
macro_rules! int_to_from_bytes_impl {
212246
($T:ty, $L:expr) => {
213247
#[cfg(feature = "has_int_to_from_bytes")]
214-
impl ToFromBytes for $T {
248+
impl ToBytes for $T {
215249
type Bytes = [u8; $L];
216250

217251
#[inline]
@@ -228,6 +262,11 @@ macro_rules! int_to_from_bytes_impl {
228262
fn to_ne_bytes(&self) -> Self::Bytes {
229263
<$T>::to_ne_bytes(*self)
230264
}
265+
}
266+
267+
#[cfg(feature = "has_int_to_from_bytes")]
268+
impl FromBytes for $T {
269+
type Bytes = [u8; $L];
231270

232271
#[inline]
233272
fn from_be_bytes(bytes: &Self::Bytes) -> Self {
@@ -246,32 +285,37 @@ macro_rules! int_to_from_bytes_impl {
246285
}
247286

248287
#[cfg(not(feature = "has_int_to_from_bytes"))]
249-
impl ToFromBytes for $T {
288+
impl ToBytes for $T {
250289
type Bytes = [u8; $L];
251290

252291
#[inline]
253292
fn to_be_bytes(&self) -> Self::Bytes {
254-
<$T as ToFromBytes>::to_ne_bytes(&<$T>::to_be(*self))
293+
<$T as ToBytes>::to_ne_bytes(&<$T>::to_be(*self))
255294
}
256295

257296
#[inline]
258297
fn to_le_bytes(&self) -> Self::Bytes {
259-
<$T as ToFromBytes>::to_ne_bytes(&<$T>::to_le(*self))
298+
<$T as ToBytes>::to_ne_bytes(&<$T>::to_le(*self))
260299
}
261300

262301
#[inline]
263302
fn to_ne_bytes(&self) -> Self::Bytes {
264303
unsafe { transmute(*self) }
265304
}
305+
}
306+
307+
#[cfg(not(feature = "has_int_to_from_bytes"))]
308+
impl FromBytes for $T {
309+
type Bytes = [u8; $L];
266310

267311
#[inline]
268312
fn from_be_bytes(bytes: &Self::Bytes) -> Self {
269-
Self::from_be(<Self as ToFromBytes>::from_ne_bytes(bytes))
313+
Self::from_be(<Self as FromBytes>::from_ne_bytes(bytes))
270314
}
271315

272316
#[inline]
273317
fn from_le_bytes(bytes: &Self::Bytes) -> Self {
274-
Self::from_le(<Self as ToFromBytes>::from_ne_bytes(bytes))
318+
Self::from_le(<Self as FromBytes>::from_ne_bytes(bytes))
275319
}
276320

277321
#[inline]
@@ -312,9 +356,9 @@ mod tests {
312356
macro_rules! check_to_from_bytes {
313357
($( $ty:ty )+) => {$({
314358
let n = 1;
315-
let be = <$ty as ToFromBytes>::to_be_bytes(&n);
316-
let le = <$ty as ToFromBytes>::to_le_bytes(&n);
317-
let ne = <$ty as ToFromBytes>::to_ne_bytes(&n);
359+
let be = <$ty as ToBytes>::to_be_bytes(&n);
360+
let le = <$ty as ToBytes>::to_le_bytes(&n);
361+
let ne = <$ty as ToBytes>::to_ne_bytes(&n);
318362

319363
assert_eq!(*be.last().unwrap(), 1);
320364
assert_eq!(*le.first().unwrap(), 1);
@@ -324,12 +368,12 @@ mod tests {
324368
assert_eq!(*ne.first().unwrap(), 1);
325369
}
326370

327-
assert_eq!(<$ty as ToFromBytes>::from_be_bytes(&be), n);
328-
assert_eq!(<$ty as ToFromBytes>::from_le_bytes(&le), n);
371+
assert_eq!(<$ty as FromBytes>::from_be_bytes(&be), n);
372+
assert_eq!(<$ty as FromBytes>::from_le_bytes(&le), n);
329373
if cfg!(target_endian = "big") {
330-
assert_eq!(<$ty as ToFromBytes>::from_ne_bytes(&be), n);
374+
assert_eq!(<$ty as FromBytes>::from_ne_bytes(&be), n);
331375
} else {
332-
assert_eq!(<$ty as ToFromBytes>::from_ne_bytes(&le), n);
376+
assert_eq!(<$ty as FromBytes>::from_ne_bytes(&le), n);
333377
}
334378
})+}
335379
}
@@ -346,18 +390,18 @@ mod tests {
346390
($( $ty:ty )+) => {$(
347391
let n: $ty = 3.14;
348392

349-
let be = <$ty as ToFromBytes>::to_be_bytes(&n);
350-
let le = <$ty as ToFromBytes>::to_le_bytes(&n);
351-
let ne = <$ty as ToFromBytes>::to_ne_bytes(&n);
393+
let be = <$ty as ToBytes>::to_be_bytes(&n);
394+
let le = <$ty as ToBytes>::to_le_bytes(&n);
395+
let ne = <$ty as ToBytes>::to_ne_bytes(&n);
352396

353-
assert_eq!(<$ty as ToFromBytes>::from_be_bytes(&be), n);
354-
assert_eq!(<$ty as ToFromBytes>::from_le_bytes(&le), n);
397+
assert_eq!(<$ty as FromBytes>::from_be_bytes(&be), n);
398+
assert_eq!(<$ty as FromBytes>::from_le_bytes(&le), n);
355399
if cfg!(target_endian = "big") {
356400
assert_eq!(ne, be);
357-
assert_eq!(<$ty as ToFromBytes>::from_ne_bytes(&be), n);
401+
assert_eq!(<$ty as FromBytes>::from_ne_bytes(&be), n);
358402
} else {
359403
assert_eq!(ne, le);
360-
assert_eq!(<$ty as ToFromBytes>::from_ne_bytes(&le), n);
404+
assert_eq!(<$ty as FromBytes>::from_ne_bytes(&le), n);
361405
}
362406
)+}
363407
}

0 commit comments

Comments
 (0)