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 118 119 120 121 122 123 124 125 126 127
use super::Tokens; pub trait ToTokens { fn to_tokens(&self, &mut Tokens); } impl<'a, T: ToTokens> ToTokens for &'a T { fn to_tokens(&self, tokens: &mut Tokens) { (**self).to_tokens(tokens); } } impl<T: ToTokens> ToTokens for Box<T> { fn to_tokens(&self, tokens: &mut Tokens) { (**self).to_tokens(tokens); } } impl<T: ToTokens> ToTokens for Option<T> { fn to_tokens(&self, tokens: &mut Tokens) { if let Some(ref t) = *self { t.to_tokens(tokens); } } } impl ToTokens for str { fn to_tokens(&self, tokens: &mut Tokens) { tokens.append(&format!("{:?}", self).replace("\\'", "'")); } } impl ToTokens for String { fn to_tokens(&self, tokens: &mut Tokens) { tokens.append(&format!("{:?}", self).replace("\\'", "'")); } } impl ToTokens for char { fn to_tokens(&self, tokens: &mut Tokens) { if *self == '"' { tokens.append("'\"'"); } else { tokens.append(&format!("{:?}", self)); } } } #[derive(Debug)] pub struct ByteStr<'a>(pub &'a str); impl<'a> ToTokens for ByteStr<'a> { fn to_tokens(&self, tokens: &mut Tokens) { tokens.append(&format!("b{:?}", self.0)); } } macro_rules! impl_to_tokens_display { ($ty:ty) => { impl ToTokens for $ty { fn to_tokens(&self, tokens: &mut Tokens) { tokens.append(&self.to_string()); } } }; } impl_to_tokens_display!(Tokens); impl_to_tokens_display!(bool); macro_rules! impl_to_tokens_integer { ($ty:ty) => { impl ToTokens for $ty { fn to_tokens(&self, tokens: &mut Tokens) { tokens.append(&format!(concat!("{}", stringify!($ty)), self)); } } }; } impl_to_tokens_integer!(i8); impl_to_tokens_integer!(i16); impl_to_tokens_integer!(i32); impl_to_tokens_integer!(i64); impl_to_tokens_integer!(isize); impl_to_tokens_integer!(u8); impl_to_tokens_integer!(u16); impl_to_tokens_integer!(u32); impl_to_tokens_integer!(u64); impl_to_tokens_integer!(usize); macro_rules! impl_to_tokens_floating { ($ty:ty) => { impl ToTokens for $ty { fn to_tokens(&self, tokens: &mut Tokens) { use std::num::FpCategory::*; match self.classify() { Zero | Subnormal | Normal => { tokens.append(&format!(concat!("{}", stringify!($ty)), self)); } Nan => { tokens.append("::"); tokens.append("std"); tokens.append("::"); tokens.append(stringify!($ty)); tokens.append("::"); tokens.append("NAN"); } Infinite => { tokens.append("::"); tokens.append("std"); tokens.append("::"); tokens.append(stringify!($ty)); tokens.append("::"); if self.is_sign_positive() { tokens.append("INFINITY"); } else { tokens.append("NEG_INFINITY"); } } } } } }; } impl_to_tokens_floating!(f32); impl_to_tokens_floating!(f64);