| 86 | } |
| 87 | |
| 88 | pub trait ArrayRefExtension { |
| 89 | fn array_ref(&self) -> &ArrayRef; |
| 90 | |
| 91 | fn insert_map_with_txn(&self, txn: &mut TransactionMut, value: Option<MapPrelim>) -> MapRef { |
| 92 | let value = value.unwrap_or_default(); |
| 93 | self.array_ref().push_back(txn, value) |
| 94 | } |
| 95 | |
| 96 | fn insert_map_at_index_with_txn( |
| 97 | &self, |
| 98 | txn: &mut TransactionMut, |
| 99 | index: u32, |
| 100 | value: Option<MapPrelim>, |
| 101 | ) -> MapRef { |
| 102 | let value = value.unwrap_or_default(); |
| 103 | self.array_ref().insert(txn, index, value) |
| 104 | } |
| 105 | |
| 106 | fn mut_map_element_with_txn<'a, F, R>( |
| 107 | &'a self, |
| 108 | txn: &'a mut TransactionMut, |
| 109 | id: &str, |
| 110 | key: &str, |
| 111 | f: F, |
| 112 | ) where |
| 113 | F: FnOnce(&mut TransactionMut, &MapRef) -> Option<R>, |
| 114 | R: Prelim, |
| 115 | { |
| 116 | if let Some(index) = self.position_with_txn(txn, id, key) { |
| 117 | if let Some(YrsValue::YMap(map)) = self.array_ref().get(txn, index) { |
| 118 | if let Some(new_value) = f(txn, &map) { |
| 119 | self.array_ref().remove(txn, index); |
| 120 | self.array_ref().insert(txn, index, new_value); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /// Retrieves the position of an element in the underlying data structure |
| 127 | /// based on its ID and key. |
| 128 | /// |
| 129 | /// The `position_with_txn` method searches through the data structure and returns |
| 130 | /// the position (as a `u32`) of the element that matches the given ID and key. |
| 131 | /// If no match is found, the method returns `None`. |
| 132 | /// |
| 133 | /// The method specifically looks for elements of type `YrsValue::YMap` and checks |
| 134 | /// if the map contains the specified key with the corresponding ID value. |
| 135 | /// |
| 136 | /// # Parameters |
| 137 | /// - `txn`: A reference to a transaction object for reading. |
| 138 | /// - `id`: A string slice representing the ID value to match. |
| 139 | /// - `key`: A string slice representing the key to look up in the `YMap`. |
| 140 | /// |
| 141 | /// # Returns |
| 142 | /// - `Option<u32>`: The position of the matching element as a `u32` if found, or `None` otherwise. |
| 143 | /// |
| 144 | fn position_with_txn<T: ReadTxn>(&self, txn: &T, id: &str, key: &str) -> Option<u32> { |
| 145 | self |
no outgoing calls
no test coverage detected