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 net_site_force_and_virial;
26mod net_site_force_virial_and_torque;
27mod orientation;
28mod position;
29mod site_pair_energy;
30mod total_energy;
31
32/// Automatically implement the `hoomd_interaction::DeltaEnergyInsert` trait.
33///
34/// The implemented `delta_energy_insert` sums the result of `delta_energy_insert`
35/// over all fields. The implementation returns early when any one field returns
36/// infinity.
37///
38/// Valid on:
39/// * Structs with named fields.
40/// * Tuple structs.
41#[proc_macro_derive(DeltaEnergyInsert)]
42pub fn delta_energy_insert_derive(input: TokenStream) -> TokenStream {
43    let input = parse_macro_input!(input as DeriveInput);
44    delta_energy_insert::delta_energy_insert(input).into()
45}
46
47/// Automatically implement the `hoomd_interaction::DeltaEnergyOne` trait.
48///
49/// The implemented `delta_energy_one` sums the result of `delta_energy_one`
50/// over all fields. The implementation returns early when any one field returns
51/// infinity.
52///
53/// Valid on:
54/// * Structs with named fields.
55/// * Tuple structs.
56#[proc_macro_derive(DeltaEnergyOne)]
57pub fn delta_energy_one_derive(input: TokenStream) -> TokenStream {
58    let input = parse_macro_input!(input as DeriveInput);
59    delta_energy_one::delta_energy_one(input).into()
60}
61
62/// Automatically implement the `hoomd_interaction::DeltaEnergyRemove` trait.
63///
64/// The implemented `delta_energy_remove` sums the result of `delta_energy_remove`
65/// over all fields. The implementation returns early when any one field returns
66/// infinity.
67///
68/// Valid on:
69/// * Structs with named fields.
70/// * Tuple structs.
71#[proc_macro_derive(DeltaEnergyRemove)]
72pub fn delta_energy_remove_derive(input: TokenStream) -> TokenStream {
73    let input = parse_macro_input!(input as DeriveInput);
74    delta_energy_remove::delta_energy_remove(input).into()
75}
76
77/// Automatically implement the `hoomd_interaction::MaximumInteractionRange` trait.
78///
79/// If the type has a `maximum_interaction_range` field, the derived implementation
80/// returns it. If the type does not, the derived implementation returns the
81/// maximum of `maximum_interaction_range()` of each field.
82///
83/// Valid on:
84/// * Structs with named fields.
85/// * Tuple structs.
86#[proc_macro_derive(MaximumInteractionRange)]
87pub fn maximum_interaction_range_derive(input: TokenStream) -> TokenStream {
88    let input = parse_macro_input!(input as DeriveInput);
89    maximum_interaction_range::maximum_interaction_range(input).into()
90}
91
92/// Automatically implement the `hoomd_interaction::NetSiteForceAndVirial` trait.
93///
94/// The implemented `net_site_force_and_virial` sums the result of `net_site_force_and_virial`
95/// over all fields.
96///
97/// Valid on:
98/// * Structs with named fields.
99/// * Tuple structs.
100#[proc_macro_derive(NetSiteForceAndVirial)]
101pub fn net_site_force_derive(input: TokenStream) -> TokenStream {
102    let input = parse_macro_input!(input as DeriveInput);
103    net_site_force_and_virial::net_site_force_and_virial(input).into()
104}
105
106/// Automatically implement the `hoomd_interaction::NetSiteForceVirialAndTorque` trait.
107///
108/// The implemented `net_site_force_virial_and_torque` sums the result of `net_site_force_virial_and_torque`
109/// over all fields.
110///
111/// Valid on:
112/// * Structs with named fields.
113/// * Tuple structs.
114#[proc_macro_derive(NetSiteForceVirialAndTorque)]
115pub fn net_site_force_and_torque_derive(input: TokenStream) -> TokenStream {
116    let input = parse_macro_input!(input as DeriveInput);
117    net_site_force_virial_and_torque::net_site_force_virial_and_torque(input).into()
118}
119
120/// Automatically implement the `hoomd_microstate::property::Orientation` trait.
121///
122/// The derived implementation returns a reference to the structure's `orientation`
123/// field.
124///
125/// Valid on structs with named fields.
126#[proc_macro_derive(Orientation)]
127pub fn orientation_derive(input: TokenStream) -> TokenStream {
128    let input = parse_macro_input!(input as DeriveInput);
129    orientation::orientation(input)
130}
131
132/// Automatically implement the `hoomd_microstate::property::Position` trait.
133///
134/// The derived implementation returns a reference to the structure's `position`
135/// field.
136///
137/// Valid on structs with named fields.
138#[proc_macro_derive(Position)]
139pub fn position_derive(input: TokenStream) -> TokenStream {
140    let input = parse_macro_input!(input as DeriveInput);
141    position::position(input)
142}
143
144/// Automatically implement the `hoomd_interaction::SitePairEnergy` trait.
145///
146/// The implemented `site_pair_energy` sums the result of `site_pair_energy`
147/// over all fields. The implementation returns early when any one field returns
148/// infinity. The implemented `site_pair_energy_initial` behaves similarly.
149/// The derived `is_only_infinite_or_zero` returns true only when all fields
150/// also return true for the same method.
151///
152/// Valid on:
153/// * Structs with named fields.
154/// * Tuple structs.
155#[proc_macro_derive(SitePairEnergy)]
156pub fn site_pair_energy_derive(input: TokenStream) -> TokenStream {
157    let input = parse_macro_input!(input as DeriveInput);
158    site_pair_energy::site_pair_energy(input).into()
159}
160
161/// Automatically implement the `hoomd_interaction::TotalEnergy` trait.
162///
163/// The implemented `total_energy` sums the result of `total_energy`
164/// over all fields. The implementation returns early when any one field returns
165/// infinity.
166///
167/// Valid on:
168/// * Structs with named fields.
169/// * Tuple structs.
170#[proc_macro_derive(TotalEnergy)]
171pub fn total_energy_derive(input: TokenStream) -> TokenStream {
172    let input = parse_macro_input!(input as DeriveInput);
173    total_energy::total_energy(input).into()
174}