strict-typing
A Rust procedural macro that enforces strict typing on struct fields, enum variants, function signatures, trait definitions, and impl blocks. It prevents the use of bare primitive types, encouraging newtype wrappers for clarity and safety.
Rust is all about correctness in my humble opinion. And I want it to stay that way in my personal experience. I think this is how Rust should have been made. This should be a compiler built-in.
Motivation
Bare primitives like u32 or bool compile fine but carry no domain meaning. A u32 could be a user ID, a pixel count, or a port number — the type system won't stop you from mixing them up. Newtype wrappers (struct UserId(u32)) make intent explicit and catch misuse at compile time.
In my projects, I always create the transparent newtypes that precisely define what they carry. If I ever need to, I also create only the correct versions of conversions between such and other types.
Originally wrote for my own projects, I decided to share it with others. Enjoy.
#[strict_types] turns this from a convention into a compiler-enforced rule.
P.S. I decided to name the project strict-typing initially with the intention to
extend its functionality.
Usage
Add the dependency:
[]
= "0.1"
Basic — reject primitives in struct fields
use strict_types;
;
;
Enums
use strict_types;
;
Functions, traits, and impl blocks
use strict_types;
;
;
;
Extending the disallow list
By default, all numeric primitives plus bool and char are disallowed. Add more with disallow(...):
use strict_types;
/// # Strictness
///
/// - [String] disallowed because domain names should use a newtype.
Allowing specific primitives
Remove types from the default disallow list with allow(...):
use strict_types;
/// # Strictness
///
/// - [bool] allowed here because a simple flag is sufficient.
Documentation requirement
When using allow(...) or disallow(...), you must document each override in a /// # Strictness doc section. This forces authors to justify every exception, keeping the decision visible in code review.
Default disallowed types
u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize f32 f64 bool char
License
MIT