messagepack_async/value/
int.rs

1use std::num::TryFromIntError;
2
3use super::Value;
4
5#[derive(Clone, Debug, PartialEq)]
6pub enum Int {
7    U8(u8),
8    U16(u16),
9    U32(u32),
10    U64(u64),
11    I8(i8),
12    I16(i16),
13    I32(i32),
14    I64(i64),
15}
16
17impl Int {
18    pub fn as_u64(&self) -> Option<u64> {
19        use Int::*;
20        match self {
21            U8(i) => Some((*i).into()),
22            U16(i) => Some((*i).into()),
23            U32(i) => Some((*i).into()),
24            U64(i) => Some(*i),
25            I8(i) => (*i >= 0).then(|| (*i).try_into().unwrap()),
26            I16(i) => (*i >= 0).then(|| (*i).try_into().unwrap()),
27            I32(i) => (*i >= 0).then(|| (*i).try_into().unwrap()),
28            I64(i) => (*i >= 0).then(|| (*i).try_into().unwrap()),
29        }
30    }
31}
32
33impl Value {
34    /// Attempt to create a new Value::Int for the supplied value
35    /// that will be encoded in the least amount of bytes.
36    ///
37    /// Positive and zero integers will become an unsigned
38    /// variant, while negative integers will become a signed
39    /// variant.
40    pub fn r#int<T>(value: T) -> Result<Self, TryFromIntError>
41    where
42        T: Ord + Default + Copy + Clone,
43        u8: TryFrom<T>,
44        u16: TryFrom<T>,
45        u32: TryFrom<T>,
46        u64: TryFrom<T, Error = TryFromIntError>,
47        i8: TryFrom<T>,
48        i16: TryFrom<T>,
49        i32: TryFrom<T>,
50        i64: TryFrom<T, Error = TryFromIntError>,
51    {
52        if value >= T::default() {
53            Value::unsigned_int(value)
54        } else {
55            Value::signed_int(value)
56        }
57    }
58
59    /// Attempt to create a new unsigned Value::Int for the supplied
60    /// value that will be encoded in the least amount of bytes.
61    pub fn unsigned_int<T>(value: T) -> Result<Self, TryFromIntError>
62    where
63        T: Copy + Clone,
64        u8: TryFrom<T>,
65        u16: TryFrom<T>,
66        u32: TryFrom<T>,
67        u64: TryFrom<T, Error = TryFromIntError>,
68    {
69        u8::try_from(value)
70            .map(Value::from)
71            .or_else(|_| u16::try_from(value).map(Value::from))
72            .or_else(|_| u32::try_from(value).map(Value::from))
73            .or_else(|_| u64::try_from(value).map(Value::from))
74    }
75
76    /// Attempt to create a new signed Value::Int for the supplied
77    /// value that will be encoded in the least amount of bytes.
78    pub fn signed_int<T>(value: T) -> Result<Self, TryFromIntError>
79    where
80        T: Copy + Clone,
81        i8: TryFrom<T>,
82        i16: TryFrom<T>,
83        i32: TryFrom<T>,
84        i64: TryFrom<T, Error = TryFromIntError>,
85    {
86        i8::try_from(value)
87            .map(Value::from)
88            .or_else(|_| i16::try_from(value).map(Value::from))
89            .or_else(|_| i32::try_from(value).map(Value::from))
90            .or_else(|_| i64::try_from(value).map(Value::from))
91    }
92}
93
94impl From<u8> for Int {
95    fn from(value: u8) -> Self {
96        Int::U8(value)
97    }
98}
99
100impl From<u16> for Int {
101    fn from(value: u16) -> Self {
102        Int::U16(value)
103    }
104}
105
106impl From<u32> for Int {
107    fn from(value: u32) -> Self {
108        Int::U32(value)
109    }
110}
111
112impl From<u64> for Int {
113    fn from(value: u64) -> Self {
114        Int::U64(value)
115    }
116}
117
118impl From<i8> for Int {
119    fn from(value: i8) -> Self {
120        Int::I8(value)
121    }
122}
123
124impl From<i16> for Int {
125    fn from(value: i16) -> Self {
126        Int::I16(value)
127    }
128}
129
130impl From<i32> for Int {
131    fn from(value: i32) -> Self {
132        Int::I32(value)
133    }
134}
135
136impl From<i64> for Int {
137    fn from(value: i64) -> Self {
138        Int::I64(value)
139    }
140}
141
142impl From<u8> for Value {
143    fn from(value: u8) -> Self {
144        Value::Int(Int::U8(value))
145    }
146}
147
148impl From<u16> for Value {
149    fn from(value: u16) -> Self {
150        Value::Int(Int::U16(value))
151    }
152}
153
154impl From<u32> for Value {
155    fn from(value: u32) -> Self {
156        Value::Int(Int::U32(value))
157    }
158}
159
160impl From<u64> for Value {
161    fn from(value: u64) -> Self {
162        Value::Int(Int::U64(value))
163    }
164}
165
166impl From<i8> for Value {
167    fn from(value: i8) -> Self {
168        Value::Int(Int::I8(value))
169    }
170}
171
172impl From<i16> for Value {
173    fn from(value: i16) -> Self {
174        Value::Int(Int::I16(value))
175    }
176}
177
178impl From<i32> for Value {
179    fn from(value: i32) -> Self {
180        Value::Int(Int::I32(value))
181    }
182}
183
184impl From<i64> for Value {
185    fn from(value: i64) -> Self {
186        Value::Int(Int::I64(value))
187    }
188}