| Crates.io | const-tools |
| lib.rs | const-tools |
| version | 0.1.1 |
| created_at | 2025-10-27 22:29:30.30341+00 |
| updated_at | 2025-10-28 00:05:15.076831+00 |
| description | Utilities to help write safe const fns such as destructuring and array operations. |
| homepage | |
| repository | https://github.com/mickvangelderen/const-map-array |
| max_upload_size | |
| id | 1903820 |
| size | 66,515 |
const-toolsThis crate provides macros that help write const functions.
Please view the documentation at docs.rs/const-tools.
Stable rust (as of Oct 2025) does not allow destructuring of tuples, structs and arrays in const contexts:
const fn into_inner<T>(wrap: (T,)) -> T {
let (value,) = wrap;
value
}
gives:
error[E0493]: destructor of `Wrap<T>` cannot be evaluated at compile-time
Additionally, moving values out of arrays and building new ones is tricky:
const fn wrap_all<T, const N: usize>(value: [T; N]) -> [(T,); N] {
// Create uninitialized output array
let mut oa: [std::mem::MaybeUninit<(T,)>; N] =
[const { std::mem::MaybeUninit::uninit() }; N];
// Wrap input to prevent drop
let ia = std::mem::ManuallyDrop::new(value);
// Get reference to inner value (Deref not available in const)
let ia: &[T; N] = unsafe { std::mem::transmute(&ia) };
let mut index = 0;
while index < N {
// Read a single item from the input array
let item = unsafe { std::ptr::read(&ia[index]) };
// Initialize the element in the output array
oa[index].write((item,));
index += 1;
}
// All elements have been initialized
unsafe { std::mem::transmute_copy(&oa) }
}
This library provides macros to make all of this safe and easy.
use const_tools::{destructure, map};
const fn into_inner<T>(value: (T,)) -> T {
destructure!(let (inner,) = value);
inner
}
const fn wrap_all<T, const N: usize>(value: [T; N]) -> [(T,); N] {
map!(value, |item| (item,))
}