#enums #no-std #map #static-map #derive #linearize

no-std linearize

Types that are enumerable and an array-backed map

5 releases

0.1.4 Feb 12, 2025
0.1.3 Jan 12, 2025
0.1.2 Jan 6, 2025
0.1.1 Nov 21, 2024
0.1.0 Nov 21, 2024

#227 in Data structures

Download history 17/week @ 2024-12-19 25/week @ 2024-12-26 122/week @ 2025-01-02 265/week @ 2025-01-09 719/week @ 2025-01-16 372/week @ 2025-01-23 145/week @ 2025-01-30 157/week @ 2025-02-06 816/week @ 2025-02-13 937/week @ 2025-02-20 999/week @ 2025-02-27 713/week @ 2025-03-06 480/week @ 2025-03-13 437/week @ 2025-03-20 741/week @ 2025-03-27 735/week @ 2025-04-03

2,487 downloads per month
Used in 3 crates (2 directly)

MIT/Apache

125KB
3K SLoC

linearize

crates.io docs.rs MSRV

This crate provides a trait that defines an enumeration of a type and an efficient no_std map that uses such types as keys.

Example

use linearize::{Linearize, static_map};

#[derive(Linearize)]
enum Keys {
    A,
    B(bool),
}

fn main() {
    let map = static_map! {
        Keys::A => "a",
        Keys::B(false) => "b",
        Keys::B(true) => "c",
    };
    assert_eq!(map[Keys::A], "a");
    assert_eq!(map[Keys::B(true)], "c");
}

MSRV

The MSRV is max(1.83, stable - 3).

License

This project is licensed under either of

  • Apache License, Version 2.0
  • MIT License

at your option.


lib.rs:

A crate for enumerable types.

This crate provides the [Linearize] trait which defines a bijection between a type and an interval of the natural numbers.

Given such a bijection, many useful things become possible. For example, this crate defines the types [StaticMap] and [StaticCopyMap] which provide high-performance, non-allocating mappings from linearizable types to arbitrary values.

#
#[derive(Linearize)]
enum ColorFormat {
    R,
    Rgb {
        alpha: bool,
    },
}

let mut channels = StaticMap::default();
channels[ColorFormat::R] = 1;
channels[ColorFormat::Rgb { alpha: false }] = 3;
channels[ColorFormat::Rgb { alpha: true }] = 4;

assert_eq!(channels[ColorFormat::Rgb { alpha: false }], 3);

These maps can be constructed conveniently with the [static_map] macro:

#
#
let channels = static_map! {
    ColorFormat::R => 1,
    ColorFormat::Rgb { alpha } => 3 + alpha as u32,
};

assert_eq!(channels[ColorFormat::Rgb { alpha: false }], 3);

Features

The following features are enabled by default:

  • std

This crate provides the following features:

  • alloc: Adds a dependency on the alloc crate. This implements additional traits for the map types.
  • std: Adds a dependency on the std crate.
  • derive: Provides the Linearize derive macro.
  • serde-1: Implements Serialize and Deserialize from serde 1.x for the map types.
  • arbitrary-1: Implements Arbitrary from arbitrary 1.x for the map types.
  • bytemuck-1: Implements NoUninit, Zeroable, and AnyBitPattern from bytemuck 1.x for the map types.
  • rand-0_8: Implements various distributions from rand 0.8.x for the map types.
  • rand-0_9: Implements various distributions from rand 0.9.x for the map types. All iterators exposed by this crate.

This module exists only to keep the top-level namespace clean.

Dependencies

~1–425KB