rusty_common/pools/
order_pool.rs

1//! High-performance memory pool for order objects
2//!
3//! Provides zero-allocation order creation and recycling for HFT applications.
4//! Uses the generic pool with order-specific optimizations.
5
6use crate::pools::generic_pool::{GenericPool, PoolStats, Poolable};
7
8// Note: This module provides the interface for order pooling.
9// The actual Order type integration happens at the application level
10// where rusty_model is available.
11
12/// Type alias for order pool - will be configured with actual Order type
13/// when integrated with rusty_model
14pub type OrderPoolTemplate<T> = GenericPool<T>;
15
16// PoolStats is already imported above
17
18/// Factory for creating order pools
19pub struct OrderPoolFactory;
20
21impl OrderPoolFactory {
22    /// Create a new order pool for the given order type
23    #[must_use]
24    pub fn create<T: Poolable + Clone>() -> OrderPoolTemplate<T> {
25        OrderPoolTemplate::new()
26    }
27
28    /// Create a new order pool with specified capacity
29    #[must_use]
30    pub fn create_with_capacity<T: Poolable + Clone>(capacity: usize) -> OrderPoolTemplate<T> {
31        OrderPoolTemplate::with_capacity(capacity)
32    }
33}
34
35// Global instance placeholder - will be properly typed when integrated
36thread_local! {
37    static GLOBAL_POOL_HOLDER: std::cell::RefCell<Option<Box<dyn std::any::Any>>> =
38        std::cell::RefCell::new(None);
39}
40
41/// Set the global order pool instance (to be called from application code)
42pub fn set_global_order_pool<T: Poolable + Clone + 'static>(pool: OrderPoolTemplate<T>) {
43    GLOBAL_POOL_HOLDER.with(|holder| {
44        *holder.borrow_mut() = Some(Box::new(pool));
45    });
46}
47
48/// Get the global order pool instance (to be called from application code)
49pub fn with_global_order_pool<T: Poolable + Clone + 'static, R>(
50    f: impl FnOnce(&OrderPoolTemplate<T>) -> R,
51) -> Option<R> {
52    GLOBAL_POOL_HOLDER.with(|holder| {
53        holder
54            .borrow()
55            .as_ref()
56            .and_then(|any_pool| any_pool.downcast_ref::<OrderPoolTemplate<T>>().map(f))
57    })
58}
59
60/// Placeholder functions that will be properly implemented when integrated with rusty_model
61/// These provide the expected interface but need proper typing
62/// Borrow an object from the global pool (generic version)
63pub fn borrow_from_global_pool<T: Poolable + Clone + 'static>() -> Option<T> {
64    with_global_order_pool(|pool: &OrderPoolTemplate<T>| pool.borrow())
65}
66
67/// Return an object to the global pool (generic version)
68pub fn return_to_global_pool<T: Poolable + Clone + 'static>(object: T) {
69    with_global_order_pool(|pool: &OrderPoolTemplate<T>| pool.return_object(object));
70}
71
72/// Get statistics from the global pool
73pub fn global_pool_stats<T: Poolable + Clone + 'static>() -> Option<PoolStats> {
74    with_global_order_pool(|pool: &OrderPoolTemplate<T>| pool.stats())
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[derive(Debug, Default, Clone)]
82    #[allow(dead_code)]
83    struct MockOrder {
84        id: u32,
85        symbol: String,
86        quantity: f64,
87    }
88
89    impl Poolable for MockOrder {
90        fn new_for_pool() -> Self {
91            Self {
92                id: 0,
93                symbol: String::from("POOL-INVALID"),
94                quantity: 0.0,
95            }
96        }
97
98        fn reset_for_pool(&mut self) {
99            self.id = 0;
100            self.symbol = String::from("POOL-INVALID");
101            self.quantity = 0.0;
102        }
103    }
104
105    #[test]
106    fn test_order_pool_factory() {
107        let pool = OrderPoolFactory::create::<MockOrder>();
108
109        let order1 = pool.borrow();
110        let order2 = pool.borrow();
111
112        pool.return_object(order1);
113        pool.return_object(order2);
114
115        let stats = pool.stats();
116        assert_eq!(stats.total_borrowed, 2);
117        assert_eq!(stats.total_returned, 2);
118    }
119
120    #[test]
121    fn test_global_pool_operations() {
122        let pool = OrderPoolFactory::create::<MockOrder>();
123        set_global_order_pool(pool);
124
125        let order = borrow_from_global_pool::<MockOrder>().unwrap();
126        return_to_global_pool(order);
127
128        let stats = global_pool_stats::<MockOrder>().unwrap();
129        assert!(stats.total_borrowed >= 1);
130        assert!(stats.total_returned >= 1);
131    }
132}