Async Entity Component System based on the ideas of specs (https://github.com/amethyst/specs)
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

282 řádky
6.0 KiB

  1. use crate::{
  2. Consumer, Executor, ExecutorCallback, IndexedParallelIterator, IndexedProducer,
  3. IndexedProducerCallback, IntoParallelIterator, ParallelIterator, Producer, ProducerCallback,
  4. Reducer,
  5. };
  6. impl<'a, T> IntoParallelIterator<'a> for &'a [T]
  7. where
  8. T: Send + Sync,
  9. {
  10. type Iter = Iter<'a, T>;
  11. type Item = &'a T;
  12. fn into_par_iter(self) -> Self::Iter {
  13. Iter { slice: self }
  14. }
  15. }
  16. impl<'a, T> IntoParallelIterator<'a> for &'a mut [T]
  17. where
  18. T: Send + Sync,
  19. {
  20. type Iter = IterMut<'a, T>;
  21. type Item = &'a mut T;
  22. fn into_par_iter(self) -> Self::Iter {
  23. IterMut { slice: self }
  24. }
  25. }
  26. impl<'a, T> IntoParallelIterator<'a> for &'a Vec<T>
  27. where
  28. T: Send + Sync,
  29. {
  30. type Iter = Iter<'a, T>;
  31. type Item = &'a T;
  32. fn into_par_iter(self) -> Self::Iter {
  33. Iter { slice: self }
  34. }
  35. }
  36. impl<'a, T> IntoParallelIterator<'a> for &'a mut Vec<T>
  37. where
  38. T: Send + Sync,
  39. {
  40. type Iter = IterMut<'a, T>;
  41. type Item = &'a mut T;
  42. fn into_par_iter(self) -> Self::Iter {
  43. IterMut { slice: self }
  44. }
  45. }
  46. /* Iter */
  47. pub struct Iter<'a, T> {
  48. slice: &'a [T],
  49. }
  50. impl<'a, T> ParallelIterator<'a> for Iter<'a, T>
  51. where
  52. T: Send + Sync,
  53. {
  54. type Item = &'a T;
  55. fn drive<E, C, D, R>(self, executor: E, consumer: C) -> E::Result
  56. where
  57. E: Executor<'a, D>,
  58. C: Consumer<Self::Item, Result = D, Reducer = R> + 'a,
  59. D: Send,
  60. R: Reducer<D> + Send,
  61. {
  62. self.with_producer_indexed(ExecutorCallback::new(executor, consumer))
  63. }
  64. fn with_producer<CB>(self, callback: CB) -> CB::Output
  65. where
  66. CB: ProducerCallback<'a, Self::Item>,
  67. {
  68. callback.callback(IterProducer { slice: self.slice })
  69. }
  70. fn len_hint_opt(&self) -> Option<usize> {
  71. Some(self.slice.len())
  72. }
  73. }
  74. impl<'a, T> IndexedParallelIterator<'a> for Iter<'a, T>
  75. where
  76. T: Send + Sync,
  77. {
  78. fn drive_indexed<E, C, D, R>(self, executor: E, consumer: C) -> E::Result
  79. where
  80. E: Executor<'a, D>,
  81. C: Consumer<Self::Item, Result = D, Reducer = R> + 'a,
  82. D: Send,
  83. R: Reducer<D> + Send,
  84. {
  85. self.with_producer_indexed(ExecutorCallback::new(executor, consumer))
  86. }
  87. fn with_producer_indexed<CB>(self, callback: CB) -> CB::Output
  88. where
  89. CB: IndexedProducerCallback<'a, Self::Item>,
  90. {
  91. callback.callback(IterProducer { slice: self.slice })
  92. }
  93. fn len_hint(&self) -> usize {
  94. self.slice.len()
  95. }
  96. }
  97. /* IterProducer */
  98. struct IterProducer<'a, T> {
  99. slice: &'a [T],
  100. }
  101. impl<'a, T> Producer for IterProducer<'a, T>
  102. where
  103. T: Send + Sync,
  104. {
  105. type Item = &'a T;
  106. type IntoIter = std::slice::Iter<'a, T>;
  107. fn into_iter(self) -> Self::IntoIter {
  108. self.slice.iter()
  109. }
  110. fn split(self) -> (Self, Option<Self>) {
  111. if self.slice.len() < 2 {
  112. (self, None)
  113. } else {
  114. let mid = self.slice.len() / 2;
  115. let (left, right) = self.split_at(mid);
  116. (left, Some(right))
  117. }
  118. }
  119. }
  120. impl<'a, T> IndexedProducer for IterProducer<'a, T>
  121. where
  122. T: Send + Sync,
  123. {
  124. type Item = &'a T;
  125. type IntoIter = std::slice::Iter<'a, T>;
  126. fn into_iter(self) -> Self::IntoIter {
  127. self.slice.iter()
  128. }
  129. fn len(&self) -> usize {
  130. self.slice.len()
  131. }
  132. fn split_at(self, index: usize) -> (Self, Self) {
  133. let (left, right) = self.slice.split_at(index);
  134. let left = IterProducer { slice: left };
  135. let right = IterProducer { slice: right };
  136. (left, right)
  137. }
  138. }
  139. /* IterMut */
  140. pub struct IterMut<'a, T> {
  141. slice: &'a mut [T],
  142. }
  143. impl<'a, T> ParallelIterator<'a> for IterMut<'a, T>
  144. where
  145. T: Send + Sync,
  146. {
  147. type Item = &'a mut T;
  148. fn drive<E, C, D, R>(self, executor: E, consumer: C) -> E::Result
  149. where
  150. E: Executor<'a, D>,
  151. C: Consumer<Self::Item, Result = D, Reducer = R> + 'a,
  152. D: Send,
  153. R: Reducer<D> + Send,
  154. {
  155. self.with_producer_indexed(ExecutorCallback::new(executor, consumer))
  156. }
  157. fn with_producer<CB>(self, callback: CB) -> CB::Output
  158. where
  159. CB: ProducerCallback<'a, Self::Item>,
  160. {
  161. callback.callback(IterMutProducer { slice: self.slice })
  162. }
  163. fn len_hint_opt(&self) -> Option<usize> {
  164. Some(self.slice.len())
  165. }
  166. }
  167. impl<'a, T> IndexedParallelIterator<'a> for IterMut<'a, T>
  168. where
  169. T: Send + Sync,
  170. {
  171. fn drive_indexed<E, C, D, R>(self, executor: E, consumer: C) -> E::Result
  172. where
  173. E: Executor<'a, D>,
  174. C: Consumer<Self::Item, Result = D, Reducer = R> + 'a,
  175. D: Send,
  176. R: Reducer<D> + Send,
  177. {
  178. self.with_producer_indexed(ExecutorCallback::new(executor, consumer))
  179. }
  180. fn with_producer_indexed<CB>(self, callback: CB) -> CB::Output
  181. where
  182. CB: IndexedProducerCallback<'a, Self::Item>,
  183. {
  184. callback.callback(IterMutProducer { slice: self.slice })
  185. }
  186. fn len_hint(&self) -> usize {
  187. self.slice.len()
  188. }
  189. }
  190. /* IterMutProducer */
  191. struct IterMutProducer<'a, T> {
  192. slice: &'a mut [T],
  193. }
  194. impl<'a, T> Producer for IterMutProducer<'a, T>
  195. where
  196. T: Send + Sync,
  197. {
  198. type Item = &'a mut T;
  199. type IntoIter = std::slice::IterMut<'a, T>;
  200. fn into_iter(self) -> Self::IntoIter {
  201. self.slice.iter_mut()
  202. }
  203. fn split(self) -> (Self, Option<Self>) {
  204. if self.slice.len() < 2 {
  205. (self, None)
  206. } else {
  207. let mid = self.slice.len() / 2;
  208. let (left, right) = self.split_at(mid);
  209. (left, Some(right))
  210. }
  211. }
  212. }
  213. impl<'a, T> IndexedProducer for IterMutProducer<'a, T>
  214. where
  215. T: Send + Sync,
  216. {
  217. type Item = &'a mut T;
  218. type IntoIter = std::slice::IterMut<'a, T>;
  219. fn into_iter(self) -> Self::IntoIter {
  220. self.slice.iter_mut()
  221. }
  222. fn len(&self) -> usize {
  223. self.slice.len()
  224. }
  225. fn split_at(self, index: usize) -> (Self, Self) {
  226. let (left, right) = self.slice.split_at_mut(index);
  227. let left = IterMutProducer { slice: left };
  228. let right = IterMutProducer { slice: right };
  229. (left, right)
  230. }
  231. }