Created
September 27, 2018 15:53
-
-
Save rust-play/a813331a3627d0e7942adf6679c242c0 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or 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
#![allow(unused)] | |
mod generic { | |
pub struct Scalar<T: Default> { | |
value: T, | |
} | |
impl<T: Default> Scalar<T> { | |
pub fn new() -> Self { | |
Scalar { | |
value: Default::default(), | |
} | |
} | |
pub fn set(&mut self, value: T) { | |
self.value = value; | |
} | |
pub fn get(&self) -> &T { | |
&self.value | |
} | |
} | |
pub type StringScalar = Scalar<String>; | |
pub type BoolScalar = Scalar<bool>; | |
pub type UintScalar = Scalar<u32>; | |
pub fn create<T: Default>() -> Scalar<T> { | |
Scalar::new() | |
} | |
} | |
mod traited { | |
pub trait Scalar { | |
type Type; | |
fn set(&mut self, value: Self::Type); | |
fn get(&self) -> &Self::Type; | |
} | |
struct StringScalar(String); | |
impl Scalar for StringScalar { | |
type Type = String; | |
fn set(&mut self, value: Self::Type) { | |
self.0 = value; | |
} | |
fn get(&self) -> &Self::Type { | |
&self.0 | |
} | |
} | |
struct BoolScalar(bool); | |
impl Scalar for BoolScalar { | |
type Type = bool; | |
fn set(&mut self, value: Self::Type) { | |
self.0 = value; | |
} | |
fn get(&self) -> &Self::Type { | |
&self.0 | |
} | |
} | |
struct UintScalar(u32); | |
impl Scalar for UintScalar { | |
type Type = u32; | |
fn set(&mut self, value: Self::Type) { | |
self.0 = value; | |
} | |
fn get(&self) -> &Self::Type { | |
&self.0 | |
} | |
} | |
#[derive(Debug, Clone, Copy)] | |
pub enum ScalarType { | |
String, | |
Bool, | |
Unsigned, | |
} | |
pub fn create(typ: ScalarType) -> Box<Scalar<Type = ()>> { | |
return match typ { | |
_ => unimplemented!(), | |
}; | |
} | |
} | |
mod enumed { | |
use ::std::mem; | |
pub trait StringSetter { fn set(&mut self, val: String); } | |
pub trait BoolSetter { fn set(&mut self, val: bool); } | |
pub trait UintSetter { fn set(&mut self, val: u32); } | |
#[derive(Debug)] | |
pub enum Scalar { | |
String(String), | |
Bool(bool), | |
Unsigned(u32), | |
} | |
impl StringSetter for Scalar { | |
fn set(&mut self, val: String) { | |
match self { | |
Scalar::String(ref mut s) => mem::replace(s, val), | |
_ => panic!("Wrong type!"), | |
}; | |
} | |
} | |
impl BoolSetter for Scalar { | |
fn set(&mut self, val: bool) { | |
match self { | |
Scalar::Bool(ref mut b) => *b = val, | |
_ => panic!("Wrong type!"), | |
}; | |
} | |
} | |
impl UintSetter for Scalar { | |
fn set(&mut self, val: u32) { | |
match self { | |
Scalar::Unsigned(ref mut i) => *i = val, | |
_ => panic!("Wrong type!"), | |
}; | |
} | |
} | |
#[derive(Debug, Clone, Copy)] | |
pub enum ScalarType { | |
String, | |
Bool, | |
Unsigned, | |
} | |
pub fn create(typ: ScalarType) -> Scalar { | |
return match typ { | |
ScalarType::String => Scalar::String(String::new()), | |
ScalarType::Bool => Scalar::Bool(false), | |
ScalarType::Unsigned => Scalar::Unsigned(0), | |
}; | |
} | |
} | |
fn main() { | |
{ | |
let mut b = generic::create::<bool>(); | |
b.set(true); | |
// b.set(42); // will fail | |
let mut s = generic::StringScalar::new(); | |
s.set("foobar".into()); | |
} | |
{ | |
use enumed::*; | |
let mut b = create(ScalarType::Bool); | |
BoolSetter::set(&mut b, true); | |
// UintSetter::set(&mut b, 42); // Will panic at runtime | |
let mut v = Vec::new(); | |
v.push(create(ScalarType::Bool)); | |
v.push(create(ScalarType::Unsigned)); | |
BoolSetter::set(&mut v[0], true); | |
UintSetter::set(&mut v[1], 42); | |
println!("Scalars: {:?}", v); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment