diff --git a/src/attr.rs b/src/attr.rs index 46a3339c77..64bcc4efa9 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -85,7 +85,6 @@ impl<'a, T> FilterAttrs<'a> for T pub mod parsing { use super::*; use ident::parsing::ident; - use lit::{Lit, StrStyle}; use lit::parsing::lit; use space::{block_comment, whitespace}; @@ -111,10 +110,7 @@ pub mod parsing { style: AttrStyle::Inner, value: MetaItem::NameValue( "doc".into(), - Lit::Str( - format!("//!{}", content), - StrStyle::Cooked, - ), + format!("//!{}", content).into(), ), is_sugared_doc: true, }) @@ -128,7 +124,7 @@ pub mod parsing { style: AttrStyle::Inner, value: MetaItem::NameValue( "doc".into(), - Lit::Str(com.to_owned(), StrStyle::Cooked), + com.into(), ), is_sugared_doc: true, }) @@ -156,10 +152,7 @@ pub mod parsing { style: AttrStyle::Outer, value: MetaItem::NameValue( "doc".into(), - Lit::Str( - format!("///{}", content), - StrStyle::Cooked, - ), + format!("///{}", content).into(), ), is_sugared_doc: true, }) @@ -173,7 +166,7 @@ pub mod parsing { style: AttrStyle::Outer, value: MetaItem::NameValue( "doc".into(), - Lit::Str(com.to_owned(), StrStyle::Cooked), + com.into(), ), is_sugared_doc: true, }) diff --git a/src/lit.rs b/src/lit.rs index c1a2876322..cbe51b9cd8 100644 --- a/src/lit.rs +++ b/src/lit.rs @@ -29,6 +29,42 @@ pub enum StrStyle { Raw(usize), } +impl From for Lit { + fn from(input: String) -> Lit { + Lit::Str(input, StrStyle::Cooked) + } +} + +impl<'a> From<&'a str> for Lit { + fn from(input: &str) -> Lit { + Lit::Str(input.into(), StrStyle::Cooked) + } +} + +impl From> for Lit { + fn from(input: Vec) -> Lit { + Lit::ByteStr(input) + } +} + +impl<'a> From<&'a [u8]> for Lit { + fn from(input: &[u8]) -> Lit { + Lit::ByteStr(input.into()) + } +} + +impl From for Lit { + fn from(input: char) -> Lit { + Lit::Char(input) + } +} + +impl From for Lit { + fn from(input: bool) -> Lit { + Lit::Bool(input) + } +} + #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum IntTy { Isize, @@ -51,6 +87,45 @@ pub enum FloatTy { Unsuffixed, } +macro_rules! impl_from_for_lit { + (Int, [$($rust_type:ty => $syn_type:expr),+]) => { + $( + impl From<$rust_type> for Lit { + fn from(input: $rust_type) -> Lit { + Lit::Int(input as u64, $syn_type) + } + } + )+ + }; + (Float, [$($rust_type:ty => $syn_type:expr),+]) => { + $( + impl From<$rust_type> for Lit { + fn from(input: $rust_type) -> Lit { + Lit::Float(format!("{}", input), $syn_type) + } + } + )+ + }; +} + +impl_from_for_lit! {Int, [ + isize => IntTy::Isize, + i8 => IntTy::I8, + i16 => IntTy::I16, + i32 => IntTy::I32, + i64 => IntTy::I64, + usize => IntTy::Usize, + u8 => IntTy::U8, + u16 => IntTy::U16, + u32 => IntTy::U32, + u64 => IntTy::U64 +]} + +impl_from_for_lit! {Float, [ + f32 => FloatTy::F32, + f64 => FloatTy::F64 +]} + #[cfg(feature = "parsing")] pub mod parsing { use super::*;