array-reshape 0.1.0

Reshape arrays by value, by reference and by mutable reference in const fn
Documentation
  • Coverage
  • 100%
    22 out of 22 items documented1 out of 22 items with examples
  • Size
  • Source code size: 22.2 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 206.4 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 1s Average build duration of successful builds.
  • all releases: 1s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • nazar-pc/array-reshape
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • nazar-pc

array-reshape

Crates.io Docs

Reshaping of arrays, by value, by reference and by mutable reference, all in const fn.

<[[T; N]]>::as_flattened() and <[T]>::as_chunks() in the standard library convert to and from slices, losing the length in the process. This crate converts between arrays of the same total length instead, for example [[T; N]; M] into [T; N * M]:

use array_reshape::{Flatten, Rechunk, Unflatten};

let pairs = [[1, 2], [3, 4], [5, 6]];

let flat: &[u8; 6] = pairs.flatten_ref();
assert_eq!(flat, &[1, 2, 3, 4, 5, 6]);

let triples = pairs.rechunk_ref::<3, 2>();
assert_eq!(triples, &[[1, 2, 3], [4, 5, 6]]);

let pairs_again = flat.unflatten::<2, 3>();
assert_eq!(pairs_again, pairs);

Every conversion is also available as a free const fn (like array_reshape::flatten_ref()) that works in const context on stable Rust. With const-trait feature, which requires nightly Rust, Flatten, Rechunk and Unflatten become const traits, so their methods can be called in const fn directly.

Stable Rust doesn't allow computing array lengths from generic constants yet, so the length of the output is a separate generic parameter, which is usually inferred from the context. The lengths are checked at compile time, but since the check happens after monomorphization, it is only reported by cargo build, not cargo check.

The crate is no_std and works on stable Rust 1.85 or newer.