nautilus-common 0.56.0

Common functionality and machinery for the Nautilus trading engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
// -------------------------------------------------------------------------------------------------
//  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.
// -------------------------------------------------------------------------------------------------

//! Compile-time type-safe message handler infrastructure.
//!
//! This module provides generic handler traits and types that enable type-safe
//! message dispatch without runtime downcasting for built-in message types.

use std::{any::Any, fmt::Debug, marker::PhantomData, rc::Rc};

use nautilus_core::UUID4;
use ustr::Ustr;

/// Compile-time type-safe message handler trait.
///
/// Provides zero-cost dispatch for statically typed messages. Can also be used
/// with `dyn Any` for dynamic dispatch when type flexibility is needed.
pub trait Handler<T: ?Sized>: 'static {
    /// Returns the unique identifier for this handler.
    fn id(&self) -> Ustr;

    /// Handles a message of type `T`.
    fn handle(&self, message: &T);
}

impl<T: ?Sized, H: Handler<T>> Handler<T> for Rc<H> {
    fn id(&self) -> Ustr {
        (**self).id()
    }

    fn handle(&self, message: &T) {
        (**self).handle(message);
    }
}

/// A shareable wrapper for typed handlers.
///
/// Provides reference-counted access to handlers. Supports both concrete types
/// for zero-cost dispatch and `dyn Any` for dynamic dispatch.
///
/// # Thread Safety
///
/// Uses `Rc` intentionally (not `Arc`) for single-threaded use within each
/// async runtime. The MessageBus uses thread-local storage to ensure each
/// thread gets its own handlers.
pub struct TypedHandler<T: 'static + ?Sized>(pub Rc<dyn Handler<T>>);

impl<T: 'static + ?Sized> Clone for TypedHandler<T> {
    fn clone(&self) -> Self {
        Self(Rc::clone(&self.0))
    }
}

impl<T: 'static + ?Sized> TypedHandler<T> {
    /// Returns the handler ID.
    pub fn id(&self) -> Ustr {
        self.0.id()
    }

    /// Handles a message by delegating to the inner handler.
    pub fn handle(&self, message: &T) {
        self.0.handle(message);
    }
}

impl<T: 'static> TypedHandler<T> {
    /// Creates a new typed handler from any type implementing `Handler<T>`.
    pub fn new<H: Handler<T>>(handler: H) -> Self {
        Self(Rc::new(handler))
    }

    /// Creates a new typed handler from a callback function.
    pub fn from<F>(callback: F) -> Self
    where
        F: Fn(&T) + 'static,
    {
        Self::new(CallbackHandler::new(None::<&str>, callback))
    }

    /// Creates a new typed handler from a callback function with a custom ID.
    pub fn from_with_id<S: AsRef<str>, F>(id: S, callback: F) -> Self
    where
        F: Fn(&T) + 'static,
    {
        Self::new(CallbackHandler::new(Some(id), callback))
    }
}

impl<T: 'static + ?Sized> Debug for TypedHandler<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct(stringify!(TypedHandler))
            .field("id", &self.0.id())
            .field("type", &std::any::type_name::<T>())
            .finish()
    }
}

impl<T: 'static + ?Sized> PartialEq for TypedHandler<T> {
    fn eq(&self, other: &Self) -> bool {
        self.0.id() == other.0.id()
    }
}

impl<T: 'static + ?Sized> Eq for TypedHandler<T> {}

impl<T: 'static + ?Sized> std::hash::Hash for TypedHandler<T> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.0.id().hash(state);
    }
}

impl From<Rc<dyn Handler<dyn Any>>> for TypedHandler<dyn Any> {
    fn from(handler: Rc<dyn Handler<dyn Any>>) -> Self {
        Self(handler)
    }
}

/// Type alias for handlers that work with dynamically-typed messages.
///
/// This replaces the legacy `MessageHandler` trait with a unified handler
/// type that supports both typed and dynamic dispatch through the same trait.
pub type ShareableMessageHandler = TypedHandler<dyn Any>;

/// Creates a new `ShareableMessageHandler` from an `Rc<dyn Handler<dyn Any>>`.
#[must_use]
pub fn shareable_handler(handler: Rc<dyn Handler<dyn Any>>) -> ShareableMessageHandler {
    TypedHandler(handler)
}

