Skip to content

Instantly share code, notes, and snippets.

@eevee
Created February 18, 2013 21:37
Show Gist options
  • Save eevee/4981007 to your computer and use it in GitHub Desktop.
Save eevee/4981007 to your computer and use it in GitHub Desktop.
lvalues can update themselves in Rust? (there is probably a nicer way to write this)
enum Flag {
X0 = 1,
X1 = 2,
X2 = 4,
X3 = 8,
X4 = 16,
}
type Flagset = uint;
trait FlagsetMethods {
fn add(&mut self, Flag);
fn has(Flag) -> bool;
}
impl Flagset: FlagsetMethods {
fn add(&mut self, f: Flag) {
*self |= (f as uint);
}
fn has(f: Flag) -> bool {
return self & (f as uint) != 0;
}
}
fn main() {
let mut flags: Flagset = 0;
(&mut flags).add(X0);
(&mut flags).add(X2);
io::println(fmt!("%?", flags.has(X0)));
io::println(fmt!("%?", flags.has(X1)));
io::println(fmt!("%?", flags.has(X2)));
io::println(fmt!("%?", flags.has(X3)));
io::println(fmt!("%?", flags.has(X4)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment