Crates.io | cluConstData |
lib.rs | cluConstData |
version | 1.3.0 |
source | src |
created_at | 2019-05-06 17:31:21.251616 |
updated_at | 2019-10-17 16:59:20.44153 |
description | Create and merge any persistent data at compile time. |
homepage | |
repository | https://github.com/clucompany/cluConstData.git |
max_upload_size | |
id | 132419 |
size | 35,332 |
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]
#[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]. ");
}
#[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]");
}
#[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]);
}
#[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 2019 #UlinProject Denis Kotlyarov (Денис Котляров)
Licensed under the Apache License, Version 2.0