Skip to main content

nautilus_common/python/
fifo.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//! Python bindings for FIFO cache.
17
18use pyo3::prelude::*;
19
20use crate::cache::fifo::FifoCache;
21
22#[pyo3::pyclass(
23    name = "FifoCache",
24    module = "nautilus_trader.core.nautilus_pyo3.common"
25)]
26#[pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.common")]
27#[derive(Debug)]
28pub struct PyFifoCache {
29    inner: FifoCache<String, 10_000>,
30}
31
32#[pymethods]
33#[pyo3_stub_gen::derive::gen_stub_pymethods]
34impl PyFifoCache {
35    #[new]
36    fn py_new() -> Self {
37        Self {
38            inner: FifoCache::new(),
39        }
40    }
41
42    fn __repr__(&self) -> String {
43        format!(
44            "FifoCache(capacity={}, len={})",
45            self.inner.capacity(),
46            self.inner.len()
47        )
48    }
49
50    #[getter]
51    fn capacity(&self) -> usize {
52        self.inner.capacity()
53    }
54
55    fn __len__(&self) -> usize {
56        self.inner.len()
57    }
58
59    #[expect(clippy::needless_pass_by_value)]
60    fn __contains__(&self, key: String) -> bool {
61        self.inner.contains(&key)
62    }
63
64    fn add(&mut self, key: String) {
65        self.inner.add(key);
66    }
67
68    #[expect(clippy::needless_pass_by_value)]
69    fn remove(&mut self, key: String) {
70        self.inner.remove(&key);
71    }
72
73    fn clear(&mut self) {
74        self.inner.clear();
75    }
76}