← LeaderboardThe Skill Leaderboard

rust-ndarray-row-major-output

Preserve row-major output from Rust ndarray views. Verify shape, strides, and element order after transposing or slicing a matrix.

Published by sickn33 ↗sickn33/agentic-awesome-skills

Try your first task

A source-grounded starter task has not been verified for this skill yet.

SKILL.md

name: rust-ndarray-row-major-outputdescription: Preserve row-major output from Rust ndarray views. Verify shape, strides, and element order after transposing or slicing a matrix.category: developmentrisk: criticalsource: selfsource_type: selfdate_added: "2026-12-03"author: ShianMiketags: [rust, ndarray, array-layout, row-major, strides]

Rust ndarray row-major output

When to Use

  • A Rust exporter reports the correct transposed dimensions and writes the original flat element order.
  • An ndarray path relies on as_slice_memory_order for a consumer that reads contiguous row-major values.
  • A crop, transpose, or stepped slice changes the relationship between logical coordinates and physical offsets.

Prerequisites

  • Locate the view transformation and the consumer's contract for shape, axes, and element order.
  • Record the resolved ndarray version. The example uses 0.17.2 and a two-dimensional unsigned-integer array.
  • Use a rectangular input with distinct values. A square or symmetric fixture can hide an incorrect permutation.

1. Inspect shape and strides before flattening

  1. Record the view's shape, strides, and relevant coordinate values. A transpose can swap axes without moving the underlying elements.
  2. Check whether the consumer needs row-major logical order or can use explicit strides. Include its requirement for contiguous data.
  3. as_slice() requires standard layout. as_slice_memory_order() can return a slice from a contiguous nonstandard view. Its order follows storage.
  4. Check a stepped slice as well as a transpose. Some views have gaps and cannot provide a single memory-order slice.

2. Normalize only at the boundary that needs it

  • Use as_standard_layout() before passing a contiguous row-major slice to a sink. It can borrow a standard layout and materialize a copy when it needs to reorder data.
  • Pass the logical shape beside the normalized values. Verify the resulting payload against known row and column positions.
  • If the consumer can accept elements incrementally, consider iter() and write in logical order without requiring an intermediate contiguous slice.
  • Preserve strides for a consumer that explicitly accepts them. Account for the source view's lifetime and any layout requirements of that interface.

3. Verify the consumer's sequence

  1. Check a standard array first. Assert both shape and exact values to preserve the original path.
  2. Transpose the same rectangle. Compare the physical slice and the sequence a row-major consumer must receive.
  3. Include a noncontiguous stepped view. Verify that normalization retains its shape and yields only selected values in the proper order.
  4. Exercise the actual encoder, FFI boundary, or data writer if it also handles byte order, row pitch, or ownership.

Example

Serialize the logical matrix reproduces the wrong values for a contiguous transpose and verifies the correct row-major output for standard, transposed, and stepped views.

Limitations

  • The fixture uses an in-process consumer of a shape and integer slice. It does not test a file format, physical byte endianness, BLAS, or FFI.
  • Normalization can allocate. Benchmark the real sink and inspect scratch memory if the path is large or frequent.
  • Shape and axis semantics depend on the caller. This workflow preserves the order of the supplied logical view.

Safety and Side Effects

The example uses a small in-memory matrix. Before changing a serialized layout, verify the consumer's axis and order contract with a non-symmetric fixture.

Related Skills and Documentation