{"record":{"id":"28eec93e1477be37","repo":"gfx-rs/wgpu","slug":"iterator-given-to-write-iter-produced-more-than","errorCode":null,"errorMessage":"iterator given to write_iter() produced more than {self_len} elements","messagePattern":"iterator given to write_iter\\(\\) produced more than (.+?) elements","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"wgpu-types/src/write_only.rs","lineNumber":295,"sourceCode":"    /// assert_eq!(buf, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);\n    /// ```\n    #[inline]\n    #[track_caller]\n    pub fn write_iter<I>(self, iter: I)\n    where\n        T: Copy, // required by write()\n        I: IntoIterator<Item = T>,\n    {\n        let self_len = self.len();\n        let mut slot_iter = self.into_iter();\n\n        // Call `for_each()` to take advantage of the iterator’s custom implementation, if it has\n        // one. This may be superior to a `for` loop for `chain()`ed iterators and other cases where\n        // the implementation of `Iterator::next()` would need to branch, and is typically\n        // equivalent to a `for` loop for other iterators.\n        iter.into_iter().for_each(|item| {\n            let Some(slot) = slot_iter.next() else {\n                panic!(\"iterator given to write_iter() produced more than {self_len} elements\");\n            };\n\n            slot.write(item);\n        });\n\n        let remaining_len = slot_iter.len();\n        if remaining_len != 0 {\n            panic!(\n                \"iterator given to write_iter() produced {iter_len} elements \\\n                    but must produce {self_len} elements\",\n                // infer how many elements the iterator produced by how many of ours were consumed\n                iter_len = self_len - remaining_len,\n            );\n        };\n    }\n\n    /// Writes copies of `value` to every element of `self`.\n    ///","sourceCodeStart":277,"sourceCodeEnd":313,"githubUrl":"https://github.com/gfx-rs/wgpu/blob/3e11ff59bf3f9795d285ecc045014089640d7248/wgpu-types/src/write_only.rs#L277-L313","documentation":"`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.","triggerScenarios":"Calling `write_iter` (on a write-only buffer type in wgpu-types) with an iterator producing more than `self_len` elements.","commonSituations":"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.","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."],"exampleFix":"// before\nbuffer.write_iter(data.iter().copied());\n// after\nbuffer.write_iter(data.iter().copied().take(buffer.size() as usize / item_size));","handlingStrategy":"validation","validationCode":"let capacity = buffer.size() as usize / item_size;\nassert!(\n    data.len() <= capacity,\n    \"write_iter: {} items exceed buffer capacity {}\",\n    data.len(), capacity\n);\nbuffer.write_iter(data.iter().copied());","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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)."],"tags":["buffer","iterator","out-of-bounds","panic"],"backgroundTag":"iterator-length-mismatch","analyzedSha":"3e11ff59bf3f9795d285ecc045014089640d7248","analyzedAt":"2026-09-03T01:43:21.459Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T07:17:11.731Z"}