lexical_parse_integer/
api.rs

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