[go: up one dir, main page]

lexical-core 0.6.5

Lexical, to- and from-string conversion routines.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
//! Wrapped float that behaves like an integer.
//!
//! Comprehensive wrapper for the Float trait that acts like an Integer
//! trait, allowed floats to be mocked as integers.
//! Operations natively supported by floats are wrapped, while
//! those that can be mocked (like overflow-checked operations)
//! are implemented, and others (like bitshift assigns) are unimplemented.

use lib::{cmp, fmt, iter, ops};
use super::cast::*;
use super::config::*;
use super::num::*;
use super::primitive::*;

// WRAPPED FLOAT

/// Wrap a float to act like an integer.
///
/// Required for the lossy atof algorithm.
#[derive(Clone, Copy, Debug, PartialOrd)]
pub(crate) struct WrappedFloat<T: Float>
{
    /// Internal data.
    data: T,
}

impl<T: Float> WrappedFloat<T> {
    /// Wrap existing float.
    #[inline]
    pub fn from_float(t: T) -> Self {
        WrappedFloat { data: t }
    }

    /// Consume wrapper and return float.
    #[inline]
    pub fn into_inner(self) -> T {
        self.data
    }
}

// IMPL AS PRIMITIVE

impl<T: Float> PartialEq for WrappedFloat<T> {
    fn eq(&self, other: &Self) -> bool {
        // Need to return true when both are NaN, since the default
        // PartialEq for floats returns false when both are NaN.
        // We demand total ordering, do it the right way,
        if self.data.is_nan() && other.data.is_nan() {
            true
        } else {
            self.data == other.data
        }
    }
}

impl<T: Float> AsPrimitive for WrappedFloat<T> {
    #[inline]
    fn as_u8(self) -> u8 {
        as_cast(self.data)
    }

    #[inline]
    fn as_u16(self) -> u16 {
        as_cast(self.data)
    }

    #[inline]
    fn as_u32(self) -> u32 {
        as_cast(self.data)
    }

    #[inline]
    fn as_u64(self) -> u64 {
        as_cast(self.data)
    }

    #[inline]
    fn as_u128(self) -> u128 {
        as_cast(self.data)
    }

    #[inline]
    fn as_usize(self) -> usize {
        as_cast(self.data)
    }

    #[inline]
    fn as_i8(self) -> i8 {
        as_cast(self.data)
    }

    #[inline]
    fn as_i16(self) -> i16 {
        as_cast(self.data)
    }

    #[inline]
    fn as_i32(self) -> i32 {
        as_cast(self.data)
    }

    #[inline]
    fn as_i64(self) -> i64 {
        as_cast(self.data)
    }

    #[inline]
    fn as_i128(self) -> i128 {
        as_cast(self.data)
    }

    #[inline]
    fn as_isize(self) -> isize {
        as_cast(self.data)
    }

    #[inline]
    fn as_f32(self) -> f32 {
        as_cast(self.data)
    }

    #[inline]
    fn as_f64(self) -> f64 {
        as_cast(self.data)
    }
}

// IMPL TRY PRIMITIVE

impl<T: Float> TryPrimitive for WrappedFloat<T> {
    #[inline]
    fn try_u8(self) -> Option<u8> {
        try_cast(self.data)
    }

    #[inline]
    fn try_u16(self) -> Option<u16> {
        try_cast(self.data)
    }

    #[inline]
    fn try_u32(self) -> Option<u32> {
        try_cast(self.data)
    }

    #[inline]
    fn try_u64(self) -> Option<u64> {
        try_cast(self.data)
    }

    #[inline]
    fn try_u128(self) -> Option<u128> {
        try_cast(self.data)
    }

    #[inline]
    fn try_usize(self) -> Option<usize> {
        try_cast(self.data)
    }

    #[inline]
    fn try_i8(self) -> Option<i8> {
        try_cast(self.data)
    }

    #[inline]
    fn try_i16(self) -> Option<i16> {
        try_cast(self.data)
    }

    #[inline]
    fn try_i32(self) -> Option<i32> {
        try_cast(self.data)
    }

    #[inline]
    fn try_i64(self) -> Option<i64> {
        try_cast(self.data)
    }

    #[inline]
    fn try_i128(self) -> Option<i128> {
        try_cast(self.data)
    }

    #[inline]
    fn try_isize(self) -> Option<isize> {
        try_cast(self.data)
    }

    #[inline]
    fn try_f32(self) -> Option<f32> {
        try_cast(self.data)
    }

    #[inline]
    fn try_f64(self) -> Option<f64> {
        try_cast(self.data)
    }
}

// IMPL AS CAST

impl<T: Float> AsCast for WrappedFloat<T> {
    #[inline]
    fn as_cast<N: AsPrimitive>(n: N) -> Self {
        // Wrap to wide float and back down. Should be a no-op. Just indirection.
        WrappedFloat { data: as_cast(n.as_f64()) }
    }
}

// IMPL TRY CAST

impl<N: Primitive, T: Float + TryCast<N>> TryCast<N> for WrappedFloat<T> {
    #[inline]
    fn try_cast(self) -> Option<N> {
        try_cast(self.data)
    }
}

// IMPL PRIMITIVE

impl<T: Float> fmt::Display for WrappedFloat<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(&self.data, f)
    }
}

impl<T: Float> Primitive for WrappedFloat<T> {
}

// IMPL NUMBER

impl<T: Float> iter::Product for WrappedFloat<T> {
    #[inline]
    fn product<Iter: Iterator<Item=WrappedFloat<T>>>(iter: Iter) -> Self {
        iter.fold(Self::from_float(T::ONE), ops::Mul::mul)
    }
}

impl<T: Float> iter::Sum for WrappedFloat<T> {
    #[inline]
    fn sum<Iter: Iterator<Item=WrappedFloat<T>>>(iter: Iter) -> Self {
        iter.fold(Self::from_float(T::ZERO), ops::Add::add)
    }
}

/// Implement arithmetic operations.
macro_rules! ops_impl {
    ($($t:ident, $meth:ident ;)*) => ($(
        impl<T: Float> ops::$t for WrappedFloat<T> {
            type Output = Self;

            #[inline]
            fn $meth(self, other: Self) -> Self::Output {
                WrappedFloat { data: self.data.$meth(other.data) }
            }
        }
    )*);
}

ops_impl! {
    Add, add ;
    Div, div ;
    Mul, mul ;
    Rem, rem ;
    Sub, sub ;
}

/// Implement arithmetic assignment operations.
macro_rules! ops_assign_impl {
    ($($t:ident, $meth:ident ;)*) => ($(
        impl<T: Float> ops::$t for WrappedFloat<T> {
            #[inline]
            fn $meth(&mut self, other: Self) {
                self.data.$meth(other.data)
            }
        }
    )*);
}

ops_assign_impl! {
    AddAssign, add_assign ;
    DivAssign, div_assign ;
    MulAssign, mul_assign ;
    RemAssign, rem_assign ;
    SubAssign, sub_assign ;
}

impl<T: Float> Number for WrappedFloat<T> {
    const FORMATTED_SIZE: usize = T::FORMATTED_SIZE;
    const FORMATTED_SIZE_DECIMAL: usize = T::FORMATTED_SIZE_DECIMAL;
    const IS_SIGNED: bool = T::IS_SIGNED;
}

// IMPL INTEGER

impl<T: Float> Ord for WrappedFloat<T> {
    #[inline]
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        // Implements total ordering for a float, while keeping typical
        // behavior. All ordering is preserved, except for NaN,
        // such that if both are NaN, they compare equal.
        // PartialOrd are fails to provide an Ordering if
        // either are NaN, so we just need to provide consistent
        // ordering if either is NaN.
        if let Some(ordering) = self.partial_cmp(&other) {
            ordering
        } else if !self.data.is_nan() {
            cmp::Ordering::Less
        } else if other.data.is_nan() {
            cmp::Ordering::Equal
        } else {
            cmp::Ordering::Greater
        }
    }
}

impl<T: Float> Eq for WrappedFloat<T> {
}

impl<T: Float> fmt::Octal for WrappedFloat<T> {
    #[inline]
    fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
        unimplemented!()
    }
}

impl<T: Float> fmt::LowerHex for WrappedFloat<T> {
    #[inline]
    fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
        unimplemented!()
    }
}

impl<T: Float> fmt::UpperHex for WrappedFloat<T> {
    #[inline]
    fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
        unimplemented!()
    }
}

/// Unimplement bitwise operations.
macro_rules! bitwise_impl {
    ($($t:ident, $meth:ident ;)*) => ($(
        impl<T: Float> ops::$t for WrappedFloat<T> {
            type Output = Self;

            #[inline]
            fn $meth(self, _: Self) -> Self::Output {
                unimplemented!()
            }
        }
    )*);
}

bitwise_impl! {
    BitAnd, bitand ;
    BitOr, bitor ;
    BitXor, bitxor ;
}

/// Unimplement bitwise assignment operations.
macro_rules! bitwise_assign_impl {
    ($($t:ident, $meth:ident ;)*) => ($(
        impl<T: Float> ops::$t for WrappedFloat<T> {
            #[inline]
            fn $meth(&mut self, _: Self) {
                unimplemented!()
            }
        }
    )*);
}

bitwise_assign_impl! {
    BitAndAssign, bitand_assign ;
    BitOrAssign, bitor_assign ;
    BitXorAssign, bitxor_assign ;
}

impl<T: Float> ops::Not for WrappedFloat<T> {
    type Output = Self;

    #[inline]
    fn not(self) -> Self::Output {
        unimplemented!()
    }
}

/// Unimplement bitshift operations.
macro_rules! bitshift_impl {
    ($t:tt, $meth:ident ; $($s:ty)*) => ($(
        // Iterate over all primitives.
        impl<T: Float> ops::$t<$s> for WrappedFloat<T> {
            type Output = Self;

            #[inline]
            fn $meth(self, _: $s) -> Self::Output {
                unimplemented!()
            }
        }
    )*);
    ($($t:ident, $meth:ident ;)*) => ($(
        // Base case, same as self.
        impl<T: Float> ops::$t<> for WrappedFloat<T> {
            type Output = Self;

            #[inline]
            fn $meth(self, _: Self) -> Self::Output {
                unimplemented!()
            }
        }

        bitshift_impl!($t, $meth ; u8 u16 u32 u64 usize i8 i16 i32 i64 isize);
    )*);
}

bitshift_impl! {
    Shl, shl ;
    Shr, shr ;
}

/// Unimplement bitshift assignment operations.
macro_rules! bitshift_assign_impl {
    ($t:tt, $meth:ident ; $($s:ty)*) => ($(
        // Iterate over all primitives.
        impl<T: Float> ops::$t<$s> for WrappedFloat<T> {
            #[inline]
            fn $meth(&mut self, _: $s) {
                unimplemented!()
            }
        }
    )*);
    ($($t:ident, $meth:ident ;)*) => ($(
        // Base case, same as self.
        impl<T: Float> ops::$t<> for WrappedFloat<T> {
            #[inline]
            fn $meth(&mut self, _: Self) {
                unimplemented!()
            }
        }

        bitshift_assign_impl!($t, $meth ; u8 u16 u32 u64 usize i8 i16 i32 i64 isize);
    )*);
}

bitshift_assign_impl! {
    ShlAssign, shl_assign ;
    ShrAssign, shr_assign ;
}

impl<T: Float> Integer for WrappedFloat<T> {
    const ZERO: Self = WrappedFloat { data: T::ZERO };
    const ONE: Self = WrappedFloat { data: T::ONE };
    const TWO: Self = WrappedFloat { data: T::TWO };
    const MAX: Self = WrappedFloat { data: T::MAX };
    const MIN: Self = WrappedFloat { data: T::MIN };
    const BITS: usize = T::BITS;

    #[inline]
    fn max_value() -> Self {
        Self::MAX
    }

    #[inline]
    fn min_value() -> Self {
        Self::MIN
    }

    #[inline]
    fn count_ones(self) -> u32 {
        unreachable!()
    }

    #[inline]
    fn count_zeros(self) -> u32 {
        unreachable!()
    }

    #[inline]
    fn leading_zeros(self) -> u32 {
        unreachable!()
    }

    #[inline]
    fn trailing_zeros(self) -> u32 {
        unreachable!()
    }

    #[inline]
    fn pow(self, _: u32) -> Self {
        unreachable!()
    }

    #[inline]
    fn checked_add(self, i: Self) -> Option<Self> {
        Some(self + i)
    }

    #[inline]
    fn checked_sub(self, i: Self) -> Option<Self> {
        Some(self - i)
    }

    #[inline]
    fn checked_mul(self, i: Self) -> Option<Self> {
        Some(self * i)
    }

    #[inline]
    fn checked_div(self, i: Self) -> Option<Self> {
        Some(self / i)
    }

    #[inline]
    fn checked_rem(self, i: Self) -> Option<Self> {
        Some(self % i)
    }

    #[inline]
    fn checked_neg(self) -> Option<Self> {
        Some(-self)
    }

    #[inline]
    fn checked_shl(self, _: u32) -> Option<Self> {
        unimplemented!()
    }

    #[inline]
    fn checked_shr(self, _: u32) -> Option<Self> {
        unimplemented!()
    }

    #[inline]
    fn wrapping_add(self, i: Self) -> Self {
        self + i
    }

    #[inline]
    fn wrapping_sub(self, i: Self) -> Self {
        self - i
    }

    #[inline]
    fn wrapping_mul(self, i: Self) -> Self {
        self * i
    }

    #[inline]
    fn wrapping_div(self, i: Self) -> Self {
        self / i
    }

    #[inline]
    fn wrapping_rem(self, i: Self) -> Self {
        self % i
    }

    #[inline]
    fn wrapping_neg(self) -> Self {
        -self
    }

