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 std::borrow::Cow;

use crate::{Cons, Number, Value};

macro_rules! impl_from_number {
    (
        $($ty:ty),*
    ) => {
        $(
            impl From<$ty> for Value {
                #[inline]
                fn from(n: $ty) -> Self {
                    Value::Number(Number::from(n))
                }
            }
        )*
    };
}

impl_from_number!(u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);

impl From<char> for Value {
    #[inline]
    fn from(c: char) -> Self {
        Value::Char(c)
    }
}

impl From<&str> for Value {
    #[inline]
    fn from(s: &str) -> Self {
        Value::String(s.into())
    }
}

impl<'a> From<Cow<'a, str>> for Value {
    #[inline]
    fn from(s: Cow<'a, str>) -> Self {
        Value::from(s.as_ref())
    }
}

impl From<Box<str>> for Value {
    #[inline]
    fn from(s: Box<str>) -> Self {
        Value::String(s)
    }
}

impl From<String> for Value {
    #[inline]
    fn from(s: String) -> Self {
        Value::String(s.into_boxed_str())
    }
}

impl From<bool> for Value {
    #[inline]
    fn from(v: bool) -> Self {
        Value::Bool(v)
    }
}

impl From<&[u8]> for Value {
    #[inline]
    fn from(bytes: &[u8]) -> Self {
        Value::Bytes(bytes.into())
    }
}

impl From<Box<[u8]>> for Value {
    #[inline]
    fn from(bytes: Box<[u8]>) -> Self {
        Value::Bytes(bytes)
    }
}

impl From<Vec<u8>> for Value {
    #[inline]
    fn from(bytes: Vec<u8>) -> Self {
        Value::Bytes(bytes.into_boxed_slice())
    }
}

impl From<Number> for Value {
    fn from(n: Number) -> Self {
        Value::Number(n)
    }
}

impl<T, U> From<(T, U)> for Value
where
    T: Into<Value>,
    U: Into<Value>,
{
    fn from((car, cdr): (T, U)) -> Self {
        Value::Cons(Cons::new(car, cdr))
    }
}

impl From<Cons> for Value {
    fn from(pair: Cons) -> Self {
        Value::Cons(pair)
    }
}

impl From<Vec<Value>> for Value {
    fn from(elts: Vec<Value>) -> Self {
        Value::Vector(elts.into())
    }
}

impl From<Box<[Value]>> for Value {
    fn from(elts: Box<[Value]>) -> Self {
        Value::Vector(elts)
    }
}