gfx-rs/wgpu · error
mid > len
Error message
mid > len
What it means
WriteOnly<[T]>::split_at panics when mid is greater than the slice's length. It is the infallible variant of split_at_checked and is also reachable indirectly through into_chunks, which calls split_at at each chunk boundary.
Source
Thrown at wgpu-types/src/write_only.rs:481
(array_slice, remainder)
}
/// Divides one write-only slice reference into two at an index.
///
/// The first will contain all indices from `[0, mid)` (excluding
/// the index `mid` itself) and the second will contain all
/// indices from `[mid, len)` (excluding the index `len` itself).
///
/// # Panics
///
/// Panics if `mid > len`.
#[inline]
#[must_use]
#[track_caller]
pub fn split_at(self, mid: usize) -> (WriteOnly<'a, [T]>, WriteOnly<'a, [T]>) {
match self.split_at_checked(mid) {
Ok(slices) => slices,
Err(_) => panic!("mid > len"),
}
}
/// Divides one write-only slice reference into two at an index, returning [`Err`] if the
/// slice is too short.
///
/// If `mid ≤ len`, returns a pair of slices where the first will contain all
/// indices from `[0, mid)` (excluding the index `mid` itself) and the
/// second will contain all indices from `[mid, len)` (excluding the index
/// `len` itself).
///
/// Otherwise, if `mid > len`, returns [`Err`] with the original slice.
#[inline]
pub const fn split_at_checked(self, mid: usize) -> Result<(Self, Self), Self> {
if mid <= self.len() {
let Self { ptr, _phantom: _ } = self;
let element_ptr = ptr.cast::<T>();
Ok(unsafe {View on GitHub (pinned to 3e11ff59bf)
Solutions
- Ensure mid <= slice.len(): clamp with mid.min(slice.len()) or check first.
- Use split_at_checked and handle the Err case instead of split_at.
- For into_chunks, size the buffer to be an exact multiple of the chunk size, or use chunks_exact-style logic for the tail.
Example fix
// before
let (a, b) = slice.split_at(chunk_size); // chunk_size may exceed len
// after
let (a, b) = match slice.split_at_checked(chunk_size) {
Ok(pair) => pair,
Err(_) => (slice, /* empty tail */ slice.split_at(slice.len()).1),
}; Defensive patterns
Strategy: validation
Validate before calling
if mid > slice.len() {
return Err(format!("split_at mid {mid} exceeds len {}", slice.len()));
}
let (a, b) = slice.split_at(mid); Try / catch
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| slice.split_at(mid)));
Prevention
- Prefer split_at_checked and handle Err explicitly.
- When using into_chunks, ensure the buffer length is a multiple of the chunk size or handle the remainder chunk.
- Clamp mid with slice.len() before splitting.
When it happens
Trigger: Calling slice.split_at(mid) with mid > slice.len(), or calling into_chunks with a chunk size that does not divide the slice length so the final split_at goes past the end.
Common situations: Dividing a mapped buffer slice into fixed-size chunks (e.g. uniforms of 256 bytes) without handling the remainder, or computing mid from a wrong constant/stride.
Related errors
- source slice length ({src_len}) does not match destination s
- split_off() requires a one-sided range
- slice offset {slice_offset} is out of range for buffer of si
- slice offset {slice_offset} size {slice_size} is out of rang
- No wgpu backend feature that is implemented for the target p
AI-assisted analysis of gfx-rs/wgpu@3e11ff59bf (2026-09-03).
Data as JSON: /api/errors/63dd9ca74b59058f.
Report an issue: GitHub.