container.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. use std::{
  2. any::{Any, TypeId},
  3. collections::HashMap,
  4. };
  5. #[derive(Default)]
  6. pub struct DataContainer {
  7. map: HashMap<TypeId, Box<dyn Any + Sync + Send>>,
  8. }
  9. impl DataContainer {
  10. #[inline]
  11. pub fn new() -> DataContainer {
  12. DataContainer {
  13. map: HashMap::default(),
  14. }
  15. }
  16. pub fn insert<T>(&mut self, val: T) -> Option<T>
  17. where
  18. T: 'static + Send + Sync,
  19. {
  20. self.map
  21. .insert(TypeId::of::<T>(), Box::new(val))
  22. .and_then(downcast_owned)
  23. }
  24. pub fn remove<T>(&mut self) -> Option<T>
  25. where
  26. T: 'static + Send + Sync,
  27. {
  28. self.map.remove(&TypeId::of::<T>()).and_then(downcast_owned)
  29. }
  30. pub fn get<T>(&self) -> Option<&T>
  31. where
  32. T: 'static + Send + Sync,
  33. {
  34. self.map
  35. .get(&TypeId::of::<T>())
  36. .and_then(|boxed| boxed.downcast_ref())
  37. }
  38. pub fn get_mut<T>(&mut self) -> Option<&mut T>
  39. where
  40. T: 'static + Send + Sync,
  41. {
  42. self.map
  43. .get_mut(&TypeId::of::<T>())
  44. .and_then(|boxed| boxed.downcast_mut())
  45. }
  46. pub fn contains<T>(&self) -> bool
  47. where
  48. T: 'static + Send + Sync,
  49. {
  50. self.map.contains_key(&TypeId::of::<T>())
  51. }
  52. pub fn extend(&mut self, other: DataContainer) { self.map.extend(other.map); }
  53. }
  54. fn downcast_owned<T: 'static + Send + Sync>(boxed: Box<dyn Any + Send + Sync>) -> Option<T> {
  55. boxed.downcast().ok().map(|boxed| *boxed)
  56. }
  57. // use std::{
  58. // any::{Any, TypeId},
  59. // collections::HashMap,
  60. // sync::RwLock,
  61. // };
  62. //
  63. // #[derive(Default)]
  64. // pub struct DataContainer {
  65. // map: RwLock<HashMap<TypeId, Box<dyn Any>>>,
  66. // }
  67. //
  68. // impl DataContainer {
  69. // #[inline]
  70. // pub fn new() -> DataContainer {
  71. // DataContainer {
  72. // map: RwLock::new(HashMap::default()),
  73. // }
  74. // }
  75. //
  76. // pub fn insert<T: 'static>(&mut self, val: T) -> Option<T> {
  77. // self.map
  78. // .write()
  79. // .unwrap()
  80. // .insert(TypeId::of::<T>(), Box::new(val))
  81. // .and_then(downcast_owned)
  82. // }
  83. //
  84. // pub fn remove<T: 'static>(&mut self) -> Option<T> {
  85. // self.map
  86. // .write()
  87. // .unwrap()
  88. // .remove(&TypeId::of::<T>())
  89. // .and_then(downcast_owned)
  90. // }
  91. //
  92. // pub fn get<T: 'static>(&self) -> Option<&T> {
  93. // self.map
  94. // .read()
  95. // .unwrap()
  96. // .get(&TypeId::of::<T>())
  97. // .and_then(|boxed| boxed.downcast_ref())
  98. // }
  99. //
  100. // pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T> {
  101. // self.map
  102. // .write()
  103. // .unwrap()
  104. // .get_mut(&TypeId::of::<T>())
  105. // .and_then(|boxed| boxed.downcast_mut())
  106. // }
  107. //
  108. // pub fn contains<T: 'static>(&self) -> bool {
  109. // self.map.read().unwrap().contains_key(&TypeId::of::<T>())
  110. // }
  111. //
  112. // pub fn extend(&mut self, other: DataContainer) {
  113. // self.map.write().unwrap().extend(other.map); } }
  114. //
  115. // fn downcast_owned<T: 'static>(boxed: Box<dyn Any>) -> Option<T> {
  116. // boxed.downcast().ok().map(|boxed| *boxed)
  117. // }