nautilus-common 0.65.0

Common functionality and machinery for the Nautilus trading engine
Documentation
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! Actor system for event-driven message processing.
//!
//! This module provides the actor framework used throughout NautilusTrader for handling
//! data processing, event management, and asynchronous message handling. Actors are
//! lightweight components that process messages in isolation.

#![allow(unsafe_code)]

use std::{any::Any, fmt::Debug};

use ustr::Ustr;

#[doc(hidden)]
pub mod binding;
pub mod data_actor;
pub mod indicators;
pub mod registry;

mod access;
mod dispatch;
mod invocation;
mod storage;

#[cfg(test)]
pub(crate) mod tests;

// Re-exports
pub use data_actor::{DataActor, DataActorConfig, DataActorCore, DataActorNative};
#[doc(hidden)]
pub use dispatch::DispatchError as CallbackDispatchError;
pub(crate) use dispatch::{ChainContext, PublicationScope};
#[cfg(feature = "live")]
pub(crate) use dispatch::{SendChainContext, collect_command_contexts};

pub use crate::component::Component;

/// Drains at most `budget` callback slots at a caller-established safe boundary.
///
/// Returns whether queued slots remain after exhausting the budget. Retained roots alone do not
/// require another drain. Callers must release component, engine, and cache borrows before entry.
///
/// # Errors
///
/// Returns the first fatal dispatch error, or an active-work error if delivery cannot safely enter
/// or encounters an unfinished reservation. A busy head latches a fatal stalled-delivery error.
/// An otherwise successful drain entered during panic unwinding latches a fatal error on exit;
/// its result remains successful, and [`callback_failure`] reports the failure.
#[doc(hidden)]
pub fn drain_callbacks(budget: usize) -> Result<bool, CallbackDispatchError> {
    let result = dispatch::drain_at_boundary(budget)?;
    Ok(result.status == dispatch::DrainStatus::BudgetExhausted)
}

/// Returns the first fatal callback dispatch error on this thread.
#[doc(hidden)]
#[must_use]
pub fn callback_failure() -> Option<CallbackDispatchError> {
    dispatch::failure()
}

/// Releases queued callback captures and resets dispatch accounting at a safe boundary.
/// Clearing also resets the latched fatal failure.
///
/// # Errors
///
/// Returns an active-work error while access, reservations, or externally retained roots remain.
/// Callers must release queued commands and other retained work before clearing callbacks.
#[doc(hidden)]
pub fn clear_callbacks() -> Result<(), CallbackDispatchError> {
    dispatch::clear()
}

pub trait Actor: Any + Debug {
    /// The unique identifier for the actor.
    fn id(&self) -> Ustr;
    /// Handles the `msg`.
    fn handle(&mut self, msg: &dyn Any);
    /// Returns a reference to `self` as `Any`, for downcasting support.
    fn as_any(&self) -> &dyn Any;
    /// Returns a mutable reference to `self` as `Any`, for downcasting support.
    ///
    /// Default implementation simply coerces `&mut Self` to `&mut dyn Any`.
    ///
    /// # Note
    ///
    /// This method is not object-safe and thus only available on sized `Self`.
    fn as_any_mut(&mut self) -> &mut dyn Any
    where
        Self: Sized,
    {
        self
    }
}