lexical_parse_integer/
api.rs

1////! Implements the algorithm in terms of the lexical API.
2
3#![doc(hidden)]
4
5use lexical_util::format::{NumberFormat, STANDARD};
6use lexical_util::{from_lexical, from_lexical_with_options};
7
8use crate::options::{Options, STANDARD as DEFAULT_OPTIONS};
9use crate::parse::ParseInteger;
10
11/// Implement `FromLexical` for numeric type.
12///
13/// Need to inline these, otherwise code generation is sub-optimal.
14/// For some reason, it can't determine some of the const evaluations
15/// can actually be evaluated at compile-time, which causes major branching
16/// issues.
17macro_rules! integer_from_lexical {
18    ($($t:ident $unsigned:ident ; )*) => ($(
19        impl FromLexical for $t {
20            #[cfg_attr(not(feature = "compact"), inline)]
21            fn from_lexical(bytes: &[u8]) -> lexical_util::result::Result<Self>
22            {
23                Self::parse_complete::<STANDARD>(bytes, &DEFAULT_OPTIONS)
24            }
25
26            #[cfg_attr(not(feature = "compact"), inline)]
27            fn from_lexical_partial(
28                bytes: &[u8],
29            ) -> lexical_util::result::Result<(Self, usize)>
30            {
31                Self::parse_partial::<STANDARD>(bytes, &DEFAULT_OPTIONS)
32            }
33        }
34
35        impl FromLexicalWithOptions for $t {
36            type Options = Options;
37
38            #[cfg_attr(not(feature = "compact"), inline)]
39            fn from_lexical_with_options<const FORMAT: u128>(
40                bytes: &[u8],
41                options: &Self::Options,
42            ) -> lexical_util::result::Result<Self>
43            {
44                let format = NumberFormat::<{ FORMAT }> {};
45                if !format.is_valid() {
46                    return Err(format.error());
47                }
48                Self::parse_complete::<FORMAT>(bytes, options)
49            }
50
51            #[cfg_attr(not(feature = "compact"), inline)]
52            fn from_lexical_partial_with_options<const FORMAT: u128>(
53                bytes: &[u8],
54                options: &Self::Options,
55            ) -> lexical_util::result::Result<(Self, usize)>
56            {
57                let format = NumberFormat::<{ FORMAT }> {};
58                if !format.is_valid() {
59                    return Err(format.error());
60                }
61                Self::parse_partial::<FORMAT>(bytes, options)
62            }
63        }
64    )*)
65}
66
67from_lexical! {}
68from_lexical_with_options! {}
69integer_from_lexical! {
70    u8 u8 ;
71    u16 u16 ;
72    u32 u32 ;
73    u64 u64 ;
74    u128 u128 ;
75    usize usize ;
76    i8 u8 ;
77    i16 u16 ;
78    i32 u32 ;
79    i64 u64 ;
80    i128 u128 ;
81    isize usize ;
82}