History

[git-diff] enforce greater restraint when using path-ids

That way, the trait impls can optimize access to their data structures.

Sebastian Thiel committed 5 years ago

Commit ad893201 parent 684de4b3 files · +19 −19Browse files

Showing the changes to git-diff/src (3 of 3 files). Show all

Unified · Split

3 files
  1. git-diff/src/tree/changes.rs +12 −12
  2. git-diff/src/tree/mod.rs +5 −6
  3. git-diff/src/tree/visit.rs +2 −1
git-diff/src/tree/changes.rs+12−12
@@ -75,14 +75,14 @@ impl<'a> tree::Changes<'a> { · 74 unmodified lines
7575 match (lhs_entries.next(), rhs_entries.next()) {
7676 (None, None) => {
7777 match state.trees.pop_front() {
78− Some((None, Some(rhs))) => {
79− delegate.set_current_path(rhs.parent_path_id.clone());
78+ Some((None, Some(mut rhs))) => {
79+ delegate.set_current_path(rhs.parent_path_id.take().expect("path-id is set"));
8080 rhs_entries = peekable(
8181 locate(&rhs.tree_id, &mut state.buf2).ok_or(Error::NotFound { oid: rhs.tree_id })?,
8282 );
8383 }
84− Some((Some(lhs), Some(rhs))) => {
85− delegate.set_current_path(lhs.parent_path_id.clone());
84+ Some((Some(mut lhs), Some(rhs))) => {
85+ delegate.set_current_path(lhs.parent_path_id.take().expect("path-id is set in lhs"));
8686 lhs_entries = peekable(
8787 locate(&lhs.tree_id, &mut state.buf1).ok_or(Error::NotFound { oid: lhs.tree_id })?,
8888 );
@@ -90,8 +90,8 @@ impl<'a> tree::Changes<'a> { · 1 unmodified line
9090 locate(&rhs.tree_id, &mut state.buf2).ok_or(Error::NotFound { oid: rhs.tree_id })?,
9191 );
9292 }
93− Some((Some(lhs), None)) => {
94− delegate.set_current_path(lhs.parent_path_id.clone());
93+ Some((Some(mut lhs), None)) => {
94+ delegate.set_current_path(lhs.parent_path_id.take().expect("path-id is set"));
9595 lhs_entries = peekable(
9696 locate(&lhs.tree_id, &mut state.buf1).ok_or(Error::NotFound { oid: lhs.tree_id })?,
9797 );
@@ -144,7 +144,7 @@ fn delete_entry_schedule_recursion<R: tree::Visit>( · 46 unmodified lines
144144 queue.push_back((
145145 Some(TreeInfo {
146146 tree_id: entry.oid.to_owned(),
147− parent_path_id: path_id,
147+ parent_path_id: Some(path_id),
148148 }),
149149 None,
150150 ));
@@ -174,7 +174,7 @@ fn add_entry_schedule_recursion<R: tree::Visit>( · 23 unmodified lines
174174 None,
175175 Some(TreeInfo {
176176 tree_id: entry.oid.to_owned(),
177− parent_path_id: path_id,
177+ parent_path_id: Some(path_id),
178178 }),
179179 ))
180180 }
@@ -285,11 +285,11 @@ fn handle_lhs_and_rhs_with_equal_filenames<R: tree::Visit>( · 104 unmodified lines
285285 queue.push_back((
286286 Some(TreeInfo {
287287 tree_id: lhs.oid.to_owned(),
288− parent_path_id: path_id.clone(),
288+ parent_path_id: Some(path_id),
289289 }),
290290 Some(TreeInfo {
291291 tree_id: rhs.oid.to_owned(),
292− parent_path_id: path_id,
292+ parent_path_id: None,
293293 }),
294294 ));
295295 }
@@ -317,7 +317,7 @@ fn handle_lhs_and_rhs_with_equal_filenames<R: tree::Visit>( · 21 unmodified lines
317317 None,
318318 Some(TreeInfo {
319319 tree_id: rhs.oid.to_owned(),
320− parent_path_id: path_id,
320+ parent_path_id: Some(path_id),
321321 }),
322322 ));
323323 }
@@ -344,7 +344,7 @@ fn handle_lhs_and_rhs_with_equal_filenames<R: tree::Visit>( · 20 unmodified lines
344344 queue.push_back((
345345 Some(TreeInfo {
346346 tree_id: lhs.oid.to_owned(),
347− parent_path_id: path_id,
347+ parent_path_id: Some(path_id),
348348 }),
349349 None,
350350 ));
git-diff/src/tree/mod.rs+5−6
@@ -3,8 +3,8 @@ use git_object::immutable; · 2 unmodified lines
33 use std::collections::VecDeque;
44
55 /// The state required to visit [Changes] to be instantiated with `State::default()`.
6−#[derive(Default, Clone)]
7−pub struct State<PathId: Default + Clone> {
6+#[derive(Default)]
7+pub struct State<PathId> {
88 buf1: Vec<u8>,
99 buf2: Vec<u8>,
1010 trees: VecDeque<TreeInfoPair<PathId>>,
@@ -12,13 +12,12 @@ pub struct State<PathId: Default + Clone> { · 1 unmodified line
1212
1313 type TreeInfoPair<PathId> = (Option<TreeInfo<PathId>>, Option<TreeInfo<PathId>>);
1414
15−#[derive(Clone)]
16−pub(crate) struct TreeInfo<PathId: Clone> {
15+pub(crate) struct TreeInfo<PathId> {
1716 pub tree_id: ObjectId,
18− pub parent_path_id: PathId,
17+ pub parent_path_id: Option<PathId>,
1918 }
2019
21−impl<P: Clone + Default> State<P> {
20+impl<P> State<P> {
2221 fn clear(&mut self) {
2322 self.trees.clear();
2423 self.buf1.clear();
git-diff/src/tree/visit.rs+2−1
@@ -52,9 +52,10 @@ impl Action { · 51 unmodified lines
5252 /// to turn tree A into tree B.
5353 pub trait Visit {
5454 /// A type capable of uniquely identifying paths in a tree.
55− type PathId: Clone + Default;
55+ type PathId;
5656
5757 /// Sets the path associated with the given `id` so future calls to push and pop components affect it instead.
58+ /// It will only ever be called once with a given `id`.
5859 fn set_current_path(&mut self, id: Self::PathId);
5960 /// Append a `component` to the end of a path, which may be empty, and associate it with the returned path id.
6061 fn push_tracked_path_component(&mut self, component: &BStr) -> Self::PathId;