Skip to content

Instantly share code, notes, and snippets.

Created June 29, 2015 00:56
Show Gist options
  • Save anonymous/0bf55869a4b283bbb2e3 to your computer and use it in GitHub Desktop.
Save anonymous/0bf55869a4b283bbb2e3 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
#![allow(unused_mut, non_camel_case_types, dead_code, unused_variables)]
use std::fmt::{self, Debug};
use std::ops::Add;
/*macro_rules! using {
[ $pp:path : ( $($elem:ident),* )
] => [
use $pp :: {self, $(elem),*};
];
}
using!{ std::fmt : (Debug) }*/
macro_rules! make_overloadable {
[ fn $fn_name:ident();
] => [
trait $fn_name {
type ReturnType;
fn $fn_name( &self ) -> Self::ReturnType;
}
fn $fn_name<TType: $fn_name>( mytuple: &TType ) -> TType::ReturnType {
mytuple.$fn_name()
}
];
/*[ fn $fn_name() -> void;
] => [
trait $fn_name {
type ReturnType;
fn $fn_name( &self );
}
];*/
}
//trait OVERLOADED_GENERIC_FUNCTIONS {}
macro_rules! overload_impl {
[ ( $($all:tt)+ ) :: [][][] :: { $one:ident # } ]
] => [
overload! { ( $($all)+ ) :: [$one] @expand }
];
[ ( $($all:tt)+ ) :: [][][] :: { $one:ident : $($gens:tt)* # }
] => [
overload_impl! { ( $($all)+ ) :: [ $one }
];
}
macro_rules! overload {
( [ $( $t:ident $(:$tr:ident)+ ),* ]
fn $func:ident( $( $arg:ident : $typ:ident ),+ ) $body:block
) => [
impl< $( $t : $( $tr +)+ ),* > $func for ( $( $typ ),+ ) {
type ReturnType = ();
fn $func ( &self )
{
let & ( $( ref $arg ),+ ) = self;
$body
}
}
];
( //[ $( $t:ident $(:$tr:ident)+ ),* ]
[ $( $T1:ident $(+$tr:ident)* ),* ]
fn $func:ident( $( $arg:ident : $typ:ident ),+ ) -> $ret:ty => $body:block
) => [
impl< $( $t : $( $tr +)+ ),* > $func for ( $( $typ ),+ ) {
type ReturnType = $ret;
fn $func ( &self ) -> $ret
{
let & ( $( ref $arg ),+ ) = self;
$body
}
}
];
(
fn $func:ident( $( $arg:ident : $typ:ty ),+ )
[ $( $T1:ident ($($positive_tr:ident),* $(!$negative_tr:ident),*) )* ]
// older syntax
/*[ $($T0:ident.)* ] [ $( #$T1:ident $(+$tr:ident)* );* ]*/
$body:block
) => [
impl
<
/*$($T0),*/
$( $T1 : $( $tr +)* ),*
> $func for ( $( $typ ),+ ) {
type ReturnType = ();
fn $func ( &self )
{
let & ( $( ref $arg ),+ ) = self;
$body
}
}
];
(
fn $func:ident( $( $arg:ident : $typ:ty ),+ ) -> $ret:ty
/*[ $($T0:ident.)* ]*/ //[ $( $T1:ident ($($positive_tr:ident),* $(!$negative_tr:ident),*) )* ]
//[ $($tyt:tt)* ]
//[ $( $t00:ident $(: $( +$tr00:ident )* $(. $(!$trc:ident),+ )* )* );* ] //SPECIAL Version XZ
//[ $( $t00:ident $(: $($tr00:ident)* )* ),* ]
//[ $( $t00:ident $(: $( $tr00:ident ),* $(! $($trc:ident),+ )* )* );* ]
[ $( $t00:ident ($( $tree0:ident )*) )* ]
$body:block
) => [
impl
<
//$($tyt)*
$( $t00 : $($tree0+)* /*$($($trc+)*)*/ ),* // negative bounds not yet supported
/*$($T0),*/
//$( $T1 : $( $positive_tr+ )* $(!$negative_tr +)* ),*
> $func for ( $( $typ ),+ ) {
type ReturnType = $ret;
fn $func ( &self ) -> $ret
{
let &( $( ref $arg ),+ ) = self;
$body
}
}
];
}
make_overloadable! {
fn mk();
}
//#[cfg( pretty_expanded )]
overload! {
fn mk( s: (H, T), g: G ) -> (bool, u8)
//[ H() T(Debug) G(Debug, Copy, Clone) ]
//[ H, T: Debug, G: Debug + Copy + Clone ] // NOT WORKING
//[ H: Debug ! Sized, Clone; T: Debug, Add; G: Debug, Copy, Clone ]
[ H(Debug Sized Clone) T(Debug Add) G(Debug Copy Clone) ]
{
println!("{:?}'tup;
'pkp | {:?}", s, g);
(false, 9)
}
}//
/*
macro_rules! mkfun {
[ ($($func:tt)*) => [$($gen:tt)*] $body:block
] => [
//impl< /*$($gen)*/ > create for (i32) {
$($func:tt)*
where $($gen:tt)*
$body
//}
];
}
mkfun!{
(fn create(_: i32)) => [T: Copy] {
let g = 3 + 3;
}
}
*/
/*
```{.rust}
haskell! {
mk :: U: Debug, Copy => () -> () {
}
}
```
*/
overload! {
[]fn mk( s: char, g: i32, c: char )
{
println!("{}; {:?}", s, g);
}
}
fn mkee<U>(s: i32, g: U) -> bool
where U: fmt::Debug + Copy
{
println!("{}; {:?}", s, g);
false
}
fn mkmee<T: mk>(tuple: &T) -> T::ReturnType {
tuple.mk()
}
fn void() -> Result<i8, u8> {
Err(88)
}
fn none() {
}
fn main() {
println!("Hello, world!");
//mk(32, 'a');
((true, 43), 'Z').mk();
//('F', 63, 'x').mk();
mk(&((true, 43), 'Z'));
let vvv = void();
let nnn = none();
println!("{:?}; {:?}", vvv, nnn);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment