Crate multiconst

Source
Expand description

For destructuring an expression into multiple constants.

The primary feature of this crate is the multiconst macro, which destructuring an expression into multiple constants.

§Example

For more examples you can look in the docs for multiconst

§Basic

This example demonstrates destructuring an array (whose length is inferred) into multiple constants.

use multiconst::multiconst;

assert_eq!(A, 0b11);
assert_eq!(B, 0b111);
assert_eq!(C, 0b1111);
assert_eq!(D, 0b11111);

multiconst!{
    pub const [A, B, C, D]: [u64; _] = mersennes_from(2);
}

/// Generates all mersenne numbers (binary numbers that are all `1` bits)
/// from `start` amount of 1s up to `start + N - 1`.
const fn mersennes_from<const N: usize>(start: u32) -> [u64; N] {
    let mut out = [0; N];
    multiconst::for_range!{i in 0..N =>
        out[i] = (1 << (i as u32 + start)) - 1;
    }
    out
}

§Struct

This example demonstrates how structs that impl FieldType can be destructured.

This example uses the FieldType derive macro (which requires the “derive” feature) to make it possible to destructure struct fields without annotating their types,.

use multiconst::{FieldType, multiconst};

assert_eq!(MIN, 3);
assert_eq!(MAX, 21);


multiconst!{
    const MinMax{min: MIN, max: MAX}: MinMax = min_max(&[21, 13, 3, 8, 5]);
}

#[derive(FieldType)]
struct MinMax {
    min: u32,
    max: u32,
}

const fn min_max(elems: &[u32]) -> MinMax {
    let mut min = u32::MAX;
    let mut max = 0;
     
    multiconst::for_range!{i in 0..elems.len() =>
        let elem = elems[i];
         
        if elem < min { min = elem; }
        if elem > max { max = elem; }
    }
     
    MinMax{min, max}
}

§Features

All these crate features are opt-in:

  • "derive": enables the FieldType derive macro.

§No-std support

multiconst is #![no_std], it can be used anywhere Rust can be used.

§Minimum Supported Rust Version

multiconst requires Rust 1.51.0, requiring crate features to use newer language features.

Macros§

associated_multiconst
Destructures a constant expression into multiple associated constants.
field_name
Macro that expands to a type-level representation of a field name.
field_name_aliases
Declares type aliases for type-level representations of field names.
for_range
For loop over a range
multiconst
Destructures a constant expression into multiple constants

Structs§

TChars
Type-level representation of up to 8 characters, with spaces padding the const arguments after the last character.
TIdent
type-level representation of an identifier,
Usize
A type-level usize, used to query the type of positional fields (tuple field s).

Traits§

FieldType
For querying the type of a field in Self.

Type Aliases§

GetFieldType
Gets the type of a (potentially nested) field.

Derive Macros§

FieldTypederive
Derives the FieldType trait for a struct.