百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

static-assertions

> 测试质量
开源

确保 Rust 中关于常量、类型等的假设正确

665 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

确保 Rust 中关于常量、类型等的假设正确

Compile-time assertions for Rust, brought to you by Nikolai Vazquez.

This library lets you ensure correct assumptions about constants, types, and more. See the docs and FAQ for more info!

Installation

This crate is available on crates.io and can be used by adding the following to your project's Cargo.toml:

[dependencies]
static_assertions = "1.1.0"

and this to your crate root (main.rs or lib.rs):

#[macro_use]
extern crate static_assertions;

Usage

This crate exposes the following macros:

  • assert_cfg!
  • assert_eq_align!
  • assert_eq_size!
  • assert_eq_size_ptr!
  • assert_eq_size_val!
  • assert_fields!
  • assert_impl_all!
  • assert_impl_any!
  • assert_impl_one!
  • assert_not_impl_all!
  • assert_not_impl_any!
  • assert_obj_safe!
  • assert_trait_sub_all!
  • assert_trait_super_all!
  • assert_type_eq_all!
  • assert_type_ne_all!
  • const_assert!
  • const_assert_eq!
  • const_assert_ne!

FAQ

  • Q: When would I want to use this?

    A: This library is useful for when wanting to ensure properties of constants, types, and traits.

    Basic examples:

    • With the release of 1.39, str::len can be called in a const context. Using const_assert!, one can check that a string generated from elsewhere is of a given size:

      const DATA: &str = include_str!("path/to/string.txt");
      
      const_assert!(DATA.len() < 512);
      
    • Have a type that absolutely must implement certain traits? With assert_impl_all!, one can ensure this:

      struct Foo {
          value: // ...
      }
      
      assert_impl_all!(Foo: Send, Sync);
      
  • Q: How can I contribute?

    A: A couple of ways! You can:

    • Attempt coming up with some form of static analysis that you'd like to see implemented. Create a new issue and describe how you'd imagine your assertion to work, with example code to demonstrate.

    • Implement your own static assertion and create a pull request.

    • Give feedback. What are some pain points? Where is it unpleasant?

    • Write docs. If you're familiar with how this library works, sharing your knowledge with the rest its users would be great!

  • Q: Will this affect my compiled binary?

    A: Nope! There is zero runtime cost to using this because all checks are at compile-time, and so no code is emitted to run.

  • Q: Will this affect my compile times?

    A: Likely not by anything perceivable. If this is a concern, this library can be put in dev-dependencies:

    [dev-dependencies]
    static_assertions = "1.1.0"
    

    and then assertions can be conditionally run behind #[cfg(test)]:

    #[cfg(test)]
    const_assert_eq!(MEANING_OF_LIFE, 42);
    

    However, the assertions will only be checked when running cargo test. This somewhat defeats the purpose of catching false static conditions up-front with a compilation failure.

  • Q: What is const _?

    A: It's a way of creating an unnamed constant. This is used so that macros can be called from a global scope without requiring a scope-unique label. This library makes use of the side effects of evaluating the const expression. See the feature's tracking issue and issue #1 for more info.

Changes

See CHANGELOG.md for a complete list of what has changed from one version to another.

License

This project is released under either:

  • MIT License

  • Apache License (Version 2.0)

at your choosing.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rustassertcompile-timeruststatic-assertions

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类测试质量
定价开源

> 相关工具

J
Jest
JavaScript 测试框架
P
Playwright
现代端到端测试框架
E
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.