Skip to main content

nautilus_common/actor/
mod.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//! Actor system for event-driven message processing.
17//!
18//! This module provides the actor framework used throughout NautilusTrader for handling
19//! data processing, event management, and asynchronous message handling. Actors are
20//! lightweight components that process messages in isolation.
21
22#![allow(unsafe_code)]
23
24use std::{any::Any, fmt::Debug};
25
26use ustr::Ustr;
27
28#[doc(hidden)]
29pub mod binding;
30pub mod data_actor;
31pub mod indicators;
32pub mod registry;
33
34mod access;
35mod dispatch;
36mod invocation;
37mod requests;
38mod storage;
39
40#[cfg(test)]
41pub(crate) mod tests;
42
43// Re-exports
44pub use data_actor::{DataActor, DataActorConfig, DataActorCore, DataActorNative};
45#[doc(hidden)]
46pub use dispatch::DispatchError as CallbackDispatchError;
47pub(crate) use dispatch::{ChainContext, PublicationScope};
48#[cfg(feature = "live")]
49pub(crate) use dispatch::{SendChainContext, collect_command_contexts};
50#[cfg(feature = "python")]
51pub(crate) use requests::ResponseScope;
52
53pub use crate::component::Component;
54
55/// Drains at most `budget` callback slots at a caller-established safe boundary.
56///
57/// Returns whether queued slots remain after exhausting the budget. Retained roots alone do not
58/// require another drain. Callers must release component, engine, and cache borrows before entry.
59///
60/// # Errors
61///
62/// Returns an error if a fatal dispatch failure has been recorded, delivery cannot safely enter, or
63/// delivery encounters an unfinished reservation. Returns the first fatal dispatch error, or an
64/// active-work error for unsafe entry or unfinished reservations. A busy head latches a fatal
65/// stalled-delivery error. An otherwise successful drain entered during panic unwinding latches a
66/// fatal error on exit; its result remains successful, and [`callback_failure`] reports the
67/// failure.
68#[doc(hidden)]
69pub fn drain_callbacks(budget: usize) -> Result<bool, CallbackDispatchError> {
70    let result = dispatch::drain_at_boundary(budget)?;
71    Ok(result.status == dispatch::DrainStatus::BudgetExhausted)
72}
73
74/// Returns the first fatal callback dispatch error on this thread.
75#[doc(hidden)]
76#[must_use]
77pub fn callback_failure() -> Option<CallbackDispatchError> {
78    dispatch::failure()
79}
80
81/// Releases queued callback captures and resets dispatch accounting at a safe boundary.
82/// Clearing also resets the latched fatal failure.
83///
84/// # Errors
85///
86/// Returns an error if access, reservations, or externally retained roots remain. The error reports
87/// active work. Callers must release queued commands and other retained work before clearing
88/// callbacks.
89#[doc(hidden)]
90pub fn clear_callbacks() -> Result<(), CallbackDispatchError> {
91    dispatch::clear()
92}
93
94pub trait Actor: Any + Debug {
95    /// The unique identifier for the actor.
96    fn id(&self) -> Ustr;
97    /// Handles the `msg`.
98    fn handle(&mut self, msg: &dyn Any);
99    /// Returns a reference to `self` as `Any`, for downcasting support.
100    fn as_any(&self) -> &dyn Any;
101    /// Returns a mutable reference to `self` as `Any`, for downcasting support.
102    ///
103    /// Default implementation simply coerces `&mut Self` to `&mut dyn Any`.
104    ///
105    /// # Note
106    ///
107    /// This method is not object-safe and thus only available on sized `Self`.
108    fn as_any_mut(&mut self) -> &mut dyn Any
109    where
110        Self: Sized,
111    {
112        self
113    }
114}