impl ShareableMessageHandler {
    /// Creates a handler from a typed closure that internally downcasts.
    ///
    /// Use this when you need Any-based routing but want type-safe handling.
    /// The callback will only be invoked if the message downcasts successfully.
    pub fn from_typed<T, F>(f: F) -> Self
    where
        T: 'static,
        F: Fn(&T) + 'static,
    {
        TypedHandler(Rc::new(DowncastingHandler::new(None::<&str>, f)))
    }

    /// Creates a handler from an Any-typed closure.
    pub fn from_any<F>(f: F) -> Self
    where
        F: Fn(&dyn Any) + 'static,
    {
        TypedHandler(Rc::new(AnyCallbackHandler::new(None::<&str>, f)))
    }
}

/// Handler that downcasts `&dyn Any` to a concrete type before calling the callback.
struct DowncastingHandler<T, F: Fn(&T)> {
    id: Ustr,
    callback: F,
    _marker: PhantomData<T>,
}

impl<T: 'static, F: Fn(&T) + 'static> DowncastingHandler<T, F> {
    fn new<S: AsRef<str>>(id: Option<S>, callback: F) -> Self {
        let id_ustr = id.map_or_else(
            || generate_handler_id::<T, F>(&callback),
            |s| Ustr::from(s.as_ref()),
        );
        Self {
            id: id_ustr,
            callback,
            _marker: PhantomData,
        }
    }
}

impl<T: 'static, F: Fn(&T) + 'static> Handler<dyn Any> for DowncastingHandler<T, F> {
    fn id(&self) -> Ustr {
        self.id
    }

    fn handle(&self, message: &dyn Any) {
        if let Some(typed_msg) = message.downcast_ref::<T>() {
            (self.callback)(typed_msg);
        } else {
            log::error!(
                "DowncastingHandler downcast failed: expected {} got {:?}",
                std::any::type_name::<T>(),
                message.type_id()
            );
        }
    }
}

/// Handler that directly receives `&dyn Any` without downcasting.
struct AnyCallbackHandler<F: Fn(&dyn Any)> {
    id: Ustr,
    callback: F,
}

impl<F: Fn(&dyn Any) + 'static> AnyCallbackHandler<F> {
    fn new<S: AsRef<str>>(id: Option<S>, callback: F) -> Self {
        let id_ustr = id.map_or_else(
            || {
                let callback_ptr = std::ptr::from_ref(&callback);
                let uuid = UUID4::new();
                Ustr::from(&format!("<{callback_ptr:?}>-{uuid}"))
            },
            |s| Ustr::from(s.as_ref()),
        );
        Self {
            id: id_ustr,
            callback,
        }
    }
}

impl<F: Fn(&dyn Any) + 'static> Handler<dyn Any> for AnyCallbackHandler<F> {
    fn id(&self) -> Ustr {
        self.id
    }

    fn handle(&self, message: &dyn Any) {
        (self.callback)(message);
    }
}

/// A callback-based handler implementation.
///
/// This is the typed equivalent of `TypedMessageHandler`,
/// but without runtime downcasting overhead.
pub struct CallbackHandler<T, F: Fn(&T)> {
    id: Ustr,
    callback: F,
    _marker: PhantomData<T>,
}

impl<T: 'static, F: Fn(&T) + 'static> CallbackHandler<T, F> {
    /// Creates a new callback handler with an optional custom ID.
    pub fn new<S: AsRef<str>>(id: Option<S>, callback: F) -> Self {
        let id_ustr = id.map_or_else(
            || generate_handler_id(&callback),
            |s| Ustr::from(s.as_ref()),
        );

        Self {
            id: id_ustr,
            callback,
            _marker: PhantomData,
        }
    }
}

impl<T: 'static, F: Fn(&T) + 'static> Handler<T> for CallbackHandler<T, F> {
    fn id(&self) -> Ustr {
        self.id
    }

    fn handle(&self, message: &T) {
        (self.callback)(message);
    }
}

impl<T, F: Fn(&T)> Debug for CallbackHandler<T, F> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct(stringify!(CallbackHandler))
            .field("id", &self.id)
            .field("type", &std::any::type_name::<T>())
            .finish()
    }
}

fn generate_handler_id<T: 'static + ?Sized, F: 'static + Fn(&T)>(callback: &F) -> Ustr {
    let callback_ptr = std::ptr::from_ref(callback);
    let uuid = UUID4::new();
    Ustr::from(&format!("<{callback_ptr:?}>-{uuid}"))
}

