Read the length of a list field, if it exists.
(&self, list: &EntityList<T>)
| 175 | |
| 176 | /// Read the length of a list field, if it exists. |
| 177 | fn len_of(&self, list: &EntityList<T>) -> Option<usize> { |
| 178 | let idx = list.index as usize; |
| 179 | // `idx` points at the list elements. The list length is encoded in the element immediately |
| 180 | // before the list elements. |
| 181 | // |
| 182 | // The `wrapping_sub` handles the special case 0, which is the empty list. This way, the |
| 183 | // cost of the bounds check that we have to pay anyway is co-opted to handle the special |
| 184 | // case of the empty list. |
| 185 | self.data.get(idx.wrapping_sub(1)).map(|len| len.index()) |
| 186 | } |
| 187 | |
| 188 | /// Allocate a storage block with a size given by `sclass`. |
| 189 | /// |