Create and merge any persistent data at compile time. A nightly compiler version is required (this is not a compiler plugin).
- Combining any persistent arrays at compile time
- Combining any static strings at compile time
- Ability to combine generic constant data (but only with known types (with unknown types Rust cannot track generic relationships)).
- The library uses #! [no _ std]
- Easy
#[macro_use]
extern crate cluConstData;
const_data! {
const A: &'static [u8] = b"[";
const B: &'static [u8] = b"].";
pub (crate) const ARRAY: &'static [u8] = A, b"User", B, b" ";
}
fn main() {
assert_eq!(A, b"[");
assert_eq!(B, b"].");
println!("#1 {}", std::str::from_utf8(ARRAY).unwrap());
assert_eq!(ARRAY, b"[User]. ");
}
- EasyStr
#[macro_use]
extern crate cluConstData;
const_data! {
const A: &'static str = "[";
const B: &'static str = "]";
pub (crate) const RESULT: &'static str = A, "DATA", B;
}
fn main() {
assert_eq!(A, "[");
assert_eq!(B, "]");
println!("#1 {}", RESULT);
assert_eq!(RESULT, "[DATA]");
}
- EasyArray
#[macro_use]
extern crate cluConstData;
const_data! {
const U32_HEAD: u32 = 255;
const U32_END: u32 = 0;
const U32_ARRAY: [u32; 3] = &[U32_HEAD], &[2], &[U32_END];
const U32_SARRAY: &'static [u32] = &[U32_HEAD, 2, 3 ,4], &[2, 3], &[U32_END];
}
fn main() {
println!("#1 {:?}", U32_HEAD);
assert_eq!(U32_HEAD, 255);
println!("#2 {:?}", U32_END);
assert_eq!(U32_END, 0);
//result
println!("#3 {:?}", U32_ARRAY);
assert_eq!(U32_ARRAY, [255, 2, 0]);
println!("#4 {:?}", U32_SARRAY);
assert_eq!(U32_SARRAY, [255, 2, 3, 4, 2, 3, 0]);
}
- DynGeneric
#[macro_use]
extern crate cluConstData;
use std::marker::PhantomData;
pub trait TypeTrait {
const TYPE: &'static str;
#[inline]
fn as_type_str() -> &'static str {
Self::TYPE
}
}
impl TypeTrait for usize {
const TYPE: &'static str = "usize";
}
impl TypeTrait for (usize, usize) {
const_data! {
const TYPE: &'static str = usize::TYPE, " + ", usize::TYPE;
}
}
impl TypeTrait for (PhantomData<()>, usize) {
const_data! {
const TYPE: &'static str = "PhantomData<()>", " + ", usize::TYPE;
}
}
fn main() {
println!("#1 {:?}", usize::as_type_str());
assert_eq!(usize::as_type_str(), "usize");
println!("#2 {:?}", <(usize, usize)>::as_type_str());
assert_eq!(<(usize, usize)>::as_type_str(), "usize + usize");
}
Copyright 2022 #UlinProject Denis Kotlyarov (Денис Котляров)
Licensed under the Apache License, Version 2.0