fn generate_into_handler_id<T: 'static, F: 'static + Fn(T)>(callback: &F) -> Ustr {
    let callback_ptr = std::ptr::from_ref(callback);
    let uuid = UUID4::new();
    Ustr::from(&format!("<{callback_ptr:?}>-{uuid}"))
}

/// Compile-time type-safe message handler trait that takes ownership.
///
/// Unlike [`Handler<T>`] which borrows messages, this trait takes ownership
/// of messages, enabling zero-copy processing when the handler needs to store
/// or forward the message.
pub trait IntoHandler<T>: 'static {
    /// Returns the unique identifier for this handler.
    fn id(&self) -> Ustr;

    /// Handles a message of type `T`, taking ownership.
    fn handle(&self, message: T);
}

impl<T, H: IntoHandler<T>> IntoHandler<T> for Rc<H> {
    fn id(&self) -> Ustr {
        (**self).id()
    }

    fn handle(&self, message: T) {
        (**self).handle(message);
    }
}

/// A shareable wrapper for ownership-based typed handlers.
///
/// This is the ownership-based equivalent of [`TypedHandler`], used for
/// point-to-point messaging where the sender transfers ownership of the message.
///
/// # Thread Safety
///
/// Uses `Rc` intentionally (not `Arc`) for single-threaded use within each
/// async runtime. The MessageBus uses thread-local storage to ensure each
/// thread gets its own handlers.
pub struct TypedIntoHandler<T: 'static>(pub Rc<dyn IntoHandler<T>>);

impl<T: 'static> Clone for TypedIntoHandler<T> {
    fn clone(&self) -> Self {
        Self(Rc::clone(&self.0))
    }
}

impl<T: 'static> TypedIntoHandler<T> {
    /// Creates a new typed into handler from any type implementing `IntoHandler<T>`.
    pub fn new<H: IntoHandler<T>>(handler: H) -> Self {
        Self(Rc::new(handler))
    }

    /// Creates a new typed into handler from a callback function.
    pub fn from<F>(callback: F) -> Self
    where
        F: Fn(T) + 'static,
    {
        Self::new(IntoCallbackHandler::new(None::<&str>, callback))
    }

    /// Creates a new typed into handler from a callback function with a custom ID.
    pub fn from_with_id<S: AsRef<str>, F>(id: S, callback: F) -> Self
    where
        F: Fn(T) + 'static,
    {
        Self::new(IntoCallbackHandler::new(Some(id), callback))
    }

    /// Returns the handler ID.
    pub fn id(&self) -> Ustr {
        self.0.id()
    }

    /// Handles a message by delegating to the inner handler, taking ownership.
    pub fn handle(&self, message: T) {
        self.0.handle(message);
    }
}

impl<T: 'static> Debug for TypedIntoHandler<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct(stringify!(TypedIntoHandler))
            .field("id", &self.0.id())
            .field("type", &std::any::type_name::<T>())
            .finish()
    }
}

impl<T: 'static> PartialEq for TypedIntoHandler<T> {
    fn eq(&self, other: &Self) -> bool {
        self.0.id() == other.0.id()
    }
}

impl<T: 'static> Eq for TypedIntoHandler<T> {}

impl<T: 'static> std::hash::Hash for TypedIntoHandler<T> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.0.id().hash(state);
    }
}

/// A callback-based handler implementation that takes ownership.
///
/// This is the ownership-based equivalent of `CallbackHandler`.
pub struct IntoCallbackHandler<T, F: Fn(T)> {
    id: Ustr,
    callback: F,
    _marker: PhantomData<T>,
}

impl<T: 'static, F: Fn(T) + 'static> IntoCallbackHandler<T, F> {
    /// Creates a new into callback handler with an optional custom ID.
    pub fn new<S: AsRef<str>>(id: Option<S>, callback: F) -> Self {
        let id_ustr = id.map_or_else(
            || generate_into_handler_id(&callback),
            |s| Ustr::from(s.as_ref()),
        );

        Self {
            id: id_ustr,
            callback,
            _marker: PhantomData,
        }
    }
}

impl<T: 'static, F: Fn(T) + 'static> IntoHandler<T> for IntoCallbackHandler<T, F> {
    fn id(&self) -> Ustr {
        self.id
    }

    fn handle(&self, message: T) {
        (self.callback)(message);
    }
}

