• Stars
    star
    566
  • Rank 78,774 (Top 2 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created about 7 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

Banner

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:

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:

at your choosing.

More Repositories

1

FileKit

Simple and expressive file management in Swift
Swift
2,289
star
2

RandomKit

Random data generation in Swift
Swift
1,473
star
3

divan

Fast and simple benchmarking for Rust projects
Rust
895
star
4

Sage

A cross-platform chess library for Swift
Swift
376
star
5

impls

A Rust macro to determine if a type implements a logical trait expression
Rust
246
star
6

Menubar-Colors

A macOS app for convenient access to the system color panel
Swift
175
star
7

fruity

Rusty bindings for Apple libraries
Rust
169
star
8

swift-bindgen

Bridging the gap between Swift and Rust
Rust
149
star
9

Unreachable

Unreachable code path optimization hint for Swift
Swift
103
star
10

Threadly

Type-safe thread-local storage in Swift
Swift
74
star
11

condtype

Choose Rust types at compile-time via constants
Rust
59
star
12

embed-plist-rs

Embed property list files like Info.plist directly in your Rust executable binary
Rust
39
star
13

Roman

Seamless Roman numeral conversion in Swift
Swift
36
star
14

rosy

Rust and Ruby, sitting in a tree 🌹
Rust
23
star
15

byte-set-rs

Efficient sets of bytes for Rust
Rust
19
star
16

cargo-emit

Talk to Cargo easily at build time
Rust
16
star
17

Clockmoji

Simple and intuitive time formatting using clock emoji.
Swift
13
star
18

XO

A cross-platform tic-tac-toe library for Swift
Swift
13
star
19

Lazy

Save the hard work for later, lazily evaluate values anywhere
Swift
12
star
20

Weak

Weak and Unowned as native Swift types
Swift
10
star
21

typebool

Type-level booleans in Rust for compile-time hackery
Rust
8
star
22

c-utf8-rs

UTF-8 encoded C strings for Rust
Rust
6
star
23

malloced

A malloc-ed box pointer type for Rust
Rust
5
star
24

dotfiles

Personal dotfiles
Shell
5
star
25

aloxide

Compile Rubies on Linux, macOS, and Windows
Rust
5
star
26

mods

Simpler Rust module declaration
Rust
5
star
27

bit-collection-rs

Iterate over bits in Rust
Rust
5
star
28

unsafe-unwrap-rs

Unsafely unwrap Result and Option types without checking in Rust
Rust
5
star
29

uncon-rs

Unchecked and unsafe type conversions in Rust
Rust
4
star
30

head-rs

Common Rust types with inline headers, such as HeaderVec for Vec
Rust
4
star
31

bad-rs

Unlicensed bad ideas
Rust
4
star
32

rust-workshop

A workshop on the Rust programming language
3
star
33

OneOrMore

A Swift collection of one or more elements
Swift
3
star
34

fmty

Rust library of composable `core::fmt` utilities
Rust
3
star
35

mem-cmp-rs

Safe memory comparison between types in Rust
Rust
2
star
36

ShiftOperations

A µframework for a needed ShiftOperations protocol that does not appear in the Swift standard library
Swift
1
star
37

rust-selector-example

My attempt at creating Objective-C selectors in Rust at compile time
Shell
1
star
38

asygnal

Handle signals (e.g. ctrl-c) efficiently and asynchronously in Rust
Rust
1
star
39

chance-rs

Random number generation in Rust
Rust
1
star