Skip to content

Instantly share code, notes, and snippets.

@julienw
Created February 23, 2016 08:02
Show Gist options
  • Save julienw/f9b04309172e91ad786d to your computer and use it in GitHub Desktop.
Save julienw/f9b04309172e91ad786d to your computer and use it in GitHub Desktop.
Interface factory
use value_classes::value_id::{ ValueType, ValueID };
trait Value<T> {
fn get() -> Result<T, ()>;
fn set(value: T) -> Result<(), ()>;
}
struct ValueBool {
value_id: ValueID
}
impl Value<bool> for ValueBool {
fn get() -> Result<bool, ()> {
unimplemented!()
}
fn set(value: bool) -> Result<(), ()> {
unimplemented!()
}
}
struct ValueFloat {
value_id: ValueID
}
impl Value<f32> for ValueFloat {
fn get() -> Result<f32, ()> {
unimplemented!()
}
fn set(value: f32) -> Result<(), ()> {
unimplemented!()
}
}
pub fn instance_for<T>(value_id: ValueID) -> Value<T> {
match value_id.get_type() {
ValueType::ValueType_Bool => {
ValueBool {
value_id: value_id
}
},
ValueType::ValueType_Decimal => {
ValueFloat {
value_id: value_id
}
},
_ => unimplemented!()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment