Created
June 30, 2014 23:12
-
-
Save akiradeveloper/5360311addc87a413efa to your computer and use it in GitHub Desktop.
Rust workaround to implement factory-pattern
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
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>); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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];
}