Crates.io | constant |
lib.rs | constant |
version | 0.2.0 |
source | src |
created_at | 2021-09-30 02:14:55.03342 |
updated_at | 2021-10-01 05:10:29.339913 |
description | Constant evaluation tools for Rust |
homepage | |
repository | https://github.com/ohsayan/constant |
max_upload_size | |
id | 458471 |
size | 11,367 |
constant
: Constant, compile-time evaluation tools for Rust 🦀The constant
crate aims to provide tools for safely working around the limits imposed by constant evaluation in Rust.
Constdef
implsstd
are supported by this crate. If you need any other external type, just open an issue and ask!#[derive(constant::Constdef)]
pub struct SpookyFriend {
name: String,
email: String,
friend_names: Vec<String>,
userid: u64,
}
const SPOOKY: SpookyFriend = SpookyFriend::default();
#[test]
fn test_struct_with_heap_fields() {
// spooky name; it's empty!
assert_eq!(SPOOKY.name, "");
// spooky email; it's empty!
assert_eq!(SPOOKY.email, "");
// spooky friend has no friends!
assert_eq!(SPOOKY.friend_names, Vec::<String>::new());
// spooky userid; it's 0!
assert_eq!(SPOOKY.userid, 0);
}
use constant::Constdef;
#[derive(Constdef)]
pub struct SystemLoad {
disk: DiskLoad,
net: NetLoad,
}
#[derive(Constdef)]
pub struct DiskLoad {
disk_count: usize,
media_list: Vec<String>,
}
#[derive(Constdef)]
pub struct NetLoad {
interface_list: Vec<String>,
portload: [usize; 65536],
}
static mut SYSLOAD: SystemLoad = SystemLoad::default();
#[test]
fn test_system_load_nested_struct() {
unsafe {
// check the number of disks
assert_eq!(SYSLOAD.disk.disk_count, 0);
// increment the number of disks
SYSLOAD.disk.disk_count += 1;
assert_eq!(SYSLOAD.disk.disk_count, 1);
// check port 80 load
assert_eq!(SYSLOAD.net.portload[80], 0);
// increment the load
SYSLOAD.net.portload[80] += 1;
assert_eq!(SYSLOAD.net.portload[80], 1);
// now let's add a disk
SYSLOAD.disk.media_list.push("/dev/sda1".to_string());
// now let's add a network interface
SYSLOAD.net.interface_list.push("eth01".to_string());
}
}
This library is distributed under the Apache-2.0 License.