{"id":25663754,"url":"https://github.com/maxxsoft/build_assert","last_synced_at":"2025-08-23T22:05:19.903Z","repository":{"id":208372539,"uuid":"719926735","full_name":"MaxXSoft/build_assert","owner":"MaxXSoft","description":"Asserts const generic expressions at build-time.","archived":false,"fork":false,"pushed_at":"2023-11-22T09:17:38.000Z","size":45,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T14:51:19.093Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/MaxXSoft.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-17T07:44:33.000Z","updated_at":"2023-11-23T00:47:19.000Z","dependencies_parsed_at":"2025-02-24T05:19:03.127Z","dependency_job_id":"86dade80-a950-4e3a-848e-978b6b73057e","html_url":"https://github.com/MaxXSoft/build_assert","commit_stats":null,"previous_names":["maxxsoft/build_assert"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaxXSoft%2Fbuild_assert","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaxXSoft%2Fbuild_assert/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaxXSoft%2Fbuild_assert/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaxXSoft%2Fbuild_assert/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MaxXSoft","download_url":"https://codeload.github.com/MaxXSoft/build_assert/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250233760,"owners_count":21396864,"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":[],"created_at":"2025-02-24T05:18:56.456Z","updated_at":"2025-04-22T11:36:30.608Z","avatar_url":"https://github.com/MaxXSoft.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# build_assert\n\n[\u003cimg alt=\"github\" src=\"https://img.shields.io/badge/github-MaxXSoft/build__assert-8da0cb?style=for-the-badge\u0026labelColor=555555\u0026logo=github\" height=\"20\"\u003e](https://github.com/MaxXSoft/build_assert)\n[\u003cimg alt=\"crates.io\" src=\"https://img.shields.io/crates/v/build_assert.svg?style=for-the-badge\u0026color=fc8d62\u0026logo=rust\" height=\"20\"\u003e](https://crates.io/crates/build_assert)\n[\u003cimg alt=\"docs.rs\" src=\"https://img.shields.io/badge/docs.rs-build__assert-66c2a5?style=for-the-badge\u0026labelColor=555555\u0026logoColor=white\u0026logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K\" height=\"20\"\u003e](https://docs.rs/build_assert)\n[\u003cimg alt=\"build status\" src=\"https://img.shields.io/github/actions/workflow/status/MaxXSoft/build_assert/build-test.yml?branch=master\u0026style=for-the-badge\" height=\"20\"\u003e](https://github.com/MaxXSoft/build_assert/actions?query=branch%3Amaster)\n\n`build_assert` allows you to make assertions at build-time.\n\nUnlike `assert` and some implementations of compile-time assertions, such as [`static_assertions`](https://docs.rs/static_assertions), `build_assert` works before runtime, and can be used for expressions containing const generics.\n\n## Usage\n\nAdd `build_assert` to your project by running `cargo add`:\n\n```\ncargo add build_assert\n```\n\n## Examples\n\n```rust\nfn foo\u003cconst N: usize\u003e() {\n  build_assert!(N \u003e 5);\n}\n\nfoo::\u003c10\u003e(); // Fine.\nfoo::\u003c0\u003e();  // Fails to compile.\n```\n\nThe above example will **fail to build in release mode**. Due to the internal implementation, it will **pass the build and panic at runtime** in debug mode.\n\nAs a comparison, `assert` will only panic at runtime, and static assertion implementations can not be applied to const generics:\n\n```rust\nmacro_rules! static_assert {\n  ($e:expr) =\u003e {\n    const _: () = core::assert!($e);\n  };\n}\n\nfn foo\u003cconst N: usize\u003e() {\n  static_assert!(N \u003e 5);\n}\n```\n\nAn error occurs when compiling the above example:\n\n```\nerror[E0401]: can't use generic parameters from outer item\n  --\u003e src/lib.rs:36:18\n   |\n9  | fn foo\u003cconst N: usize\u003e() {\n   |              - const parameter from outer item\n10 |   static_assert!(N \u003e 5);\n   |                  ^ use of generic parameter from outer item\n```\n\n## Features\n\nBy default, `build_assert` uses inline assembly (i.e. `core::arch::asm`) to raise build-time errors. If you need to build with this crate on a target that does not support inline assembly (see [the Rust reference](https://doc.rust-lang.org/nightly/reference/inline-assembly.html)), you can enable the `no_asm` feature.\n\nWhen `no_asm` is enabled, `build_assert` raises a link error by referencing an undefined symbol if the assertion fails. By default, the symbol name is `__build_error_impl`. To avoid symbol conflicts, you can set the environment variable `BUILD_ERROR_SYM` to specify a different symbol before building:\n\n```text\nBUILD_ERROR_SYM=hello cargo build --release\n```\n\nNote that if the project has been previously built, the build cache should be cleared to ensure this change takes effect.\n\n## Under the Hood\n\nThe `build_assert` macro will be expanded to:\n\n```rust\nif !cond {\n  build_error!();\n}\n```\n\nIn release mode, the condition of `if` expression is expected to be evaluated by the optimizer. If `cond` is `true`, the results of\n`build_error` macro expansion will be optimized away. Otherwise, the expansion results will be retained.\n\nOn targets that support inline assembly, the `build_error` macro will expand to:\n\n```rust\ncore::arch::asm!(\"build error at file.rs:line:column\");\n```\n\nSince `build` is not a valid instruction on any target, the build will fail.\n\nOn targets that do not support inline assembly, the `build_error` macro will expand to:\n\n```rust\nextern \"Rust\" {\n  fn __build_error_impl() -\u003e !;\n}\n\nunsafe { __build_error_impl() }\n```\n\nIt raises a link error like this:\n\n```text\nerror: linking with `cc` failed: exit status: 1\n  |\n  = note: env -u ...\n  = note: /usr/bin/ld: ... .o: in function `rust_out::main::...':\n          ... .rs:6: undefined reference to `__build_error_impl'\n          collect2: error: ld returned 1 exit status\n\n  = note: ...\n```\n\nIn debug mode, since the optimizer will not run, the `build_error` macro will always be retained. We cannot raise build errors using the above method, otherwise no matter whether the condition is `true` or not, the build will always fail. So the `build_error` macro will expand to a `panic`.\n\n## References\n\nThe idea of `build_assert` macro came from the [Rust for Linux](https://rust-for-linux.github.io/docs/kernel/macro.build_assert.html) project.\nThis crate uses a different approach to implement the macro.\n\n## Changelog\n\nSee [CHANGELOG.md](CHANGELOG.md).\n\n## License\n\nCopyright (C) 2023 MaxXing. Licensed under either of [Apache 2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT) at your option.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxxsoft%2Fbuild_assert","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaxxsoft%2Fbuild_assert","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxxsoft%2Fbuild_assert/lists"}