Skip to content

Instantly share code, notes, and snippets.

Created July 23, 2016 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/279a884293ef5d6b8d46c036b41c3c42 to your computer and use it in GitHub Desktop.
Save anonymous/279a884293ef5d6b8d46c036b41c3c42 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
#![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