Skip to content

Instantly share code, notes, and snippets.

@dbp
Created August 24, 2012 18:33
Show Gist options
  • Save dbp/3454083 to your computer and use it in GitHub Desktop.
Save dbp/3454083 to your computer and use it in GitHub Desktop.
enum variant
...
fn visit_enter_enum_variant(_variant: uint,
_disr_val: int,
_n_fields: uint,
_name: &str) -> bool {
do self.get::<int>() |e| {
if e == _disr_val {
self.out += _name;
}
};
true
}
fn visit_enum_variant_field(_i: uint, inner: *tydesc) -> bool {
self.visit_inner(inner)
}
fn visit_leave_enum_variant(_variant: uint,
_disr_val: int,
_n_fields: uint,
_name: &str) -> bool { true }
...
fn get_tydesc_for<T>(&&_t: T) -> *tydesc {
get_tydesc::<T>()
}
pure fn conv_poly<T>(v: T) -> ~str {
let p = ptr::addr_of(v) as *c_void;
let u = fmt_visitor(@{mut ptr: p, mut out: ~""});
unsafe {
let td = get_tydesc_for(v);
visit_tydesc(td, u as ty_visitor);
}
return copy u.out;
}
...
#[test]
fn enum_variant_type() {
enum a {b, c};
assert conv_poly(b) == ~"b";
enum d {e(int), f(~str)};
// FIXME: segfault
assert conv_poly(e(10)) == ~"e(10)";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment