Skip to content

Instantly share code, notes, and snippets.

View dbp's full-sized avatar

Daniel Patterson dbp

View GitHub Profile
@dbp
dbp / gist:3442506
Created August 23, 2012 21:58
macro
macro_rules! num_visitor(
($typ:ident) => (
fn visit_$typ() -> bool {
self.align_to::<$typ>();
do self.get::<$typ>() |i| {
self.out += $typ::to_str(i, 10u);
};
self.bump_past::<$typ>();
true
}
@dbp
dbp / bits.rs
Created August 16, 2012 16:39
converting ~[u8] to u32
fn main() {
let bytes : ~[u8] = ~[1,1,1]; // for example
let len = vec::len(bytes)-1;
let mut out: u32 = 0;
do vec::iteri(bytes) |i, b| {
out += (b as u32) << (8*(len-i))
}
io::println(u32::str(out));
}
@dbp
dbp / gist:3308745
Created August 9, 2012 22:44
Rust Pretty printing closures
// how pretty printer formats closures (I think; I'm unable to build from incoming right now to check)
fn main() {
do something |foo|
{
something_long_enough_to_not;
fit_on_a_single_line;
}
let my_fn = ||
{
something_long_enough_to_not;