#newtype #macro #fields #strongly-typed #generate #safety #user-id

macro typed-fields

A collection of macros that generate newtypes

3 releases (breaking)

0.3.0 Dec 18, 2024
0.2.0 Aug 21, 2024
0.1.0 Jan 26, 2024

#289 in Procedural macros

Download history 29/week @ 2024-09-16 44/week @ 2024-09-23 38/week @ 2024-09-30 35/week @ 2024-10-07 127/week @ 2024-10-14 113/week @ 2024-10-21 59/week @ 2024-10-28 73/week @ 2024-11-04 5/week @ 2024-11-11 16/week @ 2024-11-25 104/week @ 2024-12-02 90/week @ 2024-12-09 179/week @ 2024-12-16 2/week @ 2024-12-23

288 downloads per month

MIT/Apache

19KB
377 lines

This crate contains a set of macros that can be used to generate strongly-typed fields for structs. The macros implement the newtype pattern, which allows the compiler to enforce type safety while still making it easy to convert the fields to and from their underlying representation.

Example

use typed_fields::number;

// Define a new type that is backed by an `i64`
number!(UserId);

// Create a new `UserId` from an `i64`
let id = UserId::new(42);

// Common traits like `Display` are automatically implemented for the type
println!("User ID: {}", id);

Dependencies

~0.2–1.3MB
~26K SLoC