1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//! 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`][multiconst-examples]
//!
//! ### Basic
//!
//! This example demonstrates destructuring an array (whose length is inferred)
//! into multiple constants.
//!
//! ```rust
//! 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`][FieldType-trait]
//! can be destructured.
//!
//! This example uses the [`FieldType`][FieldType-derive]
//! derive macro (which requires the "derive" feature)
//! to make it possible to destructure struct fields without
//! [annotating their types][example-struct-ty-annot],.
//!
//! 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`][FieldType-derive] 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.
//!
//!
//! [`multiconst`]: crate::multiconst
//! [FieldType-trait]: trait@crate::FieldType
//! [FieldType-derive]: derive@crate::FieldType
//! [multiconst-examples]: crate::multiconst#examples
//! [example-struct-ty-annot]: crate::multiconst#example-struct-ty-annot
pub use crate*;
include!