Created
July 23, 2016 15:31
-
-
Save anonymous/279a884293ef5d6b8d46c036b41c3c42 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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
#![feature(box_syntax, box_patterns)] | |
#![allow(dead_code, unused_variables)] | |
pub trait TraitObject { | |
fn safe_method(&mut self); | |
} | |
pub struct Description { | |
common_field: &'static str, | |
} | |
pub trait Builder { | |
fn describe(&self) -> &'static Description; | |
fn probe(&self, &str) -> bool; | |
fn alloc(&self) -> Box<TraitObject>; | |
} | |
struct Obj1 { | |
internal_field: usize, | |
} | |
impl TraitObject for Obj1 { | |
fn safe_method(&mut self) { | |
println!("Method called {}", self.internal_field); | |
} | |
} | |
static OBJ_DESCR: Description = Description { common_field: "Obj1" }; | |
struct Build1 { | |
} | |
impl Builder for Build1 { | |
fn describe(&self) -> &'static Description { | |
&OBJ_DESCR | |
} | |
fn probe(&self, data: &str) -> bool { | |
true | |
} | |
fn alloc(&self) -> Box<TraitObject> { | |
let obj = Obj1 { internal_field: 0 }; | |
box obj | |
} | |
} | |
const BUILD1: Build1 = Build1 {}; | |
const BUILDERS: [&'static Builder; 1] = [&BUILD1]; | |
fn main() { | |
println!("Probe {}", BUILDERS[0].probe("foo")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment