]> git.proxmox.com Git - cargo.git/blob - vendor/log/src/kv/value/impls.rs
New upstream version 0.37.0
[cargo.git] / vendor / log / src / kv / value / impls.rs
1 use std::fmt;
2
3 use super::{ToValue, Value, Primitive};
4
5 impl ToValue for usize {
6 fn to_value(&self) -> Value {
7 Value::from(*self)
8 }
9 }
10
11 impl<'v> From<usize> for Value<'v> {
12 fn from(value: usize) -> Self {
13 Value::from_primitive(Primitive::Unsigned(value as u64))
14 }
15 }
16
17 impl ToValue for isize {
18 fn to_value(&self) -> Value {
19 Value::from(*self)
20 }
21 }
22
23 impl<'v> From<isize> for Value<'v> {
24 fn from(value: isize) -> Self {
25 Value::from_primitive(Primitive::Signed(value as i64))
26 }
27 }
28
29 impl ToValue for u8 {
30 fn to_value(&self) -> Value {
31 Value::from(*self)
32 }
33 }
34
35 impl<'v> From<u8> for Value<'v> {
36 fn from(value: u8) -> Self {
37 Value::from_primitive(Primitive::Unsigned(value as u64))
38 }
39 }
40
41 impl ToValue for u16 {
42 fn to_value(&self) -> Value {
43 Value::from(*self)
44 }
45 }
46
47 impl<'v> From<u16> for Value<'v> {
48 fn from(value: u16) -> Self {
49 Value::from_primitive(Primitive::Unsigned(value as u64))
50 }
51 }
52
53 impl ToValue for u32 {
54 fn to_value(&self) -> Value {
55 Value::from(*self)
56 }
57 }
58
59 impl<'v> From<u32> for Value<'v> {
60 fn from(value: u32) -> Self {
61 Value::from_primitive(Primitive::Unsigned(value as u64))
62 }
63 }
64
65 impl ToValue for u64 {
66 fn to_value(&self) -> Value {
67 Value::from(*self)
68 }
69 }
70
71 impl<'v> From<u64> for Value<'v> {
72 fn from(value: u64) -> Self {
73 Value::from_primitive(Primitive::Unsigned(value))
74 }
75 }
76
77 impl ToValue for i8 {
78 fn to_value(&self) -> Value {
79 Value::from(*self)
80 }
81 }
82
83 impl<'v> From<i8> for Value<'v> {
84 fn from(value: i8) -> Self {
85 Value::from_primitive(Primitive::Signed(value as i64))
86 }
87 }
88
89 impl ToValue for i16 {
90 fn to_value(&self) -> Value {
91 Value::from(*self)
92 }
93 }
94
95 impl<'v> From<i16> for Value<'v> {
96 fn from(value: i16) -> Self {
97 Value::from_primitive(Primitive::Signed(value as i64))
98 }
99 }
100
101 impl ToValue for i32 {
102 fn to_value(&self) -> Value {
103 Value::from(*self)
104 }
105 }
106
107 impl<'v> From<i32> for Value<'v> {
108 fn from(value: i32) -> Self {
109 Value::from_primitive(Primitive::Signed(value as i64))
110 }
111 }
112
113 impl ToValue for i64 {
114 fn to_value(&self) -> Value {
115 Value::from(*self)
116 }
117 }
118
119 impl<'v> From<i64> for Value<'v> {
120 fn from(value: i64) -> Self {
121 Value::from_primitive(Primitive::Signed(value))
122 }
123 }
124
125 impl ToValue for f32 {
126 fn to_value(&self) -> Value {
127 Value::from(*self)
128 }
129 }
130
131 impl<'v> From<f32> for Value<'v> {
132 fn from(value: f32) -> Self {
133 Value::from_primitive(Primitive::Float(value as f64))
134 }
135 }
136
137 impl ToValue for f64 {
138 fn to_value(&self) -> Value {
139 Value::from(*self)
140 }
141 }
142
143 impl<'v> From<f64> for Value<'v> {
144 fn from(value: f64) -> Self {
145 Value::from_primitive(Primitive::Float(value))
146 }
147 }
148
149 impl ToValue for bool {
150 fn to_value(&self) -> Value {
151 Value::from(*self)
152 }
153 }
154
155 impl<'v> From<bool> for Value<'v> {
156 fn from(value: bool) -> Self {
157 Value::from_primitive(Primitive::Bool(value))
158 }
159 }
160
161 impl ToValue for char {
162 fn to_value(&self) -> Value {
163 Value::from(*self)
164 }
165 }
166
167 impl<'v> From<char> for Value<'v> {
168 fn from(value: char) -> Self {
169 Value::from_primitive(Primitive::Char(value))
170 }
171 }
172
173 impl<'v> ToValue for &'v str {
174 fn to_value(&self) -> Value {
175 Value::from(*self)
176 }
177 }
178
179 impl<'v> From<&'v str> for Value<'v> {
180 fn from(value: &'v str) -> Self {
181 Value::from_primitive(Primitive::Str(value))
182 }
183 }
184
185 impl ToValue for () {
186 fn to_value(&self) -> Value {
187 Value::from_primitive(Primitive::None)
188 }
189 }
190
191 impl<T> ToValue for Option<T>
192 where
193 T: ToValue,
194 {
195 fn to_value(&self) -> Value {
196 match *self {
197 Some(ref value) => value.to_value(),
198 None => Value::from_primitive(Primitive::None),
199 }
200 }
201 }
202
203 impl<'v> ToValue for fmt::Arguments<'v> {
204 fn to_value(&self) -> Value {
205 Value::from_debug(self)
206 }
207 }
208
209 #[cfg(feature = "std")]
210 mod std_support {
211 use super::*;
212
213 use std::borrow::Cow;
214
215 impl<T> ToValue for Box<T>
216 where
217 T: ToValue + ?Sized,
218 {
219 fn to_value(&self) -> Value {
220 (**self).to_value()
221 }
222 }
223
224 impl ToValue for String {
225 fn to_value(&self) -> Value {
226 Value::from_primitive(Primitive::Str(&*self))
227 }
228 }
229
230 impl<'v> ToValue for Cow<'v, str> {
231 fn to_value(&self) -> Value {
232 Value::from_primitive(Primitive::Str(&*self))
233 }
234 }
235 }
236
237 #[cfg(test)]
238 mod tests {
239 use super::*;
240 use kv::value::test::Token;
241
242 #[test]
243 fn test_to_value_display() {
244 assert_eq!(42u64.to_value().to_str_buf(), "42");
245 assert_eq!(42i64.to_value().to_str_buf(), "42");
246 assert_eq!(42.01f64.to_value().to_str_buf(), "42.01");
247 assert_eq!(true.to_value().to_str_buf(), "true");
248 assert_eq!('a'.to_value().to_str_buf(), "'a'");
249 assert_eq!(format_args!("a {}", "value").to_value().to_str_buf(), "a value");
250 assert_eq!("a loong string".to_value().to_str_buf(), "\"a loong string\"");
251 assert_eq!(Some(true).to_value().to_str_buf(), "true");
252 assert_eq!(().to_value().to_str_buf(), "None");
253 assert_eq!(Option::None::<bool>.to_value().to_str_buf(), "None");
254 }
255
256 #[test]
257 fn test_to_value_structured() {
258 assert_eq!(42u64.to_value().to_token(), Token::U64(42));
259 assert_eq!(42i64.to_value().to_token(), Token::I64(42));
260 assert_eq!(42.01f64.to_value().to_token(), Token::F64(42.01));
261 assert_eq!(true.to_value().to_token(), Token::Bool(true));
262 assert_eq!('a'.to_value().to_token(), Token::Char('a'));
263 assert_eq!(format_args!("a {}", "value").to_value().to_token(), Token::Str("a value".into()));
264 assert_eq!("a loong string".to_value().to_token(), Token::Str("a loong string".into()));
265 assert_eq!(Some(true).to_value().to_token(), Token::Bool(true));
266 assert_eq!(().to_value().to_token(), Token::None);
267 assert_eq!(Option::None::<bool>.to_value().to_token(), Token::None);
268 }
269 }