Skip to content

Instantly share code, notes, and snippets.

@akiradeveloper
Created June 30, 2014 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akiradeveloper/5360311addc87a413efa to your computer and use it in GitHub Desktop.
Save akiradeveloper/5360311addc87a413efa to your computer and use it in GitHub Desktop.
Rust workaround to implement factory-pattern
trait Construct {
fn new(_hint: Option<Self>) -> Box<Construct>;
}
struct Foo { x: int }
struct Bar { y: uint }
impl Construct for Foo {
fn new(_: Option<Foo>) -> Box<Construct> {
box Foo { x: 42 } as Box<Construct>
}
}
impl Construct for Bar {
fn new(_: Option<Bar>) -> Box<Construct> {
box Bar { y: 24 } as Box<Construct>
}
}
fn main() {
let x: Box<Construct> = Construct::new(None::<Foo>);
}
@akiradeveloper
Copy link
Author

Another snippet

struct A;
struct B;

trait O { }

impl O for A { }
impl O for B { }

fn main() {
let os: [Box, ..2] = [box() A as Box, box() B as Box];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment