Skip to content

Instantly share code, notes, and snippets.

Created June 22, 2015 21:37
Show Gist options
  • Save anonymous/242f0b6596856b1c4e58 to your computer and use it in GitHub Desktop.
Save anonymous/242f0b6596856b1c4e58 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
#![allow(unused_mut, non_camel_case_types, dead_code)]
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)&+ ),* ]
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
}
}
];
}
make_overloadable! {
fn mk();
}
overload! {
[ G: Debug & Copy ]
fn mk( s: u32, g: G ) : bool =
{
println!("{:?}'u32;
{:?}", s, g);
false
}
}//
overload! {
[]fn mk( s: char, g: i32 ) :=
{
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, 'Z').mk();
('F', 63).mk();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment