From f936fcfb134260fbd952c5172f7637df4c61d82f Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Sun, 18 Jun 2017 03:05:22 -0700 Subject: [PATCH 1/2] Add exhaustive From impls for Value --- src/value.rs | 72 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/src/value.rs b/src/value.rs index 83be066..48553fd 100644 --- a/src/value.rs +++ b/src/value.rs @@ -1,6 +1,7 @@ //! Definition of a TOML value -use std::collections::BTreeMap; +use std::collections::{BTreeMap, HashMap}; +use std::hash::Hash; use std::fmt; use std::ops; use std::str::FromStr; @@ -219,47 +220,62 @@ impl ops::IndexMut for Value where I: Index { } } -impl From for Value { - fn from(val: String) -> Value { - Value::String(val) +impl<'a> From<&'a str> for Value { + #[inline] + fn from(val: &'a str) -> Value { + Value::String(val.to_string()) } } -impl From for Value { - fn from(val: i64) -> Value { - Value::Integer(val) +impl> From> for Value { + fn from(val: Vec) -> Value { + Value::Array(val.into_iter().map(|v| v.into()).collect()) } } -impl From for Value { - fn from(val: f64) -> Value { - Value::Float(val) +impl, V: Into> From> for Value { + fn from(val: BTreeMap) -> Value { + let table = val.into_iter() + .map(|(s, v)| (s.into(), v.into())) + .collect(); + + Value::Table(table) } } -impl From for Value { - fn from(val: bool) -> Value { - Value::Boolean(val) +impl + Hash + Eq, V: Into> From> for Value { + fn from(val: HashMap) -> Value { + let table = val.into_iter() + .map(|(s, v)| (s.into(), v.into())) + .collect(); + + Value::Table(table) } } -impl From for Value { - fn from(val: Array) -> Value { - Value::Array(val) - } +macro_rules! impl_into_value { + ($variant:ident : $T:ty) => (impl_into_value!($variant: $T,);); + ($variant:ident : $T:ty, $($extra:tt)*) => ( + impl From<$T> for Value { + #[inline] + fn from(val: $T) -> Value { + Value::$variant(val $($extra)*) + } + } + ) } -impl From for Value { - fn from(val: Table) -> Value { - Value::Table(val) - } -} - -impl From for Value { - fn from(val: Datetime) -> Value { - Value::Datetime(val) - } -} +impl_into_value!(String: String); +impl_into_value!(Integer: i64); +impl_into_value!(Integer: isize, as i64); +impl_into_value!(Integer: i32, as i64); +impl_into_value!(Integer: i8, as i64); +impl_into_value!(Integer: u8, as i64); +impl_into_value!(Integer: u32, as i64); +impl_into_value!(Float: f64); +impl_into_value!(Float: f32, as f64); +impl_into_value!(Boolean: bool); +impl_into_value!(Datetime: Datetime); /// Types that can be used to index a `toml::Value` /// From 7f9e594e475715f3c188909e95b27abc19467051 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Mon, 19 Jun 2017 17:49:55 -0700 Subject: [PATCH 2/2] Use 'into()' to convert numerical values safely --- src/value.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/value.rs b/src/value.rs index 48553fd..5efdb92 100644 --- a/src/value.rs +++ b/src/value.rs @@ -254,26 +254,24 @@ impl + Hash + Eq, V: Into> From> for Value } macro_rules! impl_into_value { - ($variant:ident : $T:ty) => (impl_into_value!($variant: $T,);); - ($variant:ident : $T:ty, $($extra:tt)*) => ( + ($variant:ident : $T:ty) => { impl From<$T> for Value { #[inline] fn from(val: $T) -> Value { - Value::$variant(val $($extra)*) + Value::$variant(val.into()) } } - ) + } } impl_into_value!(String: String); impl_into_value!(Integer: i64); -impl_into_value!(Integer: isize, as i64); -impl_into_value!(Integer: i32, as i64); -impl_into_value!(Integer: i8, as i64); -impl_into_value!(Integer: u8, as i64); -impl_into_value!(Integer: u32, as i64); +impl_into_value!(Integer: i32); +impl_into_value!(Integer: i8); +impl_into_value!(Integer: u8); +impl_into_value!(Integer: u32); impl_into_value!(Float: f64); -impl_into_value!(Float: f32, as f64); +impl_into_value!(Float: f32); impl_into_value!(Boolean: bool); impl_into_value!(Datetime: Datetime);