Created
May 14, 2014 03:19
-
-
Save shadowmint/d233c74dd30d0cf50c0a to your computer and use it in GitHub Desktop.
macros
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![macro_escape] | |
/** | |
* Create a new T with default values. | |
* usage: | |
* | |
* let x:T = default!(); | |
* let x = default!(Foo, x:1); | |
* let x = default!(Foo); | |
*/ | |
#[macro_export] | |
macro_rules! default( | |
($T:ident, $($k:ident: $v:expr), *) => ( | |
$T { $($k: $v), *, ..::std::default::Default::default() } | |
); | |
($T:ident) => ( | |
$T { ..::std::default::Default::default() } | |
); | |
() => ( | |
::std::default::Default::default(); | |
); | |
) | |
/** | |
* Debug print immediately to stdout | |
* usage: | |
* | |
* trace!("Hello: {}", 1); | |
*/ | |
#[macro_export] | |
macro_rules! trace( | |
{$($arg:tt)*} => { | |
{ | |
let _foo = ::std::io::stdout().write_line(format_args!(::std::fmt::format, $($arg)*)); | |
} | |
}; | |
) | |
/** | |
* Fabricate any T that implments std::container::MutableMap | |
* usage: | |
* | |
* let mut hm = map! {HashMap, "a string" => 21, "another string" => 33 }; | |
*/ | |
#[macro_export] | |
macro_rules! map( | |
{ $T:ident, $($key:expr => $value:expr),+ } => { | |
{ | |
let mut m = $T::new(); | |
$( | |
m.insert($key, $value); | |
)+ | |
m | |
} | |
}; | |
) | |
#[cfg(test)] | |
mod tests { | |
extern crate collections; | |
use self::collections::HashMap; | |
use std::default::Default; | |
#[deriving(Show)] | |
struct Foo { | |
x:int, | |
y:int, | |
z:int, | |
a:int, | |
b:int, | |
c:int | |
} | |
impl Default for Foo { | |
fn default() -> Foo { | |
return Foo { x: 1, y: 2, z: 3, a: 4, b: 5, c: 6}; | |
} | |
} | |
#[test] | |
fn test_default_works() { | |
let x:Foo = default!(); | |
assert!(x.x == 1); | |
assert!(x.y == 2); | |
assert!(x.z == 3); | |
assert!(x.a == 4); | |
assert!(x.b == 5); | |
assert!(x.c == 6); | |
} | |
#[test] | |
fn test_default_works_with_x_args() { | |
let x = default!(Foo, x:9, a:0, b:1, c:2); | |
assert!(x.x == 9); | |
assert!(x.y == 2); | |
assert!(x.z == 3); | |
assert!(x.a == 0); | |
assert!(x.b == 1); | |
assert!(x.c == 2); | |
} | |
#[test] | |
fn test_default_works_with_y_args() { | |
let x = default!(Foo, y:1, z:9, c:0); | |
assert!(x.x == 1); | |
assert!(x.y == 1); | |
assert!(x.z == 9); | |
assert!(x.a == 4); | |
assert!(x.b == 5); | |
assert!(x.c == 0); | |
} | |
fn t_factory<T: Default>() -> T { | |
let rtn:T = default!(); | |
return rtn; | |
} | |
#[test] | |
fn test_default_works_with_unknown_T() { | |
let x = t_factory::<Foo>(); | |
assert!(x.x == 1); | |
assert!(x.y == 2); | |
assert!(x.z == 3); | |
assert!(x.a == 4); | |
assert!(x.b == 5); | |
assert!(x.c == 6); | |
} | |
#[test] | |
fn test_trace_macro() { | |
trace!("Trace test: {} {}", 1, 2); | |
} | |
#[test] | |
fn test_map_create() { | |
let mut hm = map! {HashMap, | |
"a string" => 21, | |
"another string" => 33 | |
}; | |
hm.insert("and another", 43); | |
for (k, v) in hm.iter() { | |
trace!("Map ({}, {})", *k, *v); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment