10
10
11
11
use std;
12
12
use std:: { io, mem} ;
13
- use std:: num:: Int ;
14
13
use std:: ptr;
15
14
use std:: slice:: bytes:: { MutableByteVector , copy_memory} ;
16
15
@@ -101,7 +100,7 @@ pub fn read_u64v_be(dst: &mut[u64], input: &[u8]) {
101
100
for _ in ( 0 ..dst. len ( ) ) {
102
101
let mut tmp: u64 = mem:: uninitialized ( ) ;
103
102
ptr:: copy_nonoverlapping ( y, & mut tmp as * mut _ as * mut u8 , 8 ) ;
104
- * x = Int :: from_be ( tmp) ;
103
+ * x = u64 :: from_be ( tmp) ;
105
104
x = x. offset ( 1 ) ;
106
105
y = y. offset ( 8 ) ;
107
106
}
@@ -117,7 +116,7 @@ pub fn read_u64v_le(dst: &mut[u64], input: &[u8]) {
117
116
for _ in ( 0 ..dst. len ( ) ) {
118
117
let mut tmp: u64 = mem:: uninitialized ( ) ;
119
118
ptr:: copy_nonoverlapping ( y, & mut tmp as * mut _ as * mut u8 , 8 ) ;
120
- * x = Int :: from_le ( tmp) ;
119
+ * x = u64 :: from_le ( tmp) ;
121
120
x = x. offset ( 1 ) ;
122
121
y = y. offset ( 8 ) ;
123
122
}
@@ -133,7 +132,7 @@ pub fn read_u32v_be(dst: &mut[u32], input: &[u8]) {
133
132
for _ in ( 0 ..dst. len ( ) ) {
134
133
let mut tmp: u32 = mem:: uninitialized ( ) ;
135
134
ptr:: copy_nonoverlapping ( y, & mut tmp as * mut _ as * mut u8 , 4 ) ;
136
- * x = Int :: from_be ( tmp) ;
135
+ * x = u32 :: from_be ( tmp) ;
137
136
x = x. offset ( 1 ) ;
138
137
y = y. offset ( 4 ) ;
139
138
}
@@ -149,7 +148,7 @@ pub fn read_u32v_le(dst: &mut[u32], input: &[u8]) {
149
148
for _ in ( 0 ..dst. len ( ) ) {
150
149
let mut tmp: u32 = mem:: uninitialized ( ) ;
151
150
ptr:: copy_nonoverlapping ( y, & mut tmp as * mut _ as * mut u8 , 4 ) ;
152
- * x = Int :: from_le ( tmp) ;
151
+ * x = u32 :: from_le ( tmp) ;
153
152
x = x. offset ( 1 ) ;
154
153
y = y. offset ( 4 ) ;
155
154
}
@@ -162,7 +161,7 @@ pub fn read_u32_le(input: &[u8]) -> u32 {
162
161
unsafe {
163
162
let mut tmp: u32 = mem:: uninitialized ( ) ;
164
163
ptr:: copy_nonoverlapping ( input. get_unchecked ( 0 ) , & mut tmp as * mut _ as * mut u8 , 4 ) ;
165
- Int :: from_le ( tmp)
164
+ u32 :: from_le ( tmp)
166
165
}
167
166
}
168
167
@@ -172,7 +171,7 @@ pub fn read_u32_be(input: &[u8]) -> u32 {
172
171
unsafe {
173
172
let mut tmp: u32 = mem:: uninitialized ( ) ;
174
173
ptr:: copy_nonoverlapping ( input. get_unchecked ( 0 ) , & mut tmp as * mut _ as * mut u8 , 4 ) ;
175
- Int :: from_be ( tmp)
174
+ u32 :: from_be ( tmp)
176
175
}
177
176
}
178
177
@@ -243,24 +242,18 @@ pub fn symm_enc_or_dec<S: SynchronousStreamCipher, R: ReadBuffer, W: WriteBuffer
243
242
}
244
243
}
245
244
246
- pub trait ToBits {
247
- /// Convert the value in bytes to the number of bits, a tuple where the 1st item is the
248
- /// high-order value and the 2nd item is the low order value.
249
- fn to_bits ( self ) -> ( Self , Self ) ;
250
- }
251
-
252
- impl ToBits for u64 {
253
- fn to_bits ( self ) -> ( u64 , u64 ) {
254
- ( self >> 61 , self << 3 )
255
- }
245
+ /// Convert the value in bytes to the number of bits, a tuple where the 1st item is the
246
+ /// high-order value and the 2nd item is the low order value.
247
+ fn to_bits ( x : u64 ) -> ( u64 , u64 ) {
248
+ ( x >> 61 , x << 3 )
256
249
}
257
250
258
251
/// Adds the specified number of bytes to the bit count. panic!() if this would cause numeric
259
252
/// overflow.
260
- pub fn add_bytes_to_bits < T : Int + ToBits > ( bits : T , bytes : T ) -> T {
261
- let ( new_high_bits, new_low_bits) = bytes . to_bits ( ) ;
253
+ pub fn add_bytes_to_bits ( bits : u64 , bytes : u64 ) -> u64 {
254
+ let ( new_high_bits, new_low_bits) = to_bits ( bytes ) ;
262
255
263
- if new_high_bits > Int :: zero ( ) {
256
+ if new_high_bits > 0 {
264
257
panic ! ( "Numeric overflow occured." )
265
258
}
266
259
@@ -270,17 +263,16 @@ pub fn add_bytes_to_bits<T: Int + ToBits>(bits: T, bytes: T) -> T {
270
263
/// Adds the specified number of bytes to the bit count, which is a tuple where the first element is
271
264
/// the high order value. panic!() if this would cause numeric overflow.
272
265
pub fn add_bytes_to_bits_tuple
273
- < T : Int + ToBits >
274
- ( bits : ( T , T ) , bytes : T ) -> ( T , T ) {
275
- let ( new_high_bits, new_low_bits) = bytes. to_bits ( ) ;
266
+ ( bits : ( u64 , u64 ) , bytes : u64 ) -> ( u64 , u64 ) {
267
+ let ( new_high_bits, new_low_bits) = to_bits ( bytes) ;
276
268
let ( hi, low) = bits;
277
269
278
270
// Add the low order value - if there is no overflow, then add the high order values
279
271
// If the addition of the low order values causes overflow, add one to the high order values
280
272
// before adding them.
281
273
match low. checked_add ( new_low_bits) {
282
274
Some ( x) => {
283
- if new_high_bits == Int :: zero ( ) {
275
+ if new_high_bits == 0 {
284
276
// This is the fast path - every other alternative will rarely occur in practice
285
277
// considering how large an input would need to be for those paths to be used.
286
278
return ( hi, x) ;
@@ -292,8 +284,7 @@ pub fn add_bytes_to_bits_tuple
292
284
}
293
285
} ,
294
286
None => {
295
- let one: T = Int :: one ( ) ;
296
- let z = match new_high_bits. checked_add ( one) {
287
+ let z = match new_high_bits. checked_add ( 1 ) {
297
288
Some ( w) => w,
298
289
None => panic ! ( "Numeric overflow occured." )
299
290
} ;
@@ -496,8 +487,8 @@ impl <T: FixedBuffer> StandardPadding for T {
496
487
497
488
#[ cfg( test) ]
498
489
pub mod test {
490
+ use std;
499
491
use std:: iter:: repeat;
500
- use std:: num:: Int ;
501
492
502
493
use rand:: IsaacRng ;
503
494
use rand:: distributions:: { IndependentSample , Range } ;
@@ -532,47 +523,47 @@ pub mod test {
532
523
// A normal addition - no overflow occurs
533
524
#[ test]
534
525
fn test_add_bytes_to_bits_ok ( ) {
535
- assert ! ( add_bytes_to_bits:: < u64 > ( 100 , 10 ) == 180 ) ;
526
+ assert ! ( add_bytes_to_bits( 100 , 10 ) == 180 ) ;
536
527
}
537
528
538
529
// A simple failure case - adding 1 to the max value
539
530
#[ test]
540
531
#[ should_panic]
541
532
fn test_add_bytes_to_bits_overflow ( ) {
542
- add_bytes_to_bits :: < u64 > ( Int :: max_value ( ) , 1 ) ;
533
+ add_bytes_to_bits ( std :: u64:: MAX , 1 ) ;
543
534
}
544
535
545
536
// A normal addition - no overflow occurs (fast path)
546
537
#[ test]
547
538
fn test_add_bytes_to_bits_tuple_ok ( ) {
548
- assert ! ( add_bytes_to_bits_tuple:: < u64 > ( ( 5 , 100 ) , 10 ) == ( 5 , 180 ) ) ;
539
+ assert ! ( add_bytes_to_bits_tuple( ( 5 , 100 ) , 10 ) == ( 5 , 180 ) ) ;
549
540
}
550
541
551
542
// The low order value overflows into the high order value
552
543
#[ test]
553
544
fn test_add_bytes_to_bits_tuple_ok2 ( ) {
554
- assert ! ( add_bytes_to_bits_tuple:: < u64 > ( ( 5 , Int :: max_value ( ) ) , 1 ) == ( 6 , 7 ) ) ;
545
+ assert ! ( add_bytes_to_bits_tuple( ( 5 , std :: u64 :: MAX ) , 1 ) == ( 6 , 7 ) ) ;
555
546
}
556
547
557
548
// The value to add is too large to be converted into bits without overflowing its type
558
549
#[ test]
559
550
fn test_add_bytes_to_bits_tuple_ok3 ( ) {
560
- assert ! ( add_bytes_to_bits_tuple:: < u64 > ( ( 5 , 0 ) , 0x4000000000000001 ) == ( 7 , 8 ) ) ;
551
+ assert ! ( add_bytes_to_bits_tuple( ( 5 , 0 ) , 0x4000000000000001 ) == ( 7 , 8 ) ) ;
561
552
}
562
553
563
554
// A simple failure case - adding 1 to the max value
564
555
#[ test]
565
556
#[ should_panic]
566
557
fn test_add_bytes_to_bits_tuple_overflow ( ) {
567
- add_bytes_to_bits_tuple :: < u64 > ( ( Int :: max_value ( ) , Int :: max_value ( ) ) , 1 ) ;
558
+ add_bytes_to_bits_tuple ( ( std :: u64:: MAX , std :: u64 :: MAX ) , 1 ) ;
568
559
}
569
560
570
561
// The value to add is too large to convert to bytes without overflowing its type, but the high
571
562
// order value from this conversion overflows when added to the existing high order value
572
563
#[ test]
573
564
#[ should_panic]
574
565
fn test_add_bytes_to_bits_tuple_overflow2 ( ) {
575
- let value: u64 = Int :: max_value ( ) ;
576
- add_bytes_to_bits_tuple :: < u64 > ( ( value - 1 , 0 ) , 0x8000000000000000 ) ;
566
+ let value: u64 = std :: u64 :: MAX ;
567
+ add_bytes_to_bits_tuple ( ( value - 1 , 0 ) , 0x8000000000000000 ) ;
577
568
}
578
569
}
0 commit comments