//! Shared stack identity for inventories. Cloning a reference retains the same //! stack; `ItemStacks::copy` copies its value with copy-on-write components. //! These references belong to the simulation thread and release unreachable //! stacks automatically. No borrow is held across a container callback. use crate::{ component_patch::Prototype, item_stack_storage::{CopyResult, ItemStackData}, }; use std::{ cell::{Ref, RefCell, RefMut}, rc::Rc, }; #[derive(Debug)] pub struct StackRef(Rc>>); impl Clone for StackRef { fn clone(&self) -> Self { Self(self.0.clone()) } } // Native ItemStack object equality is reference identity. Value comparisons // remain explicit on ItemStackData (same_item_components/matches). impl PartialEq for StackRef { fn eq(&self, other: &Self) -> bool { self.same_reference(other) } } impl Eq for StackRef {} impl StackRef { pub fn new(data: ItemStackData) -> Self { Self(Rc::new(RefCell::new(data))) } pub fn read(&self) -> Ref<'_, ItemStackData> { self.0.borrow() } pub fn write(&self) -> RefMut<'_, ItemStackData> { self.0.borrow_mut() } pub fn same_reference(&self, other: &Self) -> bool { Rc::ptr_eq(&self.0, &other.0) } } #[derive(Clone, Debug)] pub struct ItemStacks { air: I, empty: StackRef, } impl ItemStacks { pub fn new(air: I) -> Self { Self { air, empty: StackRef::new(ItemStackData::new(air, air, 0, Prototype::new([]))), } } pub fn empty(&self) -> StackRef { self.empty.clone() } pub fn create(&self, item: I, count: i32, prototype: Prototype) -> StackRef { StackRef::new(ItemStackData::new(item, self.air, count, prototype)) } pub fn resolve(&self, copy: CopyResult>) -> StackRef { match copy { CopyResult::Empty => self.empty(), CopyResult::Stack(data) => StackRef::new(data), } } pub fn copy(&self, stack: &StackRef) -> StackRef { self.resolve(stack.read().copy()) } pub fn split(&self, stack: &StackRef, amount: i32) -> StackRef { self.resolve(stack.write().split(amount)) } }