Skip to content

Instantly share code, notes, and snippets.

Created June 24, 2015 11:33
Show Gist options
  • Save anonymous/98a5a37c0f219f98228a to your computer and use it in GitHub Desktop.
Save anonymous/98a5a37c0f219f98228a 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};
/*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() -> void;
] => [
trait $fn_name {
type ReturnType;
fn $fn_name( &self );
}
];*/
}
//trait OVERLOADED_GENERIC_FUNCTIONS {}
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),*) )* ]
$body:block
) => [
impl
<
//$(trace_macros!{true})*
/*$($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();
}
overload! {
fn mk( s: (T, H), g: G ) -> (bool, u8)
[ H() T(Debug) G(Debug, Copy, Clone) ]
{
println!("{:?}'tup;
| {:?}", s.0, g);
(false, 9)
}
}//
/*
```{.rust}
haskell! {
mk :: U: Debug, Copy => () -> () {
}
}
```
*/
overload! {
[]fn mk( s: char, g: i32, c: char )
{
println!("{}; {:?}", s, g);
}
}
fn mk<U: fmt::Debug + Copy>(s: i32, g: U) -> bool {
println!("{}; {:?}", s, g);
false
}
fn main() {
println!("Hello, world!");
mk(32, 'a');
((42, false), 'Z').mk();
('F', 63, 'x').mk();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment