Skip to content

Instantly share code, notes, and snippets.

@munael
Forked from anonymous/playground.rs
Last active August 29, 2015 14:23
Show Gist options
  • Save munael/96eaa4ec825712b4424e to your computer and use it in GitHub Desktop.
Save munael/96eaa4ec825712b4424e to your computer and use it in GitHub Desktop.
#![allow(unused_mut, non_camel_case_types, dead_code, unused_variables, non_upper_case_globals, non_snake_case, unused_imports)]
#![feature(optin_builtin_traits, /*unboxed_closures*/, associated_consts)]
use std::fmt::{self, Debug};
use std::ops::Add;
use std::convert::{self, Into, From};
/*macro_rules! using {
[ $pp:path : ( $($elem:ident),* )
] => [
use $pp :: {self, $(elem),*};
];
}
using!{ std::fmt : (Debug) }*/
/*
trait New {
type Init; type Return;
fn new( self, init: Self::Init ) -> Self::Return;
}
struct S { v: i32 }
impl New for S {
type Init = u8;
type Return = i32;
fn new( self, init: u8 ) -> i32 {
32
}
}
struct A {
val: i32
}
fn Main() {
let a: A = { let mut f = A {val: 3}; f.val += 1; f };
let _ = New::new( S{v: 4i32}, 9u8 );
let x : u8 = 5;
//a.val = 2;
println!("a: {}", a.val);
}
struct N;
struct G;
trait Hash {
const SSS: i32 = 32;
fn hash_it(&self) { println!("Hash-it: Default"); }
}
trait Hash2 : Hash {
fn hash(&self) { println!("Hash2 :"); self.hash_it(); }
}
trait Hash3 {
fn hash<>(&self)
where Self : Hash
{ println!("Hash3 :"); self.hash_it(); }
}
trait Hash4<>
where Self : Hash
{
fn hash(&self) { println!("Hash4 :"); self.hash_it(); }
}
//impl Hash2 for N {}
impl Hash3 for .. {}
impl !Hash3 for N {}
//impl Hash3 for N { fn hash(&self) { println!("Hash3 specific: "); } }
impl Hash for G {}
impl Hash2 for G {}
*/
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()
}
struct MyFrom;
trait mytr {
fn start() { println!("HAH! mytr.start()"); }
}
impl<T> mytr for T
where T: Clone
{ }
impl mytr for MyFrom {
fn start() { println!("HAH! (MyFrom: mytr).start() / mytr[MyFrom].start()"); }
}
impl Into<i32> for MyFrom {
fn into(self) -> i32 { 43 }
}
impl Into<String> for MyFrom {
fn into(self) -> String { "MyFrom".to_string() }
}
fn void() -> /*Result<i8, u8>*/ i8 {
let v : Result<i8, u8> = Err(88);
//v.unwrap()
//()
//panic!();
match v {
Err(x) => { x as i8 },
_ => { 0 }
}
}
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();
let x = MyFrom;
<MyFrom as Into<i32>>::into(x);
println!("{:?}; {:?}", vvv, nnn);
/*
let o1 = G;
let o2 = N;
o1.hash();
o2.hash();
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment