{"record":{"id":"093a08c6514a1ed0","repo":"gfx-rs/wgpu","slug":"iterator-given-to-write-iter-produced-iter-len","errorCode":null,"errorMessage":"iterator given to write_iter() produced {iter_len} elements but must produce {self_len} elements","messagePattern":"iterator given to write_iter\\(\\) produced (.+?) elements but must produce (.+?) elements","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"wgpu-types/src/write_only.rs","lineNumber":303,"sourceCode":"    {\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    ///\n    /// # Example\n    ///\n    /// ```\n    /// # use wgpu_types as wgpu;\n    /// // Ordinarily you would get a `WriteOnly` from `wgpu::Buffer` instead.\n    /// let mut buf = vec![0; 10];\n    /// let mut wo = wgpu::WriteOnly::from_mut(buf.as_mut_slice());\n    ///","sourceCodeStart":285,"sourceCodeEnd":321,"githubUrl":"https://github.com/gfx-rs/wgpu/blob/3e11ff59bf3f9795d285ecc045014089640d7248/wgpu-types/src/write_only.rs#L285-L321","documentation":"After consuming the user's iterator, `write_iter` verifies that every slot in the fixed-size buffer was filled; if slots remain, the iterator produced fewer than `self_len` elements and this panic fires. Unlike the over-length case, the count is inferred from unconsumed slots and reported in the message. Partial writes would leave stale data in the buffer, so they are rejected.","triggerScenarios":"Calling `write_iter` with an iterator that yields fewer elements than the buffer's slot count (`self_len`).","commonSituations":"Filtering iterators silently dropping elements (`.filter(...)`) before the write; short reads from a source collection; buffer allocated for a maximum count but the actual batch is smaller.","solutions":["Ensure the iterator yields exactly `self_len` elements: pad missing items with defaults (e.g. `.chain(std::iter::repeat(default))`) and take(self_len).","Don't filter before writing; pre-materialize a correctly-sized Vec and assert its length equals the buffer's element count.","Resize the buffer to match the actual data length instead of over-allocating."],"exampleFix":"// before\nbuffer.write_iter(data.iter().filter(|x| x.valid).copied());\n// after\nlet filtered: Vec<_> = data.iter().filter(|x| x.valid).copied().collect();\nassert_eq!(filtered.len(), buffer.len());\nbuffer.write_iter(filtered);","handlingStrategy":"validation","validationCode":"let capacity = buffer.size() as usize / item_size;\nassert_eq!(\n    data.len(), capacity,\n    \"write_iter: got {} items, buffer holds {}\",\n    data.len(), capacity\n);\nbuffer.write_iter(data.iter().copied());","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid filtering iterators directly before write_iter; materialize and check the length first.","Pad under-filled batches with default values to the buffer's slot count.","Derive buffer size from the actual data length, not a maximum bound."],"tags":["buffer","iterator","length-mismatch","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"}