Async Entity Component System based on the ideas of specs (https://github.com/amethyst/specs)
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

113 linhas
2.6 KiB

  1. pub mod chain;
  2. pub mod cloned;
  3. pub mod collect;
  4. pub mod copied;
  5. pub mod count;
  6. pub mod filter;
  7. pub mod filter_map;
  8. pub mod find;
  9. pub mod flatten;
  10. pub mod fold;
  11. pub mod for_each;
  12. pub mod inspect;
  13. pub mod intersperse;
  14. pub mod map;
  15. pub mod map_init;
  16. pub mod map_with;
  17. pub mod max;
  18. pub mod min;
  19. pub mod noop;
  20. pub mod panic_fuse;
  21. pub mod partition;
  22. pub mod product;
  23. pub mod reduce;
  24. pub mod splits;
  25. pub mod sum;
  26. pub mod try_fold;
  27. pub mod try_for_each;
  28. pub mod try_reduce;
  29. pub mod unzip;
  30. pub mod update;
  31. pub mod while_some;
  32. pub mod zip;
  33. #[cfg(test)]
  34. mod tests {
  35. use crate::*;
  36. #[tokio::test(flavor = "multi_thread")]
  37. async fn test_for_each() {
  38. use ::std::sync::atomic::{AtomicUsize, Ordering};
  39. use ::std::sync::Arc;
  40. let i = Arc::new(AtomicUsize::new(0));
  41. let j = Arc::new(AtomicUsize::new(0));
  42. let a = vec![
  43. vec![1usize, 2usize],
  44. vec![3usize, 4usize],
  45. vec![5usize, 6usize],
  46. ];
  47. let b = vec![
  48. vec![7usize, 8usize],
  49. vec![9usize, 10usize],
  50. vec![11usize, 12usize],
  51. ];
  52. let (x, y, z): (Vec<_>, Vec<_>, Vec<_>) = a
  53. .par_iter()
  54. .cloned()
  55. .chain(b)
  56. .update(|x| x.push(0))
  57. .zip_eq(vec![10usize, 11usize, 12usize, 13usize, 14usize, 15usize])
  58. .map(|x| x.0)
  59. .flatten_iter()
  60. .intersperse(100)
  61. .panic_fuse()
  62. .map(Some)
  63. .while_some()
  64. .map_init(
  65. move || i.fetch_add(1, Ordering::Relaxed),
  66. |init, item| (*init, item),
  67. )
  68. .map_init(
  69. move || j.fetch_add(2, Ordering::Relaxed),
  70. |init, (init2, item)| (*init, init2, item),
  71. )
  72. .with_splits(16)
  73. .inspect(|x| {
  74. println!(
  75. "Thread ID = {:?}; Item = {:?}",
  76. ::std::thread::current().id(),
  77. x
  78. )
  79. })
  80. .partition_map(|(i, j, k)| match j % 3 {
  81. 0 => (Some(i), None, None),
  82. 1 => (None, Some(j), None),
  83. 2 => (None, None, Some(k)),
  84. _ => unreachable!(),
  85. })
  86. .exec()
  87. .await;
  88. dbg!(&x);
  89. dbg!(&y);
  90. dbg!(&z);
  91. }
  92. #[tokio::test]
  93. async fn test_reduce() {
  94. let x = (0..10usize)
  95. .into_par_iter()
  96. .map::<_, Result<usize, ()>>(Ok)
  97. .try_reduce(|| 0, |a, b| Ok(a + b))
  98. .exec()
  99. .await;
  100. dbg!(&x);
  101. assert_eq!(Ok(45), x);
  102. }
  103. }