Crate const_struct

Source
Expand description

§const_struct

This crate provides a way to create a struct with const generics and const values.

use const_struct::{primitive::F32Ty, F32};
pub fn tester<A: F32Ty>() {
    println!("a: {:?}", A::__DATA);
}
fn main() {
    tester::<F32!(0.5)>();
}

This crate is no_std. used unsafe code:

  • core::mem::zeroed
  • core::mem::transmute

example code

use const_struct::{call_with_generics, const_struct, ConstStruct};
#[derive(ConstStruct, Debug)]
pub struct TestGenerics<const T: usize> {
    float: f32,
}
const COUNT: usize = 7;
#[const_struct]
const B: TestGenerics<{ COUNT }> = TestGenerics { float: 0.0 };
const fn constant<const T: usize, S: TestGenericsTy<T>>() -> f32 {
    S::FLOAT
}

const FLOAT: f32 = call_with_generics!(constant::<test_generics!(BTy)>());

If you see other complex example code, you see https://github.com/oligamiq/const_struct/tree/main/crates/test_code

See the Github README for more information.

Re-exports§

pub use primitive::PrimitiveTraits;

Modules§

keeptype
prelude
primitive
struct_prim
util_macro

Macros§

Bool
Char
F32
F64
I8
I16
I32
I64
I128
Isize
None
Some
U8
U16
U32
U64
U128
Usize
call_with_generics
init
match_end_with
match_underscore
parse_value

Attribute Macros§

const_compat
const_struct
This macro generates a const struct.

Derive Macros§

ConstStruct
This derive generates a const struct implementation from struct definitions.