    #[inline]
    fn wrapping_shl(self, _: u32) -> Self {
        unimplemented!()
    }

    #[inline]
    fn wrapping_shr(self, _: u32) -> Self {
        unimplemented!()
    }

    #[inline]
    fn overflowing_add(self, i: Self) -> (Self, bool) {
        (self + i, false)
    }

    #[inline]
    fn overflowing_sub(self, i: Self) -> (Self, bool) {
        (self - i, false)
    }

    #[inline]
    fn overflowing_mul(self, i: Self) -> (Self, bool) {
        (self * i, false)
    }

    #[inline]
    fn overflowing_div(self, i: Self) -> (Self, bool) {
        (self / i, false)
    }

    #[inline]
    fn overflowing_rem(self, i: Self) -> (Self, bool) {
        (self % i, false)
    }

    #[inline]
    fn overflowing_neg(self) -> (Self, bool) {
        (-self, false)
    }

    #[inline]
    fn overflowing_shl(self, _: u32) -> (Self, bool) {
        unimplemented!()
    }

    #[inline]
    fn overflowing_shr(self, _: u32) -> (Self, bool) {
        unimplemented!()
    }

    #[inline]
    fn saturating_add(self, i: Self) -> Self {
        self + i
    }

    #[inline]
    fn saturating_sub(self, i: Self) -> Self {
        self - i
    }

    #[inline]
    fn saturating_mul(self, i: Self) -> Self {
        self * i
    }
}

// SIGNED INTEGER

impl<T: Float> ops::Neg for WrappedFloat<T> {
    type Output = Self;

    #[inline]
    fn neg(self) -> Self {
        WrappedFloat { data: -self.data }
    }
}

impl<T: Float> SignedInteger for WrappedFloat<T> {
    fn checked_abs(self) -> Option<Self> {
        Some(self.wrapping_abs())
    }

    fn wrapping_abs(self) -> Self {
        WrappedFloat { data: self.data.abs() }
    }

    fn overflowing_abs(self) -> (Self, bool) {
        (self.wrapping_abs(), false)
    }
}

// TEST
// ----

#[cfg(test)]
mod tests {
    use super::*;

    fn check_integer<T: Integer>(mut x: T) {
        // Copy, partialeq, partialord, ord, eq
        let _ = x;
        assert!(x > T::ONE);
        assert!(x != T::ONE);
        assert_eq!(x.min(T::ONE), T::ONE);
        assert_eq!(x.max(T::ONE), x);

        // Operations
        let _ = x + T::ONE;
        let _ = x - T::ONE;
        let _ = x * T::ONE;
        let _ = x / T::ONE;
        let _ = x % T::ONE;
        x += T::ONE;
        x -= T::ONE;
        x *= T::ONE;
        x /= T::ONE;
        x %= T::ONE;

        // Functions
        assert!(T::ZERO.is_zero());
        assert!(!T::ONE.is_zero());
        assert!(T::ONE.is_one());
        assert!(!T::ZERO.is_one());

        // As cast
        let _: u8 = as_cast(x);
        let _: u16 = as_cast(x);
        let _: u32 = as_cast(x);
        let _: u64 = as_cast(x);
        let _: u128 = as_cast(x);
        let _: usize = as_cast(x);
        let _: i8 = as_cast(x);
        let _: i16 = as_cast(x);
        let _: i32 = as_cast(x);
        let _: i64 = as_cast(x);
        let _: i128 = as_cast(x);
        let _: isize = as_cast(x);
        let _: f32 = as_cast(x);
        let _: f64 = as_cast(x);
    }

    fn check_try_cast_compile<T: Integer>(x: T) {
        // Try cast
        let _: Option<u8> = try_cast(x);
        let _: Option<u16> = try_cast(x);
        let _: Option<u32> = try_cast(x);
        let _: Option<u64> = try_cast(x);
        let _: Option<u128> = try_cast(x);
        let _: Option<usize> = try_cast(x);
        let _: Option<i8> = try_cast(x);
        let _: Option<i16> = try_cast(x);
        let _: Option<i32> = try_cast(x);
        let _: Option<i64> = try_cast(x);
        let _: Option<i128> = try_cast(x);
        let _: Option<isize> = try_cast(x);
        let _: Option<f32> = try_cast(x);
        let _: Option<f64> = try_cast(x);
    }

    #[allow(dead_code)]     // Compile-only
    fn try_cast_test() {
        check_try_cast_compile(WrappedFloat::from_float(65f32));
        check_try_cast_compile(WrappedFloat::from_float(65f64));
    }

    #[test]
    fn integer_test() {
        check_integer(WrappedFloat::from_float(65f32));
        check_integer(WrappedFloat::from_float(65f64));
    }
}