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
use std::fmt;
use std::ops::Deref;
#[derive(Clone, Debug)]
pub struct Value(String);
impl Deref for Value {
type Target = str;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl fmt::Display for Value {
#[inline]
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(formatter)
}
}
impl From<Value> for String {
#[inline]
fn from(Value(inner): Value) -> Self {
inner
}
}
macro_rules! implement {
($($primitive:ty,)*) => (
$(impl From<$primitive> for Value {
#[inline]
fn from(inner: $primitive) -> Self {
Value(inner.to_string())
}
})*
);
}
implement! {
i8, i16, i32, i64, isize,
u8, u16, u32, u64, usize,
f32, f64,
String,
bool,
}
impl<'l> From<&'l str> for Value {
#[inline]
fn from(inner: &'l str) -> Value {
Value(inner.to_string())
}
}
impl<T> From<Vec<T>> for Value
where
T: Into<Value>,
{
fn from(mut inner: Vec<T>) -> Self {
Value(
inner
.drain(..)
.map(|value| value.into().0)
.collect::<Vec<_>>()
.join(" "),
)
}
}
macro_rules! implement {
(@express $e:expr) => ($e);
($pattern:expr, $(($t:ident, $n:tt)),*) => (
impl<$($t),*> From<($($t),*)> for Value
where
$($t: Into<Value>),*
{
fn from(inner: ($($t),*)) -> Self {
Value(format!($pattern, $(implement!(@express inner.$n).into()),*))
}
}
);
}
implement! { "{} {}", (T0, 0), (T1, 1) }
implement! { "{} {} {} {}", (T0, 0), (T1, 1), (T2, 2), (T3, 3) }
#[cfg(test)]
mod tests {
use super::Value;
#[test]
fn value_from_vector() {
assert_eq!(String::from(Value::from(vec![42, 69])), "42 69");
}
}