3 use super::{ToValue, Value, Primitive}
;
5 impl ToValue
for usize {
6 fn to_value(&self) -> Value
{
11 impl<'v
> From
<usize> for Value
<'v
> {
12 fn from(value
: usize) -> Self {
13 Value
::from_primitive(Primitive
::Unsigned(value
as u64))
17 impl ToValue
for isize {
18 fn to_value(&self) -> Value
{
23 impl<'v
> From
<isize> for Value
<'v
> {
24 fn from(value
: isize) -> Self {
25 Value
::from_primitive(Primitive
::Signed(value
as i64))
30 fn to_value(&self) -> Value
{
35 impl<'v
> From
<u8> for Value
<'v
> {
36 fn from(value
: u8) -> Self {
37 Value
::from_primitive(Primitive
::Unsigned(value
as u64))
41 impl ToValue
for u16 {
42 fn to_value(&self) -> Value
{
47 impl<'v
> From
<u16> for Value
<'v
> {
48 fn from(value
: u16) -> Self {
49 Value
::from_primitive(Primitive
::Unsigned(value
as u64))
53 impl ToValue
for u32 {
54 fn to_value(&self) -> Value
{
59 impl<'v
> From
<u32> for Value
<'v
> {
60 fn from(value
: u32) -> Self {
61 Value
::from_primitive(Primitive
::Unsigned(value
as u64))
65 impl ToValue
for u64 {
66 fn to_value(&self) -> Value
{
71 impl<'v
> From
<u64> for Value
<'v
> {
72 fn from(value
: u64) -> Self {
73 Value
::from_primitive(Primitive
::Unsigned(value
))
78 fn to_value(&self) -> Value
{
83 impl<'v
> From
<i8> for Value
<'v
> {
84 fn from(value
: i8) -> Self {
85 Value
::from_primitive(Primitive
::Signed(value
as i64))
89 impl ToValue
for i16 {
90 fn to_value(&self) -> Value
{
95 impl<'v
> From
<i16> for Value
<'v
> {
96 fn from(value
: i16) -> Self {
97 Value
::from_primitive(Primitive
::Signed(value
as i64))
101 impl ToValue
for i32 {
102 fn to_value(&self) -> Value
{
107 impl<'v
> From
<i32> for Value
<'v
> {
108 fn from(value
: i32) -> Self {
109 Value
::from_primitive(Primitive
::Signed(value
as i64))
113 impl ToValue
for i64 {
114 fn to_value(&self) -> Value
{
119 impl<'v
> From
<i64> for Value
<'v
> {
120 fn from(value
: i64) -> Self {
121 Value
::from_primitive(Primitive
::Signed(value
))
125 impl ToValue
for f32 {
126 fn to_value(&self) -> Value
{
131 impl<'v
> From
<f32> for Value
<'v
> {
132 fn from(value
: f32) -> Self {
133 Value
::from_primitive(Primitive
::Float(value
as f64))
137 impl ToValue
for f64 {
138 fn to_value(&self) -> Value
{
143 impl<'v
> From
<f64> for Value
<'v
> {
144 fn from(value
: f64) -> Self {
145 Value
::from_primitive(Primitive
::Float(value
))
149 impl ToValue
for bool
{
150 fn to_value(&self) -> Value
{
155 impl<'v
> From
<bool
> for Value
<'v
> {
156 fn from(value
: bool
) -> Self {
157 Value
::from_primitive(Primitive
::Bool(value
))
161 impl ToValue
for char {
162 fn to_value(&self) -> Value
{
167 impl<'v
> From
<char> for Value
<'v
> {
168 fn from(value
: char) -> Self {
169 Value
::from_primitive(Primitive
::Char(value
))
173 impl<'v
> ToValue
for &'v
str {
174 fn to_value(&self) -> Value
{
179 impl<'v
> From
<&'v
str> for Value
<'v
> {
180 fn from(value
: &'v
str) -> Self {
181 Value
::from_primitive(Primitive
::Str(value
))
185 impl ToValue
for () {
186 fn to_value(&self) -> Value
{
187 Value
::from_primitive(Primitive
::None
)
191 impl<T
> ToValue
for Option
<T
>
195 fn to_value(&self) -> Value
{
197 Some(ref value
) => value
.to_value(),
198 None
=> Value
::from_primitive(Primitive
::None
),
203 impl<'v
> ToValue
for fmt
::Arguments
<'v
> {
204 fn to_value(&self) -> Value
{
205 Value
::from_debug(self)
209 #[cfg(feature = "std")]
213 use std
::borrow
::Cow
;
215 impl<T
> ToValue
for Box
<T
>
219 fn to_value(&self) -> Value
{
224 impl ToValue
for String
{
225 fn to_value(&self) -> Value
{
226 Value
::from_primitive(Primitive
::Str(&*self))
230 impl<'v
> ToValue
for Cow
<'v
, str> {
231 fn to_value(&self) -> Value
{
232 Value
::from_primitive(Primitive
::Str(&*self))
240 use kv
::value
::test
::Token
;
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");
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
);