messagepack_async/value/
int.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std::num::TryFromIntError;

use super::Value;

#[derive(Clone, Debug, PartialEq)]
pub enum Int {
    U8(u8),
    U16(u16),
    U32(u32),
    U64(u64),
    I8(i8),
    I16(i16),
    I32(i32),
    I64(i64),
}

impl Int {
    pub fn as_u64(&self) -> Option<u64> {
        use Int::*;
        match self {
            U8(i) => Some((*i).into()),
            U16(i) => Some((*i).into()),
            U32(i) => Some((*i).into()),
            U64(i) => Some(*i),
            I8(i) => (*i >= 0).then(|| (*i).try_into().unwrap()),
            I16(i) => (*i >= 0).then(|| (*i).try_into().unwrap()),
            I32(i) => (*i >= 0).then(|| (*i).try_into().unwrap()),
            I64(i) => (*i >= 0).then(|| (*i).try_into().unwrap()),
        }
    }
}

impl Value {
    /// Attempt to create a new Value::Int for the supplied value
    /// that will be encoded in the least amount of bytes.
    ///
    /// Positive and zero integers will become an unsigned
    /// variant, while negative integers will become a signed
    /// variant.
    pub fn r#int<T>(value: T) -> Result<Self, TryFromIntError>
    where
        T: Ord + Default + Copy + Clone,
        u8: TryFrom<T>,
        u16: TryFrom<T>,
        u32: TryFrom<T>,
        u64: TryFrom<T, Error = TryFromIntError>,
        i8: TryFrom<T>,
        i16: TryFrom<T>,
        i32: TryFrom<T>,
        i64: TryFrom<T, Error = TryFromIntError>,
    {
        if value >= T::default() {
            Value::unsigned_int(value)
        } else {
            Value::signed_int(value)
        }
    }

    /// Attempt to create a new unsigned Value::Int for the supplied
    /// value that will be encoded in the least amount of bytes.
    pub fn unsigned_int<T>(value: T) -> Result<Self, TryFromIntError>
    where
        T: Copy + Clone,
        u8: TryFrom<T>,
        u16: TryFrom<T>,
        u32: TryFrom<T>,
        u64: TryFrom<T, Error = TryFromIntError>,
    {
        u8::try_from(value)
            .map(Value::from)
            .or_else(|_| u16::try_from(value).map(Value::from))
            .or_else(|_| u32::try_from(value).map(Value::from))
            .or_else(|_| u64::try_from(value).map(Value::from))
    }

    /// Attempt to create a new signed Value::Int for the supplied
    /// value that will be encoded in the least amount of bytes.
    pub fn signed_int<T>(value: T) -> Result<Self, TryFromIntError>
    where
        T: Copy + Clone,
        i8: TryFrom<T>,
        i16: TryFrom<T>,
        i32: TryFrom<T>,
        i64: TryFrom<T, Error = TryFromIntError>,
    {
        i8::try_from(value)
            .map(Value::from)
            .or_else(|_| i16::try_from(value).map(Value::from))
            .or_else(|_| i32::try_from(value).map(Value::from))
            .or_else(|_| i64::try_from(value).map(Value::from))
    }
}

impl From<u8> for Int {
    fn from(value: u8) -> Self {
        Int::U8(value)
    }
}

impl From<u16> for Int {
    fn from(value: u16) -> Self {
        Int::U16(value)
    }
}

impl From<u32> for Int {
    fn from(value: u32) -> Self {
        Int::U32(value)
    }
}

impl From<u64> for Int {
    fn from(value: u64) -> Self {
        Int::U64(value)
    }
}

impl From<i8> for Int {
    fn from(value: i8) -> Self {
        Int::I8(value)
    }
}

impl From<i16> for Int {
    fn from(value: i16) -> Self {
        Int::I16(value)
    }
}

impl From<i32> for Int {
    fn from(value: i32) -> Self {
        Int::I32(value)
    }
}

impl From<i64> for Int {
    fn from(value: i64) -> Self {
        Int::I64(value)
    }
}

impl From<u8> for Value {
    fn from(value: u8) -> Self {
        Value::Int(Int::U8(value))
    }
}

impl From<u16> for Value {
    fn from(value: u16) -> Self {
        Value::Int(Int::U16(value))
    }
}

impl From<u32> for Value {
    fn from(value: u32) -> Self {
        Value::Int(Int::U32(value))
    }
}

impl From<u64> for Value {
    fn from(value: u64) -> Self {
        Value::Int(Int::U64(value))
    }
}

impl From<i8> for Value {
    fn from(value: i8) -> Self {
        Value::Int(Int::I8(value))
    }
}

impl From<i16> for Value {
    fn from(value: i16) -> Self {
        Value::Int(Int::I16(value))
    }
}

impl From<i32> for Value {
    fn from(value: i32) -> Self {
        Value::Int(Int::I32(value))
    }
}

impl From<i64> for Value {
    fn from(value: i64) -> Self {
        Value::Int(Int::I64(value))
    }
}