Skip to main content

nautilus_common/
macros.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Convenience macros for implementing actor boilerplate.
17
18/// Implements `Deref<Target = DataActorCore>` and `DerefMut` for an actor type.
19///
20/// The struct must contain a field that dereferences to
21/// [`DataActorCore`](crate::actor::DataActorCore), either directly or through
22/// an intermediate type (e.g. `ExecutionAlgorithmCore`).
23/// By default the macro expects the field to be named `core`; pass a second argument
24/// to use a different name.
25///
26/// # Examples
27///
28/// ```ignore
29/// use nautilus_common::{nautilus_actor, actor::DataActorCore};
30///
31/// pub struct MyActor {
32///     core: DataActorCore,
33///     // ...
34/// }
35///
36/// nautilus_actor!(MyActor);
37/// ```
38///
39/// With a custom field name:
40///
41/// ```ignore
42/// pub struct MyActor {
43///     actor_core: DataActorCore,
44///     // ...
45/// }
46///
47/// nautilus_actor!(MyActor, actor_core);
48/// ```
49#[macro_export]
50macro_rules! nautilus_actor {
51    ($ty:ty) => {
52        $crate::nautilus_actor!($ty, core);
53    };
54    ($ty:ty, $field:ident) => {
55        impl ::std::ops::Deref for $ty {
56            type Target = $crate::actor::DataActorCore;
57
58            fn deref(&self) -> &Self::Target {
59                &self.$field
60            }
61        }
62
63        impl ::std::ops::DerefMut for $ty {
64            fn deref_mut(&mut self) -> &mut Self::Target {
65                &mut self.$field
66            }
67        }
68    };
69}