nautilus_common/cache/view.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//! Read-only view over the platform cache handed to adapter-facing code.
17
18use std::{
19 cell::{Ref, RefCell},
20 rc::Rc,
21};
22
23use super::Cache;
24
25// TODO: Reassess whether CacheView should consolidate with CacheApi once adapter and client
26// construction no longer need a cache-handle facade.
27/// Read-only view over the platform cache.
28///
29/// Adapter-facing code receives this type instead of the mutable cache handle so cache writes stay
30/// owned by the data and execution engines.
31#[derive(Clone, Debug)]
32pub struct CacheView {
33 inner: Rc<RefCell<Cache>>,
34}
35
36impl CacheView {
37 /// Creates a new [`CacheView`] from a cache handle.
38 #[must_use]
39 pub fn new(inner: Rc<RefCell<Cache>>) -> Self {
40 Self { inner }
41 }
42
43 /// Tries to borrow the cache without panicking when an engine owns a mutable borrow.
44 ///
45 /// # Errors
46 ///
47 /// Returns an error when the cache is mutably borrowed.
48 pub fn try_borrow(&self) -> Result<Ref<'_, Cache>, std::cell::BorrowError> {
49 self.inner.try_borrow()
50 }
51
52 /// Borrows the cache immutably.
53 ///
54 /// # Panics
55 ///
56 /// Panics if the cache is already mutably borrowed.
57 pub fn borrow(&self) -> Ref<'_, Cache> {
58 self.inner.borrow()
59 }
60}
61
62impl From<Rc<RefCell<Cache>>> for CacheView {
63 fn from(inner: Rc<RefCell<Cache>>) -> Self {
64 Self::new(inner)
65 }
66}