Mercurial > hg
comparison rust/hg-cpython/src/revlog.rs @ 51193:e79b0a4be3a7
rust-index: check equality between rust and cindex for `__len__`
author | Raphaël Gomès <rgomes@octobus.net> |
---|---|
date | Wed, 28 Jun 2023 11:36:22 +0200 |
parents | 65c9032e2e5a |
children | f0fa98752d67 |
comparison
equal
deleted
inserted
replaced
51192:65c9032e2e5a | 51193:e79b0a4be3a7 |
---|---|
140 PyErr::new::<IndexError, _>(py, "tuple index out of range")) | 140 PyErr::new::<IndexError, _>(py, "tuple index out of range")) |
141 } | 141 } |
142 let node_bytes = tup.get_item(py, 7).extract(py)?; | 142 let node_bytes = tup.get_item(py, 7).extract(py)?; |
143 let node = node_from_py_object(py, &node_bytes)?; | 143 let node = node_from_py_object(py, &node_bytes)?; |
144 | 144 |
145 let rev = self.len(py)? as BaseRevision; | |
145 let mut idx = self.cindex(py).borrow_mut(); | 146 let mut idx = self.cindex(py).borrow_mut(); |
146 | 147 |
147 // This is ok since we will just add the revision to the index | 148 // This is ok since we will just add the revision to the index |
148 let rev = Revision(idx.len() as BaseRevision); | 149 let rev = Revision(rev); |
149 idx.append(py, tup.clone_ref(py))?; | 150 idx.append(py, tup.clone_ref(py))?; |
150 self.index(py) | 151 self.index(py) |
151 .borrow_mut() | 152 .borrow_mut() |
152 .append(py_tuple_to_revision_data_params(py, tup)?) | 153 .append(py_tuple_to_revision_data_params(py, tup)?) |
153 .unwrap(); | 154 .unwrap(); |
257 // Since we call back through the high level Python API, | 258 // Since we call back through the high level Python API, |
258 // there's no point making a distinction between index_get | 259 // there's no point making a distinction between index_get |
259 // and index_getitem. | 260 // and index_getitem. |
260 | 261 |
261 def __len__(&self) -> PyResult<usize> { | 262 def __len__(&self) -> PyResult<usize> { |
262 self.cindex(py).borrow().inner().len(py) | 263 self.len(py) |
263 } | 264 } |
264 | 265 |
265 def __getitem__(&self, key: PyObject) -> PyResult<PyObject> { | 266 def __getitem__(&self, key: PyObject) -> PyResult<PyObject> { |
266 // this conversion seems needless, but that's actually because | 267 // this conversion seems needless, but that's actually because |
267 // `index_getitem` does not handle conversion from PyLong, | 268 // `index_getitem` does not handle conversion from PyLong, |
285 // this is an equivalent implementation of the index_contains() | 286 // this is an equivalent implementation of the index_contains() |
286 // defined in revlog.c | 287 // defined in revlog.c |
287 let cindex = self.cindex(py).borrow(); | 288 let cindex = self.cindex(py).borrow(); |
288 match item.extract::<i32>(py) { | 289 match item.extract::<i32>(py) { |
289 Ok(rev) => { | 290 Ok(rev) => { |
290 Ok(rev >= -1 && rev < cindex.inner().len(py)? as BaseRevision) | 291 Ok(rev >= -1 && rev < self.len(py)? as BaseRevision) |
291 } | 292 } |
292 Err(_) => { | 293 Err(_) => { |
293 cindex.inner().call_method( | 294 cindex.inner().call_method( |
294 py, | 295 py, |
295 "has_node", | 296 "has_node", |
430 RefCell::new(None), | 431 RefCell::new(None), |
431 RefCell::new(Some(buf)), | 432 RefCell::new(Some(buf)), |
432 ) | 433 ) |
433 } | 434 } |
434 | 435 |
436 fn len(&self, py: Python) -> PyResult<usize> { | |
437 let rust_index_len = self.index(py).borrow().len(); | |
438 let cindex_len = self.cindex(py).borrow().inner().len(py)?; | |
439 assert_eq!(rust_index_len, cindex_len); | |
440 Ok(cindex_len) | |
441 } | |
442 | |
435 /// This is scaffolding at this point, but it could also become | 443 /// This is scaffolding at this point, but it could also become |
436 /// a way to start a persistent nodemap or perform a | 444 /// a way to start a persistent nodemap or perform a |
437 /// vacuum / repack operation | 445 /// vacuum / repack operation |
438 fn fill_nodemap( | 446 fn fill_nodemap( |
439 &self, | 447 &self, |
440 py: Python, | 448 py: Python, |
441 nt: &mut NodeTree, | 449 nt: &mut NodeTree, |
442 ) -> PyResult<PyObject> { | 450 ) -> PyResult<PyObject> { |
443 let index = self.cindex(py).borrow(); | 451 let index = self.cindex(py).borrow(); |
444 for r in 0..index.len() { | 452 for r in 0..self.len(py)? { |
445 let rev = Revision(r as BaseRevision); | 453 let rev = Revision(r as BaseRevision); |
446 // in this case node() won't ever return None | 454 // in this case node() won't ever return None |
447 nt.insert(&*index, index.node(rev).unwrap(), rev) | 455 nt.insert(&*index, index.node(rev).unwrap(), rev) |
448 .map_err(|e| nodemap_error(py, e))? | 456 .map_err(|e| nodemap_error(py, e))? |
449 } | 457 } |