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/// Wires an actor type's core field into the native runtime contract.
19///
20/// The struct must contain a field that provides a
21/// [`DataActorCore`](crate::actor::DataActorCore) reference, either directly or
22/// by deref coercion through an intermediate core 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/// The generated native access implementation is runtime wiring. Normal actor code
27/// should use [`DataActor`](crate::actor::DataActor) facade methods such as
28/// `actor_id()`, `trader_id()`, `config()`, `clock()`, `cache()`, and the
29/// subscription methods.
30///
31/// This macro only wires the data actor core. Components with a wider native
32/// core, such as execution algorithms, keep their component-specific core
33/// access behind their own native trait.
34///
35/// # Examples
36///
37/// ```ignore
38/// use nautilus_common::{nautilus_actor, actor::DataActorCore};
39///
40/// pub struct MyActor {
41/// core: DataActorCore,
42/// // ...
43/// }
44///
45/// nautilus_actor!(MyActor);
46/// ```
47///
48/// With a custom field name:
49///
50/// ```ignore
51/// pub struct MyActor {
52/// actor_core: DataActorCore,
53/// // ...
54/// }
55///
56/// nautilus_actor!(MyActor, actor_core);
57/// ```
58#[macro_export]
59macro_rules! nautilus_actor {
60 ($ty:ty) => {
61 $crate::nautilus_actor!($ty, core);
62 };
63 ($ty:ty, $field:ident) => {
64 impl $crate::actor::DataActorNative for $ty {
65 fn core(&self) -> &$crate::actor::DataActorCore {
66 &self.$field
67 }
68
69 fn core_mut(&mut self) -> &mut $crate::actor::DataActorCore {
70 &mut self.$field
71 }
72 }
73 };
74}