Created
May 10, 2013 01:03
-
-
Save mstewartgallus/5551770 to your computer and use it in GitHub Desktop.
Introspection
This file contains 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
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