{"record":{"id":"c6b1df9c188fedfb","repo":"gfx-rs/wgpu","slug":"source-slice-length-src-len-does-not-match-des","errorCode":null,"errorMessage":"source slice length ({src_len}) does not match destination slice length ({dst_len})","messagePattern":"source slice length \\((.+?)\\) does not match destination slice length \\((.+?)\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"wgpu-types/src/write_only.rs","lineNumber":391,"sourceCode":"    /// let mut wo = wgpu::WriteOnly::from_mut(buf.as_mut_slice());\n    ///\n    /// wo.copy_from_slice(&[2, 3, 5, 7, 11]);\n    ///\n    /// assert_eq!(*buf, [2, 3, 5, 7, 11]);\n    #[inline]\n    #[track_caller]\n    pub fn copy_from_slice(&mut self, src: &[T])\n    where\n        // Ideally, we want \"does not have a destructor\" to avoid any need for dropping (which\n        // would imply reading) or forgetting the values that write operations overwrite.\n        // However, there is no such trait bound and `T: Copy` is the closest approximation.\n        T: Copy,\n    {\n        let src_len = src.len();\n        let dst_len = self.len();\n        if src_len != dst_len {\n            // wording chosen to match <[_]>::copy_from_slice()'s message\n            panic!(\n                \"source slice length ({src_len}) does not match \\\n                    destination slice length ({dst_len})\"\n            );\n        }\n\n        let src_ptr: *const T = src.as_ptr();\n        let dst_ptr: *mut T = self.as_raw_element_ptr().as_ptr();\n\n        // SAFETY:\n        // * `src_ptr` is readable because it was constructed from a reference.\n        // * `dst_ptr` is writable because that is an invariant of `WriteOnly`.\n        // * `dst_ptr` cannot alias `src_ptr` because `self` is exclusive *and*\n        //   because `src_ptr` is immutable.\n        // * We checked that the byte lengths match.\n        // * Lack of data races will be enforced by the type\n        unsafe { dst_ptr.copy_from_nonoverlapping(src_ptr, src.len()) }\n    }\n","sourceCodeStart":373,"sourceCodeEnd":409,"githubUrl":"https://github.com/gfx-rs/wgpu/blob/3e11ff59bf3f9795d285ecc045014089640d7248/wgpu-types/src/write_only.rs#L373-L409","documentation":"WriteOnly<[T]>::copy_from_slice panics when the source slice length differs from the length of the destination write-only slice. It deliberately mirrors std's <[_]>::copy_from_slice so misuse fails fast instead of causing a partial or out-of-bounds write into mapped GPU buffer memory.","triggerScenarios":"Calling buffer_slice.copy_from_slice(&data) where data.len() != the slice's element count — e.g. writing N elements into a slice created with a different size, or forgetting that get_by_mut/slice ranges are element-count based.","commonSituations":"Uploading uniform/vertex data after resizing the CPU-side Vec (e.g. pushed extra elements, or a struct layout changed), or slicing a buffer by bytes and copying a Vec of elements with mismatched count.","solutions":["Make the source slice exactly as long as the destination: slice &data[..expected_len] before copying.","Derive the buffer size from data.len() when creating/sizing the buffer or the slice range.","Use chunks to copy data larger than the destination instead of a single copy_from_slice call."],"exampleFix":"// before\nlet data: Vec<f32> = vec![0.0; 12];\nwrite_slice.copy_from_slice(&data); // slice holds 16 elements\n// after\nassert_eq!(data.len(), write_slice.len());\nwrite_slice.copy_from_slice(&data[..write_slice.len()]);","handlingStrategy":"validation","validationCode":"if src.len() != dst.len() {\n    // trim or error out before copying\n    return Err(\"slice length mismatch\".into());\n}\ndst.copy_from_slice(src);","typeGuard":null,"tryCatchPattern":"let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| dst.copy_from_slice(src)));","preventionTips":["Assert src.len() == dst.len() at the call site during development.","Derive buffer and slice sizes from the data being uploaded, not from hardcoded constants.","Add unit tests that map a buffer and copy data of the exact expected length."],"tags":["panic","rust","buffer-mapping","length-mismatch"],"backgroundTag":"slice-length-mismatch","analyzedSha":"3e11ff59bf3f9795d285ecc045014089640d7248","analyzedAt":"2026-09-03T01:43:21.459Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T07:17:11.731Z"}