Fix clamped capacity to be divided by element size
* Fix clamped capacity to be divided by element size
Fixes
Also change `clamp` to `min` so this works on Rust 2018 edition.
* Update src/multi/mod.rs
@@ -20,7 +20,8 @@ use core::num::NonZeroUsize;
///
/// This does not affect correctness. Nom will always read the full number
/// of elements regardless of the capacity cap.
-const MAX_INITIAL_CAPACITY: usize = 65536;
+#[cfg(feature = "alloc")]
+const MAX_INITIAL_CAPACITY_BYTES: usize = 65536;
/// Repeats the embedded parser until it fails
/// and returns the results in a `Vec`.
@@ -373,7 +374,8 @@ where
return Err(Err::Failure(E::from_error_kind(input, ErrorKind::ManyMN)));
}
- let mut res = crate::lib::std::vec::Vec::with_capacity(min.clamp(0, MAX_INITIAL_CAPACITY));
+ let max_initial_capacity = MAX_INITIAL_CAPACITY_BYTES / crate::lib::std::mem::size_of::<O>();
+ let mut res = crate::lib::std::vec::Vec::with_capacity(min.min(max_initial_capacity));
for count in 0..max {
let len = input.input_len();
match parse.parse(input.clone()) {
@@ -540,7 +542,8 @@ where
{
move |i: I| {
let mut input = i.clone();
- let mut res = crate::lib::std::vec::Vec::with_capacity(count.clamp(0, MAX_INITIAL_CAPACITY));
+ let max_initial_capacity = MAX_INITIAL_CAPACITY_BYTES / crate::lib::std::mem::size_of::<O>();
+ let mut res = crate::lib::std::vec::Vec::with_capacity(count.min(max_initial_capacity));
for _ in 0..count {
let input_ = input.clone();
