Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Ensure correct assumptions about constants, types, and more in Rust

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
NotificationsYou must be signed in to change notification settings

nvzqz/static-assertions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Banner

Crates.ioDownloadsBuild Statusrustc ^1.37.0
Become a Patron!Buy me a coffee

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

This library lets you ensure correct assumptions about constants, types, andmore. See thedocs andFAQ for more info!

Installation

This crate is availableon crates.io and can be used byadding the following to your project'sCargo.toml:

[dependencies]static_assertions ="1.1.0"

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

#[macro_use]externcrate static_assertions;

Usage

This crate exposes the following macros:

FAQ

  • Q: When would I want to use this?

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

    Basic examples:

    • With the release of 1.39,str::len can be called in aconstcontext. Usingconst_assert!, one can check that a string generated fromelsewhere is of a given size:

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

      structFoo{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 seeimplemented. Create anew issue and describe how you'd imagine yourassertion to work, with example code to demonstrate.

    • Implement your own static assertion and create apull request.

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

    • Write docs. If you're familiar with how this library works, sharing yourknowledge 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 areat 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 librarycan be put indev-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 runningcargo test. Thissomewhat defeats the purpose of catching false static conditions up-front witha compilation failure.

  • Q: What isconst _?

    A: It's a way of creating an unnamed constant. This is used so that macroscan be called from a global scope without requiring a scope-unique label. Thislibrary makes use of the side effects of evaluating theconst expression.See the feature'stracking issueandissue #1for more info.

Changes

SeeCHANGELOG.mdfor a complete list of what has changed from one version to another.

License

This project is released under either:

at your choosing.


[8]ページ先頭

©2009-2025 Movatter.jp