asky/utils/
num_like.rs

1use std::{fmt::Display, str::FromStr};
2
3/// A utility trait to allow only numbers in [`Number`] prompt.
4/// Also allows to custom handle they based on the type.
5///
6/// [`Number`]: crate::Number
7pub trait NumLike: Default + Display + FromStr {
8    /// Check if it is a floating point number.
9    fn is_float() -> bool {
10        false
11    }
12
13    /// Check if it is a signed number.
14    fn is_signed() -> bool {
15        false
16    }
17}
18
19impl NumLike for u8 {}
20impl NumLike for u16 {}
21impl NumLike for u32 {}
22impl NumLike for u64 {}
23impl NumLike for u128 {}
24impl NumLike for usize {}
25
26impl NumLike for i8 {
27    fn is_signed() -> bool {
28        true
29    }
30}
31
32impl NumLike for i16 {
33    fn is_signed() -> bool {
34        true
35    }
36}
37
38impl NumLike for i32 {
39    fn is_signed() -> bool {
40        true
41    }
42}
43
44impl NumLike for i64 {
45    fn is_signed() -> bool {
46        true
47    }
48}
49
50impl NumLike for i128 {
51    fn is_signed() -> bool {
52        true
53    }
54}
55
56impl NumLike for isize {
57    fn is_signed() -> bool {
58        true
59    }
60}
61
62impl NumLike for f32 {
63    fn is_signed() -> bool {
64        true
65    }
66
67    fn is_float() -> bool {
68        true
69    }
70}
71
72impl NumLike for f64 {
73    fn is_signed() -> bool {
74        true
75    }
76
77    fn is_float() -> bool {
78        true
79    }
80}