gmsol_programs/model/virtual_inventory.rs
1use std::ops::Deref;
2use std::sync::Arc;
3
4use crate::gmsol_store::{accounts::VirtualInventory, types::Pool};
5
6/// Virtual Inventory Model.
7///
8/// An off-chain encapsulation of the on-chain [`VirtualInventory`] account.
9/// This encapsulation uses Copy-On-Write (COW) semantics via [`Arc`] to allow
10/// efficient cloning while enabling mutations when needed.
11#[derive(Debug, Clone)]
12pub struct VirtualInventoryModel {
13 virtual_inventory: Arc<VirtualInventory>,
14}
15
16impl Deref for VirtualInventoryModel {
17 type Target = VirtualInventory;
18
19 fn deref(&self) -> &Self::Target {
20 &self.virtual_inventory
21 }
22}
23
24impl VirtualInventoryModel {
25 /// Create from parts.
26 pub fn from_parts(virtual_inventory: Arc<VirtualInventory>) -> Self {
27 Self { virtual_inventory }
28 }
29
30 /// Get the pool from the virtual inventory.
31 ///
32 /// This returns a reference to the [`Pool`] stored in the
33 /// [`PoolStorage`] of the virtual inventory.
34 pub fn pool(&self) -> &Pool {
35 &self.virtual_inventory.pool.pool
36 }
37
38 /// Get a mutable reference to the pool.
39 ///
40 /// This will trigger Copy-On-Write if the virtual inventory
41 /// is shared with other instances.
42 pub fn pool_mut(&mut self) -> &mut Pool {
43 let vi = self.make_virtual_inventory_mut();
44 // Access the pool field directly through the PoolStorage
45 &mut vi.pool.pool
46 }
47
48 /// Get a mutable reference to the virtual inventory.
49 ///
50 /// This will trigger Copy-On-Write if the virtual inventory
51 /// is shared with other instances.
52 fn make_virtual_inventory_mut(&mut self) -> &mut VirtualInventory {
53 Arc::make_mut(&mut self.virtual_inventory)
54 }
55}