1 unstable release
| 0.2.0 | Feb 12, 2025 |
|---|
#8 in #cuid
Used in tagid
5KB
Provides the Label derive macro for implementing the tagid::Label trait.
Overview
The Label derive macro automatically implements the Label trait for a given type,
defining an associated Labeler type and a default labeling function.
Example
use tagid::Label;
#[derive(Label)]
struct MyType;
The derived implementation generates:
impl tagid::Label for MyType {
type Labeler = tagid::MakeLabeling<Self>;
fn labeler() -> Self::Labeler { tagid::MakeLabeling::default() }
}
Dependencies
This macro relies on the syn, quote, and proc_macro crates to parse the input,
generate Rust code, and interact with the procedural macro system.
tagid-derive - Procedural macro for deriving Label in the tagid crate
The tagid-derive crate provides a procedural macro to derive the Label trait from the tagid crate.
This simplifies the implementation of label-based type identification.
Installation
Add the following to your Cargo.toml:
[dependencies]
tagid = "0.2"
tagid-derive = "0.2"
Usage
Deriving Label
The Label trait provides a compile-time string label for a type.
To derive it, simply annotate your struct or enum with #[derive(Label)]:
use tagid::Label;
#[derive(Label)]
struct Order;
assert_eq!(Order::label(), "Order");
Now, Order::label() will return a unique string label for the type:
This is useful for logging, serialization, or any scenario where type-based labeling is needed.
License
This project is licensed under the MIT License. See LICENSE for details.
Dependencies
~1.5MB
~39K SLoC