#[cfg(feature = "phf")]
use std::collections::HashMap;
use std::collections::HashSet;
#[cfg(feature = "derive")]
pub use const_gen_derive::*;
#[cfg(test)]
mod test;
#[macro_export]
macro_rules! const_definition
{
( $(#[$attr:meta])* $ty:ty) =>
{
<$ty>::const_definition(stringify!($(#[$attr])*))
}
}
#[macro_export]
macro_rules! const_declaration
{
($name:ident = $($val:tt)*) =>
{
$($val)*.const_declaration(stringify!($name))
}
}
pub trait CompileConst
{
fn const_type() -> String;
fn const_val(&self) -> String;
fn const_declaration(&self, name: &str) -> String
{
format!("const {}: {} = {};", name, Self::const_type(), self.const_val())
}
fn const_definition(_attrs: &str) -> String
{
String::new()
}
}
macro_rules! numerics
{
( $($t:ty),* ) =>
{
$(impl CompileConst for $t
{
fn const_type() -> String
{
stringify!($t).to_string()
}
fn const_val(&self) -> String
{
format!("{}{}", self, stringify!($t))
}
})*
}
}
numerics!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64);
macro_rules! strings
{
( $($t:ty),* ) =>
{
$(impl CompileConst for $t
{
fn const_type() -> String
{
"&'static str".to_string()
}
fn const_val(&self) -> String
{
format!("\"{}\"", self)
}
})*
}
}
strings!(String, &str, str);
macro_rules! arrays
{
( $($t:ty),* ) =>
{
$(impl<T: CompileConst> CompileConst for $t
{
fn const_type() -> String
{
format!("&'static [{}]", T::const_type())
}
fn const_val(&self) -> String
{
format!("&[{}]", self
.into_iter()
.map(|e| e.const_val())
.collect::<Vec<String>>()
.join(","))
}
})*
}
}
arrays!(Vec<T>, &[T]);
macro_rules! derefs
{
( $($t:ty $(=> $bound:tt)?),* ) =>
{
$(impl<T: CompileConst $(+ $bound)? > CompileConst for $t
{
fn const_type() -> String
{
T::const_type()
}
fn const_val(&self) -> String
{
(**self).const_val()
}
})*
}
}
derefs!
(
Box<T>,
std::borrow::Cow<'_, T> => Clone,
std::rc::Rc<T>,
std::sync::Arc<T>
);
#[cfg(feature = "phf")]
impl<K: CompileConst, V: CompileConst> CompileConst for HashMap<K,V>
{
fn const_type() -> String
{
format!("phf::Map<{}, {}>", K::const_type(), V::const_type())
}
fn const_val(&self) -> String
{
format!("phf::phf_map!{{{}}}", self
.into_iter()
.map(|(k,v)| format!("{} => {}", k.const_val(), v.const_val()))
.collect::<Vec<String>>()
.join(","))
}
}
#[cfg(feature = "phf")]
impl<E: CompileConst> CompileConst for HashSet<E>
{
fn const_type() -> String
{
format!("phf::Set<{}>", E::const_type())
}
fn const_val(&self) -> String
{
format!("phf::phf_set!{{{}}}", self
.into_iter()
.map(|e| format!("{}", e.const_val()))
.collect::<Vec<String>>()
.join(","))
}
}
macro_rules! tuples
{
($format:literal $(, $ty:ident $index:tt)*) =>
{
impl<$($ty: CompileConst),*> CompileConst for ($($ty),*)
{
fn const_type() -> String
{
format!($format, $($ty::const_type()),*)
}
fn const_val(&self) -> String
{
format!($format, $(self.$index.const_val()),*)
}
}
}
}
tuples!("()");
tuples!("({},{})", A 0, B 1);
tuples!("({},{},{})", A 0, B 1, C 2);
tuples!("({},{},{},{})", A 0, B 1, C 2, D 3);
tuples!("({},{},{},{},{})", A 0, B 1, C 2, D 3, E 4);
tuples!("({},{},{},{},{},{})", A 0, B 1, C 2, D 3, E 4, F 5);
tuples!("({},{},{},{},{},{},{})", A 0, B 1, C 2, D 3, E 4, F 5, G 6);
tuples!("({},{},{},{},{},{},{},{})", A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7);
tuples!("({},{},{},{},{},{},{},{},{})", A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8);
tuples!("({},{},{},{},{},{},{},{},{},{})", A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9);
tuples!("({},{},{},{},{},{},{},{},{},{},{})", A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10);
tuples!("({},{},{},{},{},{},{},{},{},{},{},{})", A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10, L 11);
tuples!("({},{},{},{},{},{},{},{},{},{},{},{},{})", A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10, L 11, M 12);
tuples!("({},{},{},{},{},{},{},{},{},{},{},{},{},{})", A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10, L 11, M 12, N 13);
tuples!("({},{},{},{},{},{},{},{},{},{},{},{},{},{},{})", A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10, L 11, M 12, N 13, O 14);
tuples!("({},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{})", A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10, L 11, M 12, N 13, O 14, P 15);