/src/rust-lexical/lexical-parse-integer/src/api.rs
Line | Count | Source |
1 | | ////! Implements the algorithm in terms of the lexical API. |
2 | | |
3 | | #![doc(hidden)] |
4 | | |
5 | | use lexical_util::format::{NumberFormat, STANDARD}; |
6 | | use lexical_util::{from_lexical, from_lexical_with_options}; |
7 | | |
8 | | use crate::options::{Options, STANDARD as DEFAULT_OPTIONS}; |
9 | | use 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. |
17 | | macro_rules! integer_from_lexical { |
18 | | ($($t:ident $unsigned:ident ; )*) => ($( |
19 | | impl FromLexical for $t { |
20 | | #[cfg_attr(not(feature = "compact"), inline)] |
21 | 254 | fn from_lexical(bytes: &[u8]) -> lexical_util::result::Result<Self> |
22 | | { |
23 | 254 | Self::parse_complete::<STANDARD>(bytes, &DEFAULT_OPTIONS) |
24 | 254 | } <u64 as lexical_parse_integer::api::FromLexical>::from_lexical Line | Count | Source | 21 | 12 | fn from_lexical(bytes: &[u8]) -> lexical_util::result::Result<Self> | 22 | | { | 23 | 12 | Self::parse_complete::<STANDARD>(bytes, &DEFAULT_OPTIONS) | 24 | 12 | } |
<usize as lexical_parse_integer::api::FromLexical>::from_lexical Line | Count | Source | 21 | 12 | fn from_lexical(bytes: &[u8]) -> lexical_util::result::Result<Self> | 22 | | { | 23 | 12 | Self::parse_complete::<STANDARD>(bytes, &DEFAULT_OPTIONS) | 24 | 12 | } |
<i8 as lexical_parse_integer::api::FromLexical>::from_lexical Line | Count | Source | 21 | 31 | fn from_lexical(bytes: &[u8]) -> lexical_util::result::Result<Self> | 22 | | { | 23 | 31 | Self::parse_complete::<STANDARD>(bytes, &DEFAULT_OPTIONS) | 24 | 31 | } |
<u32 as lexical_parse_integer::api::FromLexical>::from_lexical Line | Count | Source | 21 | 6 | fn from_lexical(bytes: &[u8]) -> lexical_util::result::Result<Self> | 22 | | { | 23 | 6 | Self::parse_complete::<STANDARD>(bytes, &DEFAULT_OPTIONS) | 24 | 6 | } |
<i128 as lexical_parse_integer::api::FromLexical>::from_lexical Line | Count | Source | 21 | 29 | fn from_lexical(bytes: &[u8]) -> lexical_util::result::Result<Self> | 22 | | { | 23 | 29 | Self::parse_complete::<STANDARD>(bytes, &DEFAULT_OPTIONS) | 24 | 29 | } |
<u8 as lexical_parse_integer::api::FromLexical>::from_lexical Line | Count | Source | 21 | 12 | fn from_lexical(bytes: &[u8]) -> lexical_util::result::Result<Self> | 22 | | { | 23 | 12 | Self::parse_complete::<STANDARD>(bytes, &DEFAULT_OPTIONS) | 24 | 12 | } |
<i32 as lexical_parse_integer::api::FromLexical>::from_lexical Line | Count | Source | 21 | 31 | fn from_lexical(bytes: &[u8]) -> lexical_util::result::Result<Self> | 22 | | { | 23 | 31 | Self::parse_complete::<STANDARD>(bytes, &DEFAULT_OPTIONS) | 24 | 31 | } |
<i16 as lexical_parse_integer::api::FromLexical>::from_lexical Line | Count | Source | 21 | 32 | fn from_lexical(bytes: &[u8]) -> lexical_util::result::Result<Self> | 22 | | { | 23 | 32 | Self::parse_complete::<STANDARD>(bytes, &DEFAULT_OPTIONS) | 24 | 32 | } |
<u16 as lexical_parse_integer::api::FromLexical>::from_lexical Line | Count | Source | 21 | 12 | fn from_lexical(bytes: &[u8]) -> lexical_util::result::Result<Self> | 22 | | { | 23 | 12 | Self::parse_complete::<STANDARD>(bytes, &DEFAULT_OPTIONS) | 24 | 12 | } |
<isize as lexical_parse_integer::api::FromLexical>::from_lexical Line | Count | Source | 21 | 32 | fn from_lexical(bytes: &[u8]) -> lexical_util::result::Result<Self> | 22 | | { | 23 | 32 | Self::parse_complete::<STANDARD>(bytes, &DEFAULT_OPTIONS) | 24 | 32 | } |
<i64 as lexical_parse_integer::api::FromLexical>::from_lexical Line | Count | Source | 21 | 29 | fn from_lexical(bytes: &[u8]) -> lexical_util::result::Result<Self> | 22 | | { | 23 | 29 | Self::parse_complete::<STANDARD>(bytes, &DEFAULT_OPTIONS) | 24 | 29 | } |
<u128 as lexical_parse_integer::api::FromLexical>::from_lexical Line | Count | Source | 21 | 16 | fn from_lexical(bytes: &[u8]) -> lexical_util::result::Result<Self> | 22 | | { | 23 | 16 | Self::parse_complete::<STANDARD>(bytes, &DEFAULT_OPTIONS) | 24 | 16 | } |
|
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 | | |
67 | | from_lexical!("lexical_parse_integer", 1234, u64, 4); |
68 | | from_lexical_with_options!("lexical_parse_integer", 1234, u64, 4, Options); |
69 | | integer_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 | | } |