strong-type
strong-type is a Rust crate that offers macros to easily create strongly typed and named primitive and string types. Strong typing helps in making code more expressive and less prone to errors, ensuring that each type is used in its intended way.
use StrongType;
;
let timestamp = new;
println!; // Timestamp(1701620628123456789)
Features
-
Derive trait
StrongType: Create a named strong type.- The macro automatically implement common traits like
Clone,Debug,Default,PartialEq,PartialOrd,Send, andSync. It also implementsDisplayby default, unless overridden by the custom_display attribute. - Conditionally, based on the underlying data type, traits like
Copy,Eq,Ord,Hashmay also be implemented. For primitive data types likei32orbool, these additional traits will be automatically included. - Numeric types, both integer and floating-point, also implement constants
MIN,MAX,INFINITY,NEG_INFINITY, andZERO. Additionally, for floating-point types,NANis implemented.
- The macro automatically implement common traits like
-
Attributes:
- Adding the following attributes to
#[strong_type(...)]allows for additional features:auto_operators: Automatically implements relevant arithmetic (for numeric types) or logical (for boolean types) operators.addable: Automatically implements theAdd,Sub, and other relevant traits. The attribute is a strict subset ofauto_operators.scalable: Automatically implements theMul,Div,Rem, and other relevant traits between a strong typed struct and its primitive type. Note that the attribute is not a subset ofauto_operators.custom_display: Allows users to manually implement theDisplaytrait, providing an alternative to the default display format.conversion: Automatically implementsFromandIntotraits for the underlying type. This is optional since conversion may make strong types less distinct.underlying: Specifies the underlying primitive type for nested strong types.
- Adding the following attributes to
Installation
Add strong-type to your Cargo.toml:
[]
= "0.12"
Supported underlying types:
- Integer types:
i8,i16,i32,i64,i128,isize - Unsigned integer types:
u8,u16,u32,u64,u128,usize - Floating-point types:
f32,f64 - Boolean type:
bool charString- Strong types of the above types
Examples
Creating a named strong type:
With a private field:
use StrongType;
;
let tag = new;
const TAG: Tag = const_new;
With a public field:
use StrongType;
;
let timestamp = Timestamp;
println!; // Timestamp(1701620628123456789)
Demonstrating type distinctiveness:
use StrongType;
use Any;
;
;
let x = new;
let y = new;
let z = new;
assert_eq!; // Same type: Second
assert_ne!; // Different types: Second versus Minute
Utilizing Hashability:
use HashSet;
;
let mut map = new;
map.insert;
map.insert;
assert_eq!;
Named integer type with arithmetic operations:
use StrongType;
;
let x = new;
let y = new;
let z = default;
assert_eq!;
assert_eq!;
assert_eq!;
assert!;
assert!;
assert_eq!;
;
let x = new;
assert_eq!;
Named bool type with logical operations:
use StrongType;
;
let x = new;
let y = new;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Custom display implementation with custom_display:
use ;
use StrongType;
;
println!; // "Second(2.72)"
println!; // "Second { value: 2.718281828459045 }"
Nested strong types:
;
;
;
Caveats:
- When using
#[derive(StrongType)], the traitsEqandPartialEqare implemented withimpl. As a result,StructuralEqandStructuralPartialEqremain unimplemented, preventing pattern matching with strong-typed primitives. #[strong_type(scalable)]does not work for nested strong types.