Guest User

Untitled

a guest
Oct 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. use std::fmt::Debug;
  2. use std::collections::{BTreeMap, BTreeSet};
  3. use std::any::{Any, TypeId};
  4.  
  5.  
  6. trait Component: Debug + Sized + Any {
  7. type Storage: Storage<Self>;
  8. }
  9.  
  10. trait Storage<T: Debug>: Debug + Any {
  11. fn new() -> Self
  12. where
  13. Self: Sized;
  14.  
  15. fn insert(&mut self, value: T);
  16. }
  17.  
  18.  
  19. #[derive(Debug)]
  20. struct VecStorage<T: Debug + 'static> {
  21. internal: Vec<T>,
  22. }
  23. impl<T: Debug> Storage<T> for VecStorage<T> {
  24. fn new() -> Self {
  25. Self {
  26. internal: Vec::new(),
  27. }
  28. }
  29.  
  30. fn insert(&mut self, value: T) {
  31. self.internal.push(value);
  32. }
  33. }
  34.  
  35. #[derive(Debug)]
  36. struct BTreeStorage<T: Debug + 'static + Ord> {
  37. internal: BTreeSet<T>,
  38. }
  39. impl<T: Debug + Ord> Storage<T> for BTreeStorage<T> {
  40. fn new() -> Self {
  41. Self {
  42. internal: BTreeSet::new(),
  43. }
  44. }
  45.  
  46. fn insert(&mut self, value: T) {
  47. self.internal.insert(value);
  48. }
  49. }
  50.  
  51.  
  52. struct StorageMgr {
  53. storages: BTreeMap<TypeId, Box<Any>>,
  54. }
  55.  
  56. impl StorageMgr {
  57. pub fn new() -> Self {
  58. Self {
  59. storages: BTreeMap::new(),
  60. }
  61. }
  62.  
  63. pub fn get_storage_mut<C: Component>(&mut self) -> &mut <C as Component>::Storage {
  64. let type_id = TypeId::of::<C>();
  65.  
  66. if !self.storages.contains_key(&type_id) {
  67. let new_storage = <C as Component>::Storage::new();
  68.  
  69. self.storages.insert(type_id, Box::new(new_storage));
  70. }
  71.  
  72. match self.storages.get_mut(&type_id) {
  73. Some(probably_storage) => {
  74. match probably_storage.downcast_mut::<<C as Component>::Storage>() {
  75. Some(storage) => storage,
  76. None => unreachable!(), // <- you may want to do something less explosive here
  77. }
  78. }
  79. None => unreachable!(),
  80. }
  81. }
  82.  
  83. pub fn get_storage<C: Component>(&self) -> Option<&<C as Component>::Storage> {
  84. let type_id = TypeId::of::<C>();
  85. match self.storages.get(&type_id) {
  86. Some(probably_storage) => {
  87. match probably_storage.downcast_ref::<<C as Component>::Storage>() {
  88. Some(storage) => Some(storage),
  89. None => unreachable!(), // <- you may want to do something less explosive here
  90. }
  91. }
  92. None => None,
  93. }
  94. }
  95. }
  96.  
  97.  
  98. #[derive(Debug)]
  99. struct Pos(f64, f64);
  100.  
  101. impl Component for Pos {
  102. type Storage = VecStorage<Self>;
  103. }
  104.  
  105. #[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
  106. struct AudioVolume(u8);
  107.  
  108. impl Component for AudioVolume {
  109. type Storage = BTreeStorage<Self>;
  110. }
  111.  
  112.  
  113. fn main() {
  114. let mut mgr: StorageMgr = StorageMgr::new();
  115.  
  116. mgr.get_storage_mut::<Pos>().insert(Pos(1.2, 3.4));
  117. mgr.get_storage_mut::<AudioVolume>().insert(AudioVolume(7));
  118.  
  119. println!("{:?}", mgr.get_storage::<Pos>());
  120. println!("{:?}", mgr.get_storage::<AudioVolume>());
  121. }
Add Comment
Please, Sign In to add comment