diff --git a/src/base/default_allocator.rs b/src/base/default_allocator.rs index 3c132413..569498af 100644 --- a/src/base/default_allocator.rs +++ b/src/base/default_allocator.rs @@ -121,9 +121,13 @@ impl Allocator for DefaultAllocator { ncols: C, iter: I, ) -> Self::Buffer { - let it = iter.into_iter(); + let size = nrows.value() * ncols.value(); + // This solution is not too elegant and meant to prevent an infinite + // loop for infinite iterators and trigger the assertion instead. See + // the tests below for more details. + let it = iter.into_iter().take(size + 1); let res: Vec = it.collect(); - assert!(res.len() == nrows.value() * ncols.value(), + assert!(res.len() == size, "Allocation from iterator error: the iterator did not yield the correct number of elements."); VecStorage::new(nrows, ncols, res) @@ -167,9 +171,13 @@ impl Allocator for DefaultAllocator { ncols: Dyn, iter: I, ) -> Self::Buffer { - let it = iter.into_iter(); + let size = nrows.value() * ncols.value(); + // This solution is not too elegant and meant to prevent an infinite + // loop for infinite iterators and trigger the assertion instead. See + // the tests below for more details. + let it = iter.into_iter().take(size + 1); let res: Vec = it.collect(); - assert!(res.len() == nrows.value() * ncols.value(), + assert!(res.len() == size, "Allocation from iterator error: the iterator did not yield the correct number of elements."); VecStorage::new(nrows, ncols, res) @@ -333,3 +341,65 @@ impl Reallocator, + Dyn(100), + iter::from_fn(|| Some(0)), + ); + } + + #[test] + #[should_panic( + message = "Allocation from iterator error: the iterator did not yield the correct number of elements." + )] + fn test_allocation_dyn_const() { + let _ = DefaultAllocator::allocate_from_iterator( + Dyn(50), + Const::<50>, + iter::from_fn(|| Some(0)), + ); + } + + #[test] + #[should_panic( + message = "Allocation from iterator error: the iterator did not yield the correct number of elements." + )] + fn test_allocation_dyn_dyn() { + let _ = DefaultAllocator::allocate_from_iterator( + Dyn(10), + Dyn(10), + iter::from_fn(|| Some(0)), + ); + } + } +}