#proc-macro #assertions #tokens #input #assert #condition #checking

proc-macro-assertions

Easily create asserts on proc macro inputs

13 releases

0.1.5 Oct 11, 2023
0.1.4 Oct 11, 2023
0.0.7 Oct 10, 2023
0.0.4 Sep 17, 2023
0.0.2 Aug 5, 2023

#296 in Procedural macros

Download history 33/week @ 2024-06-17 47/week @ 2024-06-24 60/week @ 2024-07-01 80/week @ 2024-07-08 14/week @ 2024-07-15 107/week @ 2024-07-22 34/week @ 2024-07-29 135/week @ 2024-08-05 43/week @ 2024-08-12 35/week @ 2024-08-26 25/week @ 2024-09-16 34/week @ 2024-09-23 8/week @ 2024-09-30

67 downloads per month

MIT/Apache

91KB
2.5K SLoC

This crate makes it easy to check inputs of proc macros for some conditions. It works by generating const code which is never actually executed but the type checking of it ensures the conditions are met.

For more information see the Static Assertions crate. (Disclaimer: I am not affiliated with the author of that crate in any way)

The core type of this crate is the Assertion Store, which contains all the asserts that are generated and then later turned into tokens.

For the included types of assertions it's recommended to use the assert_into! macro.

The documentation of the individual items in this crate is pretty ok-ish, a funtional overview on how it works will be provided once I've got the time to draw that up. So for now just take a look at the individual types.

Example

pub use proc_macro_assertions::prelude::*;

let type_to_test: syn::Type = syn::parse_quote!(String);
let store = DefaultStore::new();
assert_into!(store | &type_to_test impl std::default::Default);
assert_into!(store | &type_to_test == String);
let tokens = quote::quote!{ #store };

Dependencies

~0.3–0.8MB
~19K SLoC