util.rs 619 B

123456789101112131415161718
  1. /// Takes in an FnMut closure and returns a constant-length array with elements of
  2. /// type `Output`.
  3. pub fn gen_const_array<Output: Copy + Default, const LEN: usize>(
  4. closure: impl FnMut(usize) -> Output,
  5. ) -> [Output; LEN] {
  6. gen_const_array_with_default(Default::default(), closure)
  7. }
  8. pub(crate) fn gen_const_array_with_default<Output: Copy, const LEN: usize>(
  9. default_value: Output,
  10. closure: impl FnMut(usize) -> Output,
  11. ) -> [Output; LEN] {
  12. let mut ret: [Output; LEN] = [default_value; LEN];
  13. for (bit, val) in ret.iter_mut().zip((0..LEN).map(closure)) {
  14. *bit = val;
  15. }
  16. ret
  17. }