/rust/registry/src/index.crates.io-1949cf8c6b5b557f/lexical-parse-integer-1.0.6/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 | 8.77k | fn from_lexical(bytes: &[u8]) -> lexical_util::result::Result<Self> |
22 | | { |
23 | 8.77k | Self::parse_complete::<STANDARD>(bytes, &DEFAULT_OPTIONS) |
24 | 8.77k | } Unexecuted instantiation: <u8 as lexical_parse_integer::api::FromLexical>::from_lexical Unexecuted instantiation: <u64 as lexical_parse_integer::api::FromLexical>::from_lexical <i32 as lexical_parse_integer::api::FromLexical>::from_lexical Line | Count | Source | 21 | 8.77k | fn from_lexical(bytes: &[u8]) -> lexical_util::result::Result<Self> | 22 | | { | 23 | 8.77k | Self::parse_complete::<STANDARD>(bytes, &DEFAULT_OPTIONS) | 24 | 8.77k | } |
Unexecuted instantiation: <i64 as lexical_parse_integer::api::FromLexical>::from_lexical |
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 | | } |