Skip to content

Instantly share code, notes, and snippets.

@mstewartgallus
Created May 10, 2013 01:03
Show Gist options
  • Save mstewartgallus/5551770 to your computer and use it in GitHub Desktop.
Save mstewartgallus/5551770 to your computer and use it in GitHub Desktop.
Introspection
pub trait Introspectable {
fn size (&self) -> uint;
fn raw_data (&self) -> ~[u8];
fn debug_info (&self) -> ~str;
}
impl <T> Introspectable for T {
fn size (&self) -> uint { sys::size_of:: <T> () }
fn raw_data (&self) -> ~[u8] {
unsafe {
let raw : *u8 = cast::transmute (self);
let len = self.size ();
do vec::build_sized (len) |push| {
for uint::range (0, len) |ii| {
push (*(ptr::offset (raw, ii)))
}
}
}
}
fn debug_info (&self) -> ~str {
~"<" /* + self.type_name () + " " */
+ ptr::to_uint (self).to_str () + " "
+ self.raw_data ().to_str () + ">"
}
}
fn main () {
let x = 50;
println (x.debug_info ())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment