Skip to content

Instantly share code, notes, and snippets.

@little-arhat
Created October 19, 2014 19:38
Show Gist options
  • Save little-arhat/a43f57e375305e5803ae to your computer and use it in GitHub Desktop.
Save little-arhat/a43f57e375305e5803ae to your computer and use it in GitHub Desktop.
enum of traits
#![feature(if_let)]
extern crate collections;
use collections::DList;
use collections::Deque;
trait Immut {
fn immut_meth(&self);
}
trait Mut : Immut {
fn mut_meth(&mut self);
}
struct Memory{
d: uint
}
impl Immut for Memory {
fn immut_meth(&self) {
println!("Immut for memory");
}
}
impl Mut for Memory {
fn mut_meth(&mut self) {
println!("Mut for Memory");
}
}
struct Memwrp {
d: int
}
impl Immut for Memwrp {
fn immut_meth(&self) {
println!("Immut for Memwrp");
}
}
enum DH{
Mutable(Box<Mut + Send>),
Immutable(Box<Immut + Send>)
}
fn foo(dl: &mut DList<DH>) {
if let Some(ref mut dh) = dl.front_mut() {
match dh {
&&Mutable(ref mut m) => {
m.mut_meth();
}
&&Immutable(ref im) => {
im.immut_meth();
}
}
}
dl.pop_front();
}
fn main() {
let mut dl = DList::new();
dl.push(Mutable(box Memory{d:0}));
dl.push(Immutable(box Memory{d:0}));
dl.push(Immutable(box Memwrp{d:0}));
foo(&mut dl); foo(&mut dl); foo(&mut dl);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment