iof/read/impls/
signed.rs

1use crate::impl_read_one_from_for_from_str;
2use std::num::*;
3
4#[cfg(feature = "c-compatible")]
5mod inner {
6    use crate::ext::{Pattern, State};
7
8    #[derive(Debug, Clone, Copy)]
9    pub(super) enum Signed {
10        Initial,
11        Sign,
12        Digits,
13        // Overrun,
14    }
15
16    impl Pattern for Signed {
17        type Item = char;
18
19        #[inline]
20        fn step(&mut self, c: <Self as Pattern>::Item) -> bool {
21            match self {
22                Self::Initial => {
23                    match c {
24                        '+' | '-' => *self = Self::Sign,
25                        _ if c.is_ascii_digit() => *self = Self::Digits,
26                        _ => return false,
27                    }
28                    // if matches!(c, '+' | '-') {
29                    //     *self = Self::Sign;
30                    //     Ok(true)
31                    // } else if c.is_ascii_digit() {
32                    //     *self = Self::Digits;
33                    //     Ok(false)
34                    // } else {
35                    //     Err(PatternError::UnexpectedChar(c))
36                    // }
37                }
38                Self::Sign => {
39                    match c {
40                        _ if c.is_ascii_digit() => *self = Self::Digits,
41                        _ => return false,
42                    }
43                    // if c.is_ascii_digit() {
44                    //     *self = Self::Digits;
45                    //     Ok(false)
46                    // } else {
47                    //     Err(PatternError::UnexpectedChar(c))
48                    // }
49                }
50                Self::Digits => return c.is_ascii_digit(),
51                // Self::Overrun => {}
52            }
53            true
54        }
55
56        #[inline]
57        fn state(&self) -> State {
58            match self {
59                Self::Digits => State::Stoppable,
60                // Self::Overrun => State::Overrun,
61                Self::Initial | Self::Sign => State::Unfulfilled,
62            }
63        }
64    }
65}
66
67#[cfg(feature = "c-compatible")]
68impl_read_one_from_for_from_str!(
69    i8
70    i16
71    i32
72    i64
73    i128
74    isize
75
76    NonZeroI8
77    NonZeroI16
78    NonZeroI32
79    NonZeroI64
80    NonZeroI128
81    NonZeroIsize
82
83    => inner::Signed::Initial
84);
85
86#[cfg(not(feature = "c-compatible"))]
87impl_read_one_from_for_from_str!(
88    i8
89    i16
90    i32
91    i64
92    i128
93    isize
94
95    NonZeroI8
96    NonZeroI16
97    NonZeroI32
98    NonZeroI64
99    NonZeroI128
100    NonZeroIsize
101);