gfx-rs/wgpu · error
iterator given to write_iter() produced more than {self_len}
Error message
iterator given to write_iter() produced more than {self_len} elements What it means
`write_iter` copies items from a user-supplied iterator into a fixed-size buffer of `self_len` slots. If the iterator yields more items than there are slots, this panic fires immediately, since writing past the buffer would be memory unsafety. The buffer length acts as a strict upper bound on the iterator's output.
Source
Thrown at wgpu-types/src/write_only.rs:295
/// assert_eq!(buf, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
/// ```
#[inline]
#[track_caller]
pub fn write_iter<I>(self, iter: I)
where
T: Copy, // required by write()
I: IntoIterator<Item = T>,
{
let self_len = self.len();
let mut slot_iter = self.into_iter();
// Call `for_each()` to take advantage of the iterator’s custom implementation, if it has
// one. This may be superior to a `for` loop for `chain()`ed iterators and other cases where
// the implementation of `Iterator::next()` would need to branch, and is typically
// equivalent to a `for` loop for other iterators.
iter.into_iter().for_each(|item| {
let Some(slot) = slot_iter.next() else {
panic!("iterator given to write_iter() produced more than {self_len} elements");
};
slot.write(item);
});
let remaining_len = slot_iter.len();
if remaining_len != 0 {
panic!(
"iterator given to write_iter() produced {iter_len} elements \
but must produce {self_len} elements",
// infer how many elements the iterator produced by how many of ours were consumed
iter_len = self_len - remaining_len,
);
};
}
/// Writes copies of `value` to every element of `self`.
///View on GitHub (pinned to 3e11ff59bf)
Solutions
- Truncate the iterator before writing: `data.iter().take(self_len())` or resize the source data to the buffer length.
- Recompute the buffer size from the data length (`device.create_buffer(size = data.len() * stride)`) before write_iter.
- Use `write` with a slice instead, which validates length up front with a clearer error.
Example fix
// before buffer.write_iter(data.iter().copied()); // after buffer.write_iter(data.iter().copied().take(buffer.size() as usize / item_size));
Defensive patterns
Strategy: validation
Validate before calling
let capacity = buffer.size() as usize / item_size;
assert!(
data.len() <= capacity,
"write_iter: {} items exceed buffer capacity {}",
data.len(), capacity
);
buffer.write_iter(data.iter().copied()); Prevention
- Size buffers from data.len() * stride at creation time.
- Prefer write() with a slice for exact-length checking up front.
- Re-check buffer size whenever the data source can grow (dynamic records).
When it happens
Trigger: Calling `write_iter` (on a write-only buffer type in wgpu-types) with an iterator producing more than `self_len` elements.
Common situations: Buffer sized from stale metadata while data came from a longer source (e.g. record count changed, header not accounted); chaining or mapping iterators that duplicate elements; off-by-one in computed capacity.
Related errors
- iterator given to write_iter() produced {iter_len} elements
- Buffers are required to build acceleration structures
- slice offset {slice_offset} is out of range for buffer of si
- slice offset {slice_offset} size {slice_size} is out of rang
- wgpu error: {err}
AI-assisted analysis of gfx-rs/wgpu@3e11ff59bf (2026-09-03).
Data as JSON: /api/errors/28eec93e1477be37.
Report an issue: GitHub.