Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 25, 2019 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/3ccd64c752b76127df617e229e12b450 to your computer and use it in GitHub Desktop.
Save rust-play/3ccd64c752b76127df617e229e12b450 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
trait Cast<T>{
fn cast(self)->T;
}
macro_rules! cartesian_producs_of_impls {
( row[ $($row:ty),* ] col[ $($col:ty),* ] )=>{
cartesian_producs_of_impls!{inner
state()
row[$($row),*]
col[$($col),*]
}
};
(inner state($($tokens:tt)*) row[] col[ $($col:ty),* ] )=>{
$($tokens)*
};
(inner
state($($tokens:tt)*)
row[ $first_one:ty $(,$rem:ty)* ]
col[ $($col:ty),* ]
)=>{
cartesian_producs_of_impls!{inner
state(
$($tokens)*
$(
impl Cast<$col> for $first_one{
fn cast(self)->$col{
self as _
}
}
)*
)
row[$($rem),*]
col[$($col),*]
}
};
}
cartesian_producs_of_impls!{
row[u8,u16,u32]
col[u8,u16,u32]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment