Async Entity Component System based on the ideas of specs (https://github.com/amethyst/specs)
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

122 рядки
2.5 KiB

  1. use std::iter::{empty, once};
  2. use std::marker::PhantomData;
  3. use crate::{core::Driver, Consumer, Executor, Folder, ParallelIterator, Reducer};
  4. /* Product */
  5. pub struct Product<X, P> {
  6. iterator: X,
  7. marker: PhantomData<P>,
  8. }
  9. impl<X, P> Product<X, P> {
  10. pub fn new(iterator: X) -> Self {
  11. Self {
  12. iterator,
  13. marker: PhantomData,
  14. }
  15. }
  16. }
  17. impl<'a, X, P> Driver<'a, P> for Product<X, P>
  18. where
  19. X: ParallelIterator<'a>,
  20. P: std::iter::Product<X::Item> + std::iter::Product + Send + 'a,
  21. {
  22. fn exec_with<E>(self, executor: E) -> E::Result
  23. where
  24. E: Executor<'a, P>,
  25. {
  26. let iterator = self.iterator;
  27. let consumer = ProductConsumer(PhantomData);
  28. iterator.drive(executor, consumer)
  29. }
  30. }
  31. /* ProductConsumer */
  32. pub struct ProductConsumer<P>(PhantomData<P>);
  33. impl<P, I> Consumer<I> for ProductConsumer<P>
  34. where
  35. P: std::iter::Product<I> + std::iter::Product + Send,
  36. {
  37. type Folder = ProductFolder<P>;
  38. type Reducer = ProductReducer<P>;
  39. type Result = P;
  40. fn split(self) -> (Self, Self, Self::Reducer) {
  41. let left = self;
  42. let right = ProductConsumer(PhantomData);
  43. (left, right, ProductReducer(PhantomData))
  44. }
  45. fn split_at(self, _index: usize) -> (Self, Self, Self::Reducer) {
  46. let left = self;
  47. let right = ProductConsumer(PhantomData);
  48. (left, right, ProductReducer(PhantomData))
  49. }
  50. fn into_folder(self) -> Self::Folder {
  51. ProductFolder {
  52. product: empty::<I>().product(),
  53. }
  54. }
  55. }
  56. /* ProductFolder */
  57. pub struct ProductFolder<P> {
  58. product: P,
  59. }
  60. impl<P, I> Folder<I> for ProductFolder<P>
  61. where
  62. P: std::iter::Product<I> + std::iter::Product + Send,
  63. {
  64. type Result = P;
  65. fn consume(mut self, item: I) -> Self {
  66. self.product = mul(self.product, once(item).product());
  67. self
  68. }
  69. fn consume_iter<X>(mut self, iter: X) -> Self
  70. where
  71. X: IntoIterator<Item = I>,
  72. {
  73. self.product = mul(self.product, iter.into_iter().product());
  74. self
  75. }
  76. fn complete(self) -> Self::Result {
  77. self.product
  78. }
  79. }
  80. /* ProductReducer */
  81. pub struct ProductReducer<P>(PhantomData<P>);
  82. impl<P> Reducer<P> for ProductReducer<P>
  83. where
  84. P: std::iter::Product + Send,
  85. {
  86. fn reduce(self, left: P, right: P) -> P {
  87. mul(left, right)
  88. }
  89. }
  90. fn mul<T>(left: T, right: T) -> T
  91. where
  92. T: std::iter::Product,
  93. {
  94. once(left).chain(once(right)).product()
  95. }