Skip to main content

datex_core/shared_values/
collapsed_container_value.rs

1use crate::{
2    shared_values::SharedContainerInner,
3    utils::{sheep::Sheep, sheep_mut::SheepMut},
4    values::{value::Value, value_container::ValueContainer},
5};
6use alloc::rc::Rc;
7use core::cell::{Ref, RefCell, RefMut};
8
9pub struct CollapsedContainerValueShared {
10    rc: Rc<RefCell<SharedContainerInner>>,
11}
12
13pub enum CollapsedContainerValue<'a> {
14    Shared(CollapsedContainerValueShared),
15    Local(&'a Value),
16}
17
18impl<'a> CollapsedContainerValue<'a> {
19    pub fn new_shared(rc: Rc<RefCell<SharedContainerInner>>) -> Self {
20        CollapsedContainerValue::Shared(CollapsedContainerValueShared { rc })
21    }
22
23    pub fn new_local(value: &'a Value) -> Self {
24        CollapsedContainerValue::Local(value)
25    }
26
27    /// Returns a [Sheep] containing the value of the [CollapsedContainerValue].
28    /// Note: The caller must ensure that between acquiring the [CollapsedContainerValue] and calling this method,
29    /// the most inner value of the parent has not been changed to another value.
30    pub fn borrow(&'a self) -> Sheep<'a, Value> {
31        match self {
32            CollapsedContainerValue::Shared(
33                CollapsedContainerValueShared { rc },
34            ) => {
35                Sheep::Ref(Ref::map(rc.borrow(), |v| {
36                    match v.base_shared_container().value_container() {
37                        ValueContainer::Local(local_value) => local_value,
38                        ValueContainer::Shared(_) => {
39                            // This should be reached because we have already collapsed all nested shared values
40                            panic!(
41                                "CollapsedContainerValue changed a local value to a shared value, which should not happen."
42                            )
43                        }
44                    }
45                }))
46            }
47            CollapsedContainerValue::Local(local_value) => {
48                Sheep::Borrowed(local_value)
49            }
50        }
51    }
52}
53
54pub enum CollapsedContainerValueMut<'a> {
55    Shared(CollapsedContainerValueShared),
56    Local(&'a mut Value),
57}
58
59impl<'a> CollapsedContainerValueMut<'a> {
60    pub fn new_shared(rc: Rc<RefCell<SharedContainerInner>>) -> Self {
61        CollapsedContainerValueMut::Shared(CollapsedContainerValueShared { rc })
62    }
63
64    pub fn new_local(value: &'a mut Value) -> Self {
65        CollapsedContainerValueMut::Local(value)
66    }
67
68    /// Returns a [SheepMut] containing the value of the [CollapsedContainerValue].
69    /// Note: The caller must ensure that between acquiring the [CollapsedContainerValue] and calling this method,
70    /// the most inner value of the parent has not been changed to another value.
71    pub fn borrow_mut(&'a mut self) -> SheepMut<'a, Value> {
72        match self {
73            CollapsedContainerValueMut::Shared(
74                CollapsedContainerValueShared { rc },
75            ) => {
76                SheepMut::Ref(RefMut::map(rc.borrow_mut(), |v| {
77                    match v.base_shared_container_mut().value_container_mut() {
78                        ValueContainer::Local(local_value) => local_value,
79                        ValueContainer::Shared(_) => {
80                            // This should be reached because we have already collapsed all nested shared values
81                            panic!(
82                                "CollapsedContainerValue changed a local value to a shared value, which should not happen."
83                            )
84                        }
85                    }
86                }))
87            }
88            CollapsedContainerValueMut::Local(local_value) => {
89                SheepMut::Borrowed(local_value)
90            }
91        }
92    }
93}