Lightweight tagged data library.
1//! Enables compile-time separation of tags from different managers.
2
3#[cfg(doc)]
4use crate::generate_label;
5#[cfg(doc)]
6use crate::tag::Tag;
7#[cfg(doc)]
8use crate::TagManager;
9
10/// A type for compile-time separation of [`Tag`]s from different [`TagManager`]s.
11///
12/// This enables the compile-time guarantee that a [`Tag`] generated by one
13/// [`TagManager`] is never resolved through another [`TagManager`] (which might
14/// succeed but produce incorrect or nonsensical results at runtime if permitted).
15///
16/// # Safety
17///
18/// Note that the usage of [`Label`] types in this crate never actually instantiates
19/// any value of that type, and in general we recommend using a zero-sized type
20/// (this is what's done by the [`generate_label`] macro).
21///
22/// There aren't actual safety concerns around its use, and no choice to implement
23/// or not implement this marker trait can actually break safety guarantees of the
24/// crate, but it's marked `unsafe` to hint toward the special guarantees around
25/// the marker trait and encourage its construction through [`generate_label`].
26pub unsafe trait Label: Copy {}
27
28/// Generate a new type implementing [`Label`].
29///
30/// # Example
31///
32/// ```
33/// # use tagbuddy::generate_label;
34///
35/// generate_label! {
36/// /// Tags used for blog posts in this blog implementation.
37/// pub BlogPostTags {}
38/// }
39/// ```
40#[macro_export]
41macro_rules! generate_label {
42 ( $( $( #[$($attrss:meta)*] )* $visibility:vis $struct_name:ident {} )* ) => {
43 $(
44 $crate::generate_label! { @single $( #[$($attrss)*] )* $visibility $struct_name {} }
45 )*
46 };
47
48 ( @single $( #[$($attrss:meta)*] )* $visibility:vis $struct_name:ident {} ) => {
49 $( #[$($attrss)*] )*
50 #[derive(Debug, Copy, Clone)]
51 $visibility struct $struct_name;
52 unsafe impl $crate::label::Label for $struct_name {}
53 };
54}
55
56generate_label! {
57 /// The default label applied when no other label is provided.
58 pub DefaultLabel {}
59}