Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. use std::collections::HashMap;
  2. use std::rc::Rc;
  3. use std::any::Any;
  4. use std::marker::PhantomData;
  5.  
  6. // #[derive(From)] with derive_more
  7. enum AttributeData {
  8. FloatPair((f32, f32)),
  9. Int(u32),
  10. }
  11.  
  12. struct Attribute<T> {
  13. name: String,
  14. data: AttributeData,
  15. _phantom: PhantomData<T>,
  16. }
  17.  
  18. impl<T: Into<AttributeData>> Attribute<T> {
  19. fn new(name: &str, data: T) -> Self {
  20. Self {
  21. name: name.to_owned(),
  22. data: data.into(),
  23. _phantom: PhantomData::default(),
  24. }
  25. }
  26.  
  27. fn set(&mut self, data: T) {
  28. self.data = data.into();
  29. }
  30. }
  31.  
  32. fn main() {
  33. let mut attribute: Attribute<(f32, f32)> = Attribute::new("my_name", (22.2, 22.2));
  34. attribute.set((6.6, 9.9));
  35.  
  36. let mut attribute: Attribute<u32> = Attribute::new("my_name", 42);
  37. attribute.set(1337);
  38. }
  39.  
  40. impl From<(f32, f32)> for AttributeData {
  41. fn from(x: (f32, f32)) -> Self {
  42. AttributeData::FloatPair(x)
  43. }
  44. }
  45.  
  46. impl From<u32> for AttributeData {
  47. fn from(x: u32) -> Self {
  48. AttributeData::Int(x)
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement