@@ -4,8 +4,23 @@ use core::fmt::Debug;
4
4
use core:: hash:: Hash ;
5
5
use core:: mem:: transmute;
6
6
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
9
24
+ AsRef < [ u8 ] >
10
25
+ AsMut < [ u8 ] >
11
26
+ PartialEq
@@ -15,14 +30,19 @@ pub trait ToFromBytes {
15
30
+ Hash
16
31
+ Borrow < [ u8 ] >
17
32
+ BorrowMut < [ u8 ] >
18
- + Default ;
33
+ + Default
34
+ {
35
+ }
36
+
37
+ pub trait ToBytes {
38
+ type Bytes : NumBytes ;
19
39
20
40
/// Return the memory representation of this number as a byte array in big-endian byte order.
21
41
///
22
42
/// # Examples
23
43
///
24
44
/// ```
25
- /// use num_traits::ToFromBytes ;
45
+ /// use num_traits::ToBytes ;
26
46
///
27
47
/// # #[cfg(has_int_to_from_bytes)]
28
48
/// # fn main() {
@@ -39,7 +59,7 @@ pub trait ToFromBytes {
39
59
/// # Examples
40
60
///
41
61
/// ```
42
- /// use num_traits::ToFromBytes ;
62
+ /// use num_traits::ToBytes ;
43
63
///
44
64
/// # #[cfg(has_int_to_from_bytes)]
45
65
/// # fn main() {
@@ -62,7 +82,7 @@ pub trait ToFromBytes {
62
82
/// # Examples
63
83
///
64
84
/// ```
65
- /// use num_traits::ToFromBytes ;
85
+ /// use num_traits::ToBytes ;
66
86
///
67
87
/// # #[cfg(has_int_to_from_bytes)]
68
88
/// # fn main() {
@@ -79,15 +99,19 @@ pub trait ToFromBytes {
79
99
/// # fn main() {}
80
100
/// ```
81
101
fn to_ne_bytes ( & self ) -> Self :: Bytes ;
102
+ }
103
+
104
+ pub trait FromBytes {
105
+ type Bytes : NumBytes ;
82
106
83
107
/// Create a number from its representation as a byte array in big endian.
84
108
///
85
109
/// # Examples
86
110
///
87
111
/// ```
88
- /// use num_traits::ToFromBytes ;
112
+ /// use num_traits::FromBytes ;
89
113
///
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]);
91
115
/// assert_eq!(value, 0x12345678);
92
116
/// ```
93
117
fn from_be_bytes ( bytes : & Self :: Bytes ) -> Self ;
@@ -97,9 +121,9 @@ pub trait ToFromBytes {
97
121
/// # Examples
98
122
///
99
123
/// ```
100
- /// use num_traits::ToFromBytes ;
124
+ /// use num_traits::FromBytes ;
101
125
///
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]);
103
127
/// assert_eq!(value, 0x12345678);
104
128
/// ```
105
129
fn from_le_bytes ( bytes : & Self :: Bytes ) -> Self ;
@@ -115,7 +139,7 @@ pub trait ToFromBytes {
115
139
/// # Examples
116
140
///
117
141
/// ```
118
- /// use num_traits::ToFromBytes ;
142
+ /// use num_traits::FromBytes ;
119
143
///
120
144
/// # #[cfg(has_int_to_from_bytes)]
121
145
/// # fn main() {
@@ -125,7 +149,7 @@ pub trait ToFromBytes {
125
149
/// #[cfg(not(target_endian = "big"))]
126
150
/// let bytes = [0x78, 0x56, 0x34, 0x12];
127
151
///
128
- /// let value = <u32 as ToFromBytes >::from_ne_bytes(&bytes);
152
+ /// let value = <u32 as FromBytes >::from_ne_bytes(&bytes);
129
153
/// assert_eq!(value, 0x12345678)
130
154
/// # }
131
155
/// # #[cfg(not(has_int_to_from_bytes))]
@@ -137,7 +161,7 @@ pub trait ToFromBytes {
137
161
macro_rules! float_to_from_bytes_impl {
138
162
( $T: ty, $I: ty, $L: expr) => {
139
163
#[ cfg( feature = "has_float_to_from_bytes" ) ]
140
- impl ToFromBytes for $T {
164
+ impl ToBytes for $T {
141
165
type Bytes = [ u8 ; $L] ;
142
166
143
167
#[ inline]
@@ -154,6 +178,11 @@ macro_rules! float_to_from_bytes_impl {
154
178
fn to_ne_bytes( & self ) -> Self :: Bytes {
155
179
<$T>:: to_ne_bytes( * self )
156
180
}
181
+ }
182
+
183
+ #[ cfg( feature = "has_float_to_from_bytes" ) ]
184
+ impl FromBytes for $T {
185
+ type Bytes = [ u8 ; $L] ;
157
186
158
187
#[ inline]
159
188
fn from_be_bytes( bytes: & Self :: Bytes ) -> Self {
@@ -172,37 +201,42 @@ macro_rules! float_to_from_bytes_impl {
172
201
}
173
202
174
203
#[ cfg( not( feature = "has_float_to_from_bytes" ) ) ]
175
- impl ToFromBytes for $T {
204
+ impl ToBytes for $T {
176
205
type Bytes = [ u8 ; $L] ;
177
206
178
207
#[ inline]
179
208
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( ) )
181
210
}
182
211
183
212
#[ inline]
184
213
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( ) )
186
215
}
187
216
188
217
#[ inline]
189
218
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( ) )
191
220
}
221
+ }
222
+
223
+ #[ cfg( not( feature = "has_float_to_from_bytes" ) ) ]
224
+ impl FromBytes for $T {
225
+ type Bytes = [ u8 ; $L] ;
192
226
193
227
#[ inline]
194
228
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) )
196
230
}
197
231
198
232
#[ inline]
199
233
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) )
201
235
}
202
236
203
237
#[ inline]
204
238
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) )
206
240
}
207
241
}
208
242
} ;
@@ -211,7 +245,7 @@ macro_rules! float_to_from_bytes_impl {
211
245
macro_rules! int_to_from_bytes_impl {
212
246
( $T: ty, $L: expr) => {
213
247
#[ cfg( feature = "has_int_to_from_bytes" ) ]
214
- impl ToFromBytes for $T {
248
+ impl ToBytes for $T {
215
249
type Bytes = [ u8 ; $L] ;
216
250
217
251
#[ inline]
@@ -228,6 +262,11 @@ macro_rules! int_to_from_bytes_impl {
228
262
fn to_ne_bytes( & self ) -> Self :: Bytes {
229
263
<$T>:: to_ne_bytes( * self )
230
264
}
265
+ }
266
+
267
+ #[ cfg( feature = "has_int_to_from_bytes" ) ]
268
+ impl FromBytes for $T {
269
+ type Bytes = [ u8 ; $L] ;
231
270
232
271
#[ inline]
233
272
fn from_be_bytes( bytes: & Self :: Bytes ) -> Self {
@@ -246,32 +285,37 @@ macro_rules! int_to_from_bytes_impl {
246
285
}
247
286
248
287
#[ cfg( not( feature = "has_int_to_from_bytes" ) ) ]
249
- impl ToFromBytes for $T {
288
+ impl ToBytes for $T {
250
289
type Bytes = [ u8 ; $L] ;
251
290
252
291
#[ inline]
253
292
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 ) )
255
294
}
256
295
257
296
#[ inline]
258
297
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 ) )
260
299
}
261
300
262
301
#[ inline]
263
302
fn to_ne_bytes( & self ) -> Self :: Bytes {
264
303
unsafe { transmute( * self ) }
265
304
}
305
+ }
306
+
307
+ #[ cfg( not( feature = "has_int_to_from_bytes" ) ) ]
308
+ impl FromBytes for $T {
309
+ type Bytes = [ u8 ; $L] ;
266
310
267
311
#[ inline]
268
312
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) )
270
314
}
271
315
272
316
#[ inline]
273
317
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) )
275
319
}
276
320
277
321
#[ inline]
@@ -312,9 +356,9 @@ mod tests {
312
356
macro_rules! check_to_from_bytes {
313
357
( $( $ty: ty ) +) => { $( {
314
358
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) ;
318
362
319
363
assert_eq!( * be. last( ) . unwrap( ) , 1 ) ;
320
364
assert_eq!( * le. first( ) . unwrap( ) , 1 ) ;
@@ -324,12 +368,12 @@ mod tests {
324
368
assert_eq!( * ne. first( ) . unwrap( ) , 1 ) ;
325
369
}
326
370
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) ;
329
373
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) ;
331
375
} else {
332
- assert_eq!( <$ty as ToFromBytes >:: from_ne_bytes( & le) , n) ;
376
+ assert_eq!( <$ty as FromBytes >:: from_ne_bytes( & le) , n) ;
333
377
}
334
378
} ) +}
335
379
}
@@ -346,18 +390,18 @@ mod tests {
346
390
( $( $ty: ty ) +) => { $(
347
391
let n: $ty = 3.14 ;
348
392
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) ;
352
396
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) ;
355
399
if cfg!( target_endian = "big" ) {
356
400
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) ;
358
402
} else {
359
403
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) ;
361
405
}
362
406
) +}
363
407
}
0 commit comments