Mercurial > hg
diff rust/hg-cpython/src/dirstate/dirstate_map.rs @ 47123:d8ac62374943
dirstate-tree: Make `DirstateMap` borrow from a bytes buffer
… that has the contents of the `.hg/dirstate` file.
This only applies to the tree-based flavor of `DirstateMap`.
For now only the entire `&[u8]` slice is stored, so this is not useful yet.
Adding a lifetime parameter to the `DirstateMap` struct (in hg-core) makes
Python bindings non-trivial because we keep that struct in a Python object
that has a dynamic lifetime tied to Python’s reference-counting and GC.
As long as we keep the `PyBytes` that owns the borrowed bytes buffer next to
the borrowing struct, the buffer will live long enough for the borrows to stay
valid. However this relationship cannot be expressed in safe Rust code in a
way that would statisfy they borrow-checker. We use `unsafe` code to erase
that lifetime parameter, and encapsulate it in a safe abstraction similar to
the owning-ref crate: https://docs.rs/owning_ref/
Differential Revision: https://phab.mercurial-scm.org/D10557
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Fri, 30 Apr 2021 18:24:54 +0200 |
parents | 9aba0cde0ed9 |
children | cd8ca38fccff |
line wrap: on
line diff
--- a/rust/hg-cpython/src/dirstate/dirstate_map.rs Fri Apr 30 18:13:31 2021 +0200 +++ b/rust/hg-cpython/src/dirstate/dirstate_map.rs Fri Apr 30 18:24:54 2021 +0200 @@ -22,6 +22,7 @@ dirstate::non_normal_entries::{ NonNormalEntries, NonNormalEntriesIterator, }, + dirstate::owning::OwningDirstateMap, dirstate::{dirs_multiset::Dirs, make_dirstate_tuple}, parsers::dirstate_parents_to_pytuple, }; @@ -58,12 +59,13 @@ let dirstate_error = |_: DirstateError| { PyErr::new::<exc::OSError, _>(py, "Dirstate error".to_string()) }; - let bytes = on_disk.data(py); let (inner, parents) = if use_dirstate_tree { - let mut map = hg::dirstate_tree::dirstate_map::DirstateMap::new(); - let parents = map.read(bytes).map_err(dirstate_error)?; + let (map, parents) = + OwningDirstateMap::new(py, on_disk) + .map_err(dirstate_error)?; (Box::new(map) as _, parents) } else { + let bytes = on_disk.data(py); let mut map = RustDirstateMap::default(); let parents = map.read(bytes).map_err(dirstate_error)?; (Box::new(map) as _, parents)