Mercurial > hg
comparison rust/hg-core/src/dirstate_tree/status.rs @ 49000:dd6b67d5c256 stable
rust: fix unsound `OwningDirstateMap`
As per the previous patch, `OwningDirstateMap` is unsound. Self-referential
structs are difficult to implement correctly in Rust since the compiler is
free to move structs around as much as it wants to. They are also very rarely
needed in practice, so the state-of-the-art on how they should be done within
the Rust rules is still a bit new.
The crate `ouroboros` is an attempt at providing a safe way (in the Rust sense)
of declaring self-referential structs. It is getting a lot attention and was
improved very quickly when soundness issues were found in the past: rather than
relying on our own (limited) review circle, we might as well use the de-facto
common crate to fix this problem. This will give us a much better chance of
finding issues should any new ones be discovered as well as the benefit of
fewer `unsafe` APIs of our own.
I was starting to think about how I would present a safe API to the old struct
but soon realized that the callback-based approach was already done in
`ouroboros`, along with a lot more care towards refusing incorrect structs.
In short: we don't return a mutable reference to the `DirstateMap` anymore, we
expect users of its API to pass a `FnOnce` that takes the map as an argument.
This allows our `OwningDirstateMap` to control the input and output lifetimes
of the code that modifies it to prevent such issues.
Changing to `ouroboros` meant changing every API with it, but it is relatively
low churn in the end. It correctly identified the example buggy modification of
`copy_map_insert` outlined in the previous patch as violating the borrow rules.
Differential Revision: https://phab.mercurial-scm.org/D12429
author | Raphaël Gomès <rgomes@octobus.net> |
---|---|
date | Tue, 05 Apr 2022 10:55:28 +0200 |
parents | e2f8ed37201c |
children | f3e8b0b0a8c2 6cd249556e20 |
comparison
equal
deleted
inserted
replaced
48999:cfd270d83169 | 49000:dd6b67d5c256 |
---|---|
38 /// traversal is the recursive `traverse_fs_directory_and_dirstate` function | 38 /// traversal is the recursive `traverse_fs_directory_and_dirstate` function |
39 /// and its use of `itertools::merge_join_by`. When reaching a path that only | 39 /// and its use of `itertools::merge_join_by`. When reaching a path that only |
40 /// exists in one of the two trees, depending on information requested by | 40 /// exists in one of the two trees, depending on information requested by |
41 /// `options` we may need to traverse the remaining subtree. | 41 /// `options` we may need to traverse the remaining subtree. |
42 #[timed] | 42 #[timed] |
43 pub fn status<'tree, 'on_disk: 'tree>( | 43 pub fn status<'dirstate>( |
44 dmap: &'tree mut DirstateMap<'on_disk>, | 44 dmap: &'dirstate mut DirstateMap, |
45 matcher: &(dyn Matcher + Sync), | 45 matcher: &(dyn Matcher + Sync), |
46 root_dir: PathBuf, | 46 root_dir: PathBuf, |
47 ignore_files: Vec<PathBuf>, | 47 ignore_files: Vec<PathBuf>, |
48 options: StatusOptions, | 48 options: StatusOptions, |
49 ) -> Result<(DirstateStatus<'on_disk>, Vec<PatternFileWarning>), StatusError> { | 49 ) -> Result<(DirstateStatus<'dirstate>, Vec<PatternFileWarning>), StatusError> |
50 { | |
50 // Force the global rayon threadpool to not exceed 16 concurrent threads. | 51 // Force the global rayon threadpool to not exceed 16 concurrent threads. |
51 // This is a stop-gap measure until we figure out why using more than 16 | 52 // This is a stop-gap measure until we figure out why using more than 16 |
52 // threads makes `status` slower for each additional thread. | 53 // threads makes `status` slower for each additional thread. |
53 // We use `ok()` in case the global threadpool has already been | 54 // We use `ok()` in case the global threadpool has already been |
54 // instantiated in `rhg` or some other caller. | 55 // instantiated in `rhg` or some other caller. |