Async Entity Component System based on the ideas of specs (https://github.com/amethyst/specs)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 line
1.9 KiB

  1. use crate::{core::Driver, Consumer, Executor, Folder, ParallelIterator, WithSetup};
  2. use super::noop::NoOpReducer;
  3. pub struct ForEach<X, O> {
  4. iterator: X,
  5. operation: O,
  6. }
  7. impl<X, O> ForEach<X, O> {
  8. pub fn new(iterator: X, operation: O) -> Self {
  9. Self {
  10. iterator,
  11. operation,
  12. }
  13. }
  14. }
  15. impl<'a, X, O> Driver<'a, ()> for ForEach<X, O>
  16. where
  17. X: ParallelIterator<'a>,
  18. O: Fn(X::Item) + Clone + Send + 'a,
  19. {
  20. fn exec_with<E>(self, executor: E) -> E::Result
  21. where
  22. E: Executor<'a, ()>,
  23. {
  24. let iterator = self.iterator;
  25. let operation = self.operation;
  26. let consumer = ForEachConsumer { operation };
  27. iterator.drive(executor, consumer)
  28. }
  29. }
  30. pub struct ForEachConsumer<O> {
  31. operation: O,
  32. }
  33. impl<O> WithSetup for ForEachConsumer<O> {}
  34. impl<O, I> Consumer<I> for ForEachConsumer<O>
  35. where
  36. O: Fn(I) + Clone + Send,
  37. {
  38. type Folder = ForEachConsumer<O>;
  39. type Reducer = NoOpReducer;
  40. type Result = ();
  41. fn split(self) -> (Self, Self, NoOpReducer) {
  42. let left = self;
  43. let right = ForEachConsumer {
  44. operation: left.operation.clone(),
  45. };
  46. (left, right, NoOpReducer)
  47. }
  48. fn split_at(self, _index: usize) -> (Self, Self, NoOpReducer) {
  49. let left = self;
  50. let right = ForEachConsumer {
  51. operation: left.operation.clone(),
  52. };
  53. (left, right, NoOpReducer)
  54. }
  55. fn into_folder(self) -> Self {
  56. self
  57. }
  58. }
  59. impl<O, I> Folder<I> for ForEachConsumer<O>
  60. where
  61. O: Fn(I),
  62. {
  63. type Result = ();
  64. fn consume(self, item: I) -> Self {
  65. (self.operation)(item);
  66. self
  67. }
  68. fn consume_iter<X>(self, iter: X) -> Self
  69. where
  70. X: IntoIterator<Item = I>,
  71. {
  72. iter.into_iter().for_each(&self.operation);
  73. self
  74. }
  75. fn complete(self) {}
  76. }