Skip to main content

hoomd_derive/
lib.rs

1// Copyright (c) 2024-2026 The Regents of the University of Michigan.
2// Part of hoomd-rs, released under the BSD 3-Clause License.
3
4//! Derive macros for traits from a variety of hoomd-rs crates.
5//!
6//! # Complete documentation
7//!
8//! `hoomd-derive` is is a part of *hoomd-rs*. Read the [complete documentation]
9//! for more information.
10//!
11//! [complete documentation]: https://hoomd-rs.readthedocs.io
12
13#![allow(
14    clippy::missing_inline_in_public_items,
15    reason = "No need to inline macros"
16)]
17
18use proc_macro::TokenStream;
19use syn::{DeriveInput, parse_macro_input};
20
21mod delta_energy_insert;
22mod delta_energy_one;
23mod delta_energy_remove;
24mod maximum_interaction_range;
25mod orientation;
26mod position;
27mod site_pair_energy;
28mod total_energy;
29
30/// Automatically implement the `hoomd_interaction::DeltaEnergyInsert` trait.
31///
32/// The implemented `delta_energy_insert` sums the result of `delta_energy_insert`
33/// over all fields. The implementation returns early when any one field returns
34/// infinity.
35///
36/// Valid on:
37/// * Structs with named fields.
38/// * Tuple structs.
39#[proc_macro_derive(DeltaEnergyInsert)]
40pub fn delta_energy_insert_derive(input: TokenStream) -> TokenStream {
41    let input = parse_macro_input!(input as DeriveInput);
42    delta_energy_insert::delta_energy_insert(input).into()
43}
44
45/// Automatically implement the `hoomd_interaction::DeltaEnergyOne` trait.
46///
47/// The implemented `delta_energy_one` sums the result of `delta_energy_one`
48/// over all fields. The implementation returns early when any one field returns
49/// infinity.
50///
51/// Valid on:
52/// * Structs with named fields.
53/// * Tuple structs.
54#[proc_macro_derive(DeltaEnergyOne)]
55pub fn delta_energy_one_derive(input: TokenStream) -> TokenStream {
56    let input = parse_macro_input!(input as DeriveInput);
57    delta_energy_one::delta_energy_one(input).into()
58}
59
60/// Automatically implement the `hoomd_interaction::DeltaEnergyRemove` trait.
61///
62/// The implemented `delta_energy_remove` sums the result of `delta_energy_remove`
63/// over all fields. The implementation returns early when any one field returns
64/// infinity.
65///
66/// Valid on:
67/// * Structs with named fields.
68/// * Tuple structs.
69#[proc_macro_derive(DeltaEnergyRemove)]
70pub fn delta_energy_remove_derive(input: TokenStream) -> TokenStream {
71    let input = parse_macro_input!(input as DeriveInput);
72    delta_energy_remove::delta_energy_remove(input).into()
73}
74
75/// Automatically implement the `hoomd_interaction::MaximumInteractionRange` trait.
76///
77/// If the type has a `maximum_interaction_range` field, the derived implementation
78/// returns it. If the type does not, the derived implementation returns the
79/// maximum of `maximum_interaction_range()` of each field.
80///
81/// Valid on:
82/// * Structs with named fields.
83/// * Tuple structs.
84#[proc_macro_derive(MaximumInteractionRange)]
85pub fn maximum_interaction_range_derive(input: TokenStream) -> TokenStream {
86    let input = parse_macro_input!(input as DeriveInput);
87    maximum_interaction_range::maximum_interaction_range(input).into()
88}
89
90/// Automatically implement the `hoomd_microstate::property::Orientation` trait.
91///
92/// The derived implementation returns a reference to the structure's `orientation`
93/// field.
94///
95/// Valid on structs with named fields.
96#[proc_macro_derive(Orientation)]
97pub fn orientation_derive(input: TokenStream) -> TokenStream {
98    let input = parse_macro_input!(input as DeriveInput);
99    orientation::orientation(input)
100}
101
102/// Automatically implement the `hoomd_microstate::property::Position` trait.
103///
104/// The derived implementation returns a reference to the structure's `position`
105/// field.
106///
107/// Valid on structs with named fields.
108#[proc_macro_derive(Position)]
109pub fn position_derive(input: TokenStream) -> TokenStream {
110    let input = parse_macro_input!(input as DeriveInput);
111    position::position(input)
112}
113
114/// Automatically implement the `hoomd_interaction::SitePairEnergy` trait.
115///
116/// The implemented `site_pair_energy` sums the result of `site_pair_energy`
117/// over all fields. The implementation returns early when any one field returns
118/// infinity. The implemented `site_pair_energy_initial` behaves similarly.
119/// The derived `is_only_infinite_or_zero` returns true only when all fields
120/// also return true for the same method.
121///
122/// Valid on:
123/// * Structs with named fields.
124/// * Tuple structs.
125#[proc_macro_derive(SitePairEnergy)]
126pub fn site_pair_energy_derive(input: TokenStream) -> TokenStream {
127    let input = parse_macro_input!(input as DeriveInput);
128    site_pair_energy::site_pair_energy(input).into()
129}
130
131/// Automatically implement the `hoomd_interaction::TotalEnergy` trait.
132///
133/// The implemented `total_energy` sums the result of `total_energy`
134/// over all fields. The implementation returns early when any one field returns
135/// infinity.
136///
137/// Valid on:
138/// * Structs with named fields.
139/// * Tuple structs.
140#[proc_macro_derive(TotalEnergy)]
141pub fn total_energy_derive(input: TokenStream) -> TokenStream {
142    let input = parse_macro_input!(input as DeriveInput);
143    total_energy::total_energy(input).into()
144}