Crate rustifact_extra

Source
Expand description

§Rustifact extras

In future, this crate may provide other extensions to Rustifact, but for now, it serves to provide jagged array support.

§Motivation

Definition: A jagged array is an array with rows of uneven lengths.

Suppose we have a collections of arrays [T; N1], [T; N2]…, [T; Nn] that we wish to precalculate at compile time. We can store the elements in a Vec<Vec<T>>, or a [Vec<T>; n], but these are unsuitable for static memory.

The types JaggedArray and BareJaggedArray allow us to efficiently store jagged arrays in static memory by pre-populating them in a buildscript with Rustifact.

§Types

  • JaggedArray provides indexing capability at compile time and runtime. Use this type if you’re unsure of your requirements.
  • BareJaggedArray provides indexing capability at compile time. Indexes are injected into runtime as token streams.

§A simple example

build.rs

use rustifact::ToTokenStream;
use rustifact_extra::JaggedArrayBuilder;

fn main() {
    let mut num_array = JaggedArrayBuilder::new();
    num_array.push(vec![1, 2, 3]);
    num_array.push(vec![4]);
    num_array.push(vec![5, 6]);
    rustifact::write_const!(NUM_ARRAY_LEN, usize, num_array.len());
    rustifact::write_const!(NUM_ARRAY_ELEMS_LEN, usize, num_array.elems_len());
    rustifact::write_static!(NUM_ARRAY, JaggedArray<i32, NUM_ARRAY_LEN, NUM_ARRAY_ELEMS_LEN>, &num_array);
}

src/main.rs

rustifact::use_symbols!(NUM_ARRAY, NUM_ARRAY_LEN, NUM_ARRAY_ELEMS_LEN);
use rustifact_extra::JaggedArray;

fn main() {
   assert_eq!(NUM_ARRAY[0], [1, 2, 3]);
   assert_eq!(NUM_ARRAY[1], [4]);
   assert_eq!(NUM_ARRAY[2], [5, 6]);
}

Cargo.toml

[package]
# ...

[build-dependencies]
rustifact = "0.9"
rustifact_extra = "0.1"

[dependencies]
rustifact = "0.9"
rustifact_extra = "0.1"

Structs§

BareJaggedArray
BareJaggedArrayBuilder
BareJaggedArrayIndex
JaggedArray
JaggedArrayBuilder
JaggedArrayIndex