Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code shared from the Rust Playground
fn main(){
let _:i8=0u8.cast();
let _:i16=0u8.cast();
let _:i32=0u8.cast();
let _:i8=0u16.cast();
let _:i16=0u16.cast();
let _:i32=0u16.cast();
let _:i8=0u32.cast();
let _:i16=0u32.cast();
let _:i32=0u32.cast();
}
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[i8,i16,i32]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.