https://github.com/ohsayan/constant
Constant, compile-time evaluation tools for Rust
https://github.com/ohsayan/constant
const-evaluation crates-io macro rust rust-lang rust-library
Last synced: 4 months ago
JSON representation
Constant, compile-time evaluation tools for Rust
- Host: GitHub
- URL: https://github.com/ohsayan/constant
- Owner: ohsayan
- License: apache-2.0
- Created: 2021-09-30T06:53:15.000Z (about 4 years ago)
- Default Branch: next
- Last Pushed: 2021-10-01T05:25:59.000Z (about 4 years ago)
- Last Synced: 2025-06-18T18:04:51.693Z (4 months ago)
- Topics: const-evaluation, crates-io, macro, rust, rust-lang, rust-library
- Language: Rust
- Homepage: https://docs.rs/constant
- Size: 29.3 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# `constant`: Constant, compile-time evaluation tools for Rust 🦀
  [](https://crates.io/crates/constant) [](https://docs.rs/constant) [](./LICENSE)
The `constant` crate aims to provide tools for safely _working around_ the limits imposed by constant evaluation in Rust.
## Features
- Create [constant default implementations](#constant-default-implementations)
- Created [nested constant default implementations](#nested-constant-default-implementations)
- Full support for generics, lifetimes and generics in `Constdef` impls
- Almost all types provided by the `std` are supported by this crate. If you need any other external type, [just open an issue and ask!](https://github.com/ohsayan/constant/issues/new)
- More coming soon!## Constant default implementations
```rust
#[derive(constant::Constdef)]
pub struct SpookyFriend {
name: String,
email: String,
friend_names: Vec,
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::::new());
// spooky userid; it's 0!
assert_eq!(SPOOKY.userid, 0);
}
```## Nested constant default implementations
```rust
use constant::Constdef;#[derive(Constdef)]
pub struct SystemLoad {
disk: DiskLoad,
net: NetLoad,
}#[derive(Constdef)]
pub struct DiskLoad {
disk_count: usize,
media_list: Vec,
}#[derive(Constdef)]
pub struct NetLoad {
interface_list: Vec,
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());
}
}
```## License
This library is distributed under the [Apache-2.0 License](./LICENSE).