Skip to main content

composable_indexes_derive/
lib.rs

1use proc_macro::TokenStream;
2
3mod derive_index;
4mod derive_shallow_clone;
5
6/// Derives the `Index` trait for a struct where each field is itself an `Index`.
7///
8/// This macro generates an implementation of `composable_indexes::Index` for a struct
9/// where each field is itself an `Index`. This allows you to compose multiple indexes together
10/// without manually writing the Index trait implementation.
11///
12/// # Required Attribute
13///
14/// The `#[index(Type)]` attribute must be specified to indicate the type being indexed.
15#[proc_macro_derive(Index, attributes(index))]
16pub fn derive_index(input: TokenStream) -> TokenStream {
17    derive_index::run(input)
18}
19
20/// Derives the `ShallowClone` trait for a struct where each field is itself a `ShallowClone`.
21///
22/// This macro generates an implementation of `composable_indexes::ShallowClone` for a struct
23/// by calling `shallow_clone()` on each field. This is useful for types that use persistent
24/// data structures where shallow cloning is more efficient than deep cloning.
25///
26/// # Field Attributes
27///
28/// - `#[index(mark_as_shallow)]`: Use regular `clone()` instead of `shallow_clone()` for this field.
29///   This is useful for types that don't implement `ShallowClone`.
30///
31/// # Example
32///
33/// ```rust
34/// use composable_indexes::{index, aggregation};
35/// use composable_indexes_derive::ShallowClone;
36///
37/// #[derive(Clone, ShallowClone)]
38/// struct MyIndex {
39///     field1: index::im::BTree<u32>,
40///     field2: aggregation::Count,
41/// }
42/// ```
43#[proc_macro_derive(ShallowClone, attributes(index))]
44pub fn derive_shallow_clone(input: TokenStream) -> TokenStream {
45    derive_shallow_clone::run(input)
46}