impl<T, F: Fn(T)> Debug for IntoCallbackHandler<T, F> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct(stringify!(IntoCallbackHandler))
            .field("id", &self.id)
            .field("type", &std::any::type_name::<T>())
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use std::cell::RefCell;

    use rstest::rstest;

    use super::*;

    #[rstest]
    fn test_typed_handler_from_fn() {
        let received = Rc::new(RefCell::new(Vec::new()));
        let received_clone = received.clone();

        let handler = TypedHandler::from(move |msg: &String| {
            received_clone.borrow_mut().push(msg.clone());
        });

        handler.handle(&"test1".to_string());
        handler.handle(&"test2".to_string());

        assert_eq!(*received.borrow(), vec!["test1", "test2"]);
    }

    #[rstest]
    fn test_typed_handler_with_custom_id() {
        let handler = TypedHandler::from_with_id("custom-id", |_msg: &i32| {});

        assert_eq!(handler.id().as_str(), "custom-id");
    }

    #[rstest]
    fn test_typed_handler_equality() {
        let handler1 = TypedHandler::from_with_id("same-id", |_: &u32| {});
        let handler2 = TypedHandler::from_with_id("same-id", |_: &u32| {});
        let handler3 = TypedHandler::from_with_id("different-id", |_: &u32| {});

        assert_eq!(handler1, handler2);
        assert_ne!(handler1, handler3);
    }

    #[rstest]
    fn test_typed_handler_hash() {
        use std::collections::HashSet;

        let handler1 = TypedHandler::from_with_id("id-a", |_: &u32| {});
        let handler2 = TypedHandler::from_with_id("id-a", |_: &u32| {});
        let handler3 = TypedHandler::from_with_id("id-b", |_: &u32| {});

        let mut set = HashSet::new();
        set.insert(handler1);

        // Same ID should be considered duplicate
        assert!(!set.insert(handler2));
        // Different ID should be added
        assert!(set.insert(handler3));
        assert_eq!(set.len(), 2);
    }

    #[rstest]
    fn test_typed_handler_debug() {
        let handler = TypedHandler::from_with_id("debug-test", |_: &String| {});
        let debug_str = format!("{handler:?}");

        assert!(debug_str.contains("TypedHandler"));
        assert!(debug_str.contains("debug-test"));
    }

    // Tests for Handler<T> impl for Rc<H>
    struct TestHandler {
        id: Ustr,
        call_count: RefCell<usize>,
    }

    impl TestHandler {
        fn new(id: &str) -> Self {
            Self {
                id: Ustr::from(id),
                call_count: RefCell::new(0),
            }
        }
    }

    impl Handler<i32> for TestHandler {
        fn id(&self) -> Ustr {
            self.id
        }

        fn handle(&self, _message: &i32) {
            *self.call_count.borrow_mut() += 1;
        }
    }

    #[rstest]
    fn test_rc_handler_delegates_id() {
        let handler = TestHandler::new("rc-test-id");
        let rc_handler: Rc<TestHandler> = Rc::new(handler);

        assert_eq!(rc_handler.id(), Ustr::from("rc-test-id"));
    }

    #[rstest]
    fn test_rc_handler_delegates_handle() {
        let handler = TestHandler::new("rc-handle-test");
        let rc_handler: Rc<TestHandler> = Rc::new(handler);

        rc_handler.handle(&42);
        rc_handler.handle(&100);

        assert_eq!(*rc_handler.call_count.borrow(), 2);
    }

    #[rstest]
    fn test_rc_handler_shared_state() {
        let handler = TestHandler::new("shared-state");
        let rc1: Rc<TestHandler> = Rc::new(handler);
        let rc2 = rc1.clone();

        // Both Rc's point to same handler
        rc1.handle(&1);
        rc2.handle(&2);
        rc1.handle(&3);

        // All calls should be counted on the same handler
        assert_eq!(*rc1.call_count.borrow(), 3);
        assert_eq!(*rc2.call_count.borrow(), 3);
    }

    #[rstest]
    fn test_typed_handler_from_rc() {
        let handler = Rc::new(TestHandler::new("from-rc-test"));
        let typed: TypedHandler<i32> = TypedHandler::new(handler.clone());

        typed.handle(&42);

        assert_eq!(*handler.call_count.borrow(), 1);
        assert_eq!(typed.id(), Ustr::from("from-rc-test"));
    }
}