{"id":16064411,"url":"https://github.com/ohsayan/constant","last_synced_at":"2025-06-18T18:05:04.368Z","repository":{"id":62438865,"uuid":"411955424","full_name":"ohsayan/constant","owner":"ohsayan","description":"Constant, compile-time evaluation tools for Rust","archived":false,"fork":false,"pushed_at":"2021-10-01T05:25:59.000Z","size":30,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"next","last_synced_at":"2025-06-18T18:04:51.693Z","etag":null,"topics":["const-evaluation","crates-io","macro","rust","rust-lang","rust-library"],"latest_commit_sha":null,"homepage":"https://docs.rs/constant","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ohsayan.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-09-30T06:53:15.000Z","updated_at":"2021-10-01T05:24:56.000Z","dependencies_parsed_at":"2022-11-01T21:51:23.642Z","dependency_job_id":null,"html_url":"https://github.com/ohsayan/constant","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/ohsayan/constant","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ohsayan%2Fconstant","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ohsayan%2Fconstant/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ohsayan%2Fconstant/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ohsayan%2Fconstant/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ohsayan","download_url":"https://codeload.github.com/ohsayan/constant/tar.gz/refs/heads/next","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ohsayan%2Fconstant/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260606456,"owners_count":23035349,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["const-evaluation","crates-io","macro","rust","rust-lang","rust-library"],"created_at":"2024-10-09T05:07:19.455Z","updated_at":"2025-06-18T18:04:59.359Z","avatar_url":"https://github.com/ohsayan.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `constant`: Constant, compile-time evaluation tools for Rust 🦀\n\n![Rust Stable](https://img.shields.io/badge/rust-stable-50C878?style=for-the-badge) ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/ohsayan/constant/Test?style=for-the-badge) [![Crates.io](https://img.shields.io/crates/v/constant?style=for-the-badge)](https://crates.io/crates/constant) [![docs.rs](https://img.shields.io/docsrs/constant?style=for-the-badge)](https://docs.rs/constant) [![GitHub](https://img.shields.io/github/license/ohsayan/constant?style=for-the-badge)](./LICENSE)\n\nThe `constant` crate aims to provide tools for safely _working around_ the limits imposed by constant evaluation in Rust.\n\n## Features\n\n- Create [constant default implementations](#constant-default-implementations)\n- Created [nested constant default implementations](#nested-constant-default-implementations)\n- Full support for generics, lifetimes and generics in `Constdef` impls\n- 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)\n- More coming soon!\n\n## Constant default implementations\n\n```rust\n#[derive(constant::Constdef)]\npub struct SpookyFriend {\n    name: String,\n    email: String,\n    friend_names: Vec\u003cString\u003e,\n    userid: u64,\n}\n\nconst SPOOKY: SpookyFriend = SpookyFriend::default();\n\n#[test]\nfn test_struct_with_heap_fields() {\n    // spooky name; it's empty!\n    assert_eq!(SPOOKY.name, \"\");\n    // spooky email; it's empty!\n    assert_eq!(SPOOKY.email, \"\");\n    // spooky friend has no friends!\n    assert_eq!(SPOOKY.friend_names, Vec::\u003cString\u003e::new());\n    // spooky userid; it's 0!\n    assert_eq!(SPOOKY.userid, 0);\n}\n```\n\n## Nested constant default implementations\n\n```rust\nuse constant::Constdef;\n\n#[derive(Constdef)]\npub struct SystemLoad {\n    disk: DiskLoad,\n    net: NetLoad,\n}\n\n#[derive(Constdef)]\npub struct DiskLoad {\n    disk_count: usize,\n    media_list: Vec\u003cString\u003e,\n}\n\n#[derive(Constdef)]\npub struct NetLoad {\n    interface_list: Vec\u003cString\u003e,\n    portload: [usize; 65536],\n}\n\nstatic mut SYSLOAD: SystemLoad = SystemLoad::default();\n\n#[test]\nfn test_system_load_nested_struct() {\n    unsafe {\n        // check the number of disks\n        assert_eq!(SYSLOAD.disk.disk_count, 0);\n        // increment the number of disks\n        SYSLOAD.disk.disk_count += 1;\n        assert_eq!(SYSLOAD.disk.disk_count, 1);\n        // check port 80 load\n        assert_eq!(SYSLOAD.net.portload[80], 0);\n        // increment the load\n        SYSLOAD.net.portload[80] += 1;\n        assert_eq!(SYSLOAD.net.portload[80], 1);\n\n        // now let's add a disk\n        SYSLOAD.disk.media_list.push(\"/dev/sda1\".to_string());\n        // now let's add a network interface\n        SYSLOAD.net.interface_list.push(\"eth01\".to_string());\n    }\n}\n```\n\n## License\n\nThis library is distributed under the [Apache-2.0 License](./LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fohsayan%2Fconstant","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fohsayan%2Fconstant","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fohsayan%2Fconstant/lists"}