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
use super::ValueBag;
macro_rules! impl_from_internal {
($($into_ty:ty,)*) => {
$(
impl<'v> From<$into_ty> for ValueBag<'v> {
#[inline]
fn from(value: $into_ty) -> Self {
ValueBag::from_internal(value)
}
}
impl<'a, 'v> From<&'a $into_ty> for ValueBag<'v> {
#[inline]
fn from(value: &'a $into_ty) -> Self {
ValueBag::from_internal(*value)
}
}
)*
};
}
impl_from_internal![
(),
usize,
u8,
u16,
u32,
u64,
isize,
i8,
i16,
i32,
i64,
f32,
f64,
char,
bool,
];
impl<'v> From<&'v str> for ValueBag<'v> {
#[inline]
fn from(value: &'v str) -> Self {
ValueBag::from_internal(value)
}
}
impl<'v> From<&'v u128> for ValueBag<'v> {
#[inline]
fn from(value: &'v u128) -> Self {
ValueBag::from_internal(value)
}
}
impl<'v> From<&'v i128> for ValueBag<'v> {
#[inline]
fn from(value: &'v i128) -> Self {
ValueBag::from_internal(value)
}
}
#[cfg(feature = "std")]
mod std_support {
use super::*;
use crate::std::string::String;
impl<'v> From<&'v String> for ValueBag<'v> {
#[inline]
fn from(v: &'v String) -> ValueBag<'v> {
ValueBag::from_internal(&**v)
}
}
}
#[cfg(test)]
mod tests {
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;
use crate::{
std::{borrow::ToOwned, string::ToString},
test::{IntoValueBag, Token},
};
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_into_display() {
assert_eq!(42u64.into_value_bag().to_string(), "42");
assert_eq!(42i64.into_value_bag().to_string(), "42");
assert_eq!(42.01f64.into_value_bag().to_string(), "42.01");
assert_eq!(true.into_value_bag().to_string(), "true");
assert_eq!('a'.into_value_bag().to_string(), "a");
assert_eq!(
"a loong string".into_value_bag().to_string(),
"a loong string"
);
assert_eq!(().into_value_bag().to_string(), "None");
}
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_into_structured() {
assert_eq!(42u64.into_value_bag().to_token(), Token::U64(42));
assert_eq!(42i64.into_value_bag().to_token(), Token::I64(42));
assert_eq!(42.01f64.into_value_bag().to_token(), Token::F64(42.01));
assert_eq!(true.into_value_bag().to_token(), Token::Bool(true));
assert_eq!('a'.into_value_bag().to_token(), Token::Char('a'));
assert_eq!(
"a loong string".into_value_bag().to_token(),
Token::Str("a loong string".to_owned())
);
assert_eq!(().into_value_bag().to_token(), Token::None);
}
}