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

  1. Truncate the iterator before writing: `data.iter().take(self_len())` or resize the source data to the buffer length.
  2. Recompute the buffer size from the data length (`device.create_buffer(size = data.len() * stride)`) before write_iter.
  3. 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

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


AI-assisted analysis of gfx-rs/wgpu@3e11ff59bf (2026-09-03). Data as JSON: /api/errors/28eec93e1477be37. Report an issue: GitHub.