Skip to content

Commit d655ee1

Browse files
committed
move delta Node into resolve.rs
The `Node` type is only used in `resolve.rs`, and using it safely depends on using the child items correctly, so let's define it there.
1 parent d27857d commit d655ee1

File tree

2 files changed

+43
-48
lines changed

2 files changed

+43
-48
lines changed

gix-pack/src/cache/delta/traverse/resolve.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,55 @@ use gix_features::{progress::Progress, threading, zlib};
77

88
use crate::{
99
cache::delta::{
10-
traverse::{
11-
util::{ItemSliceSend, Node},
12-
Context, Error,
13-
},
10+
traverse::{util::ItemSliceSend, Context, Error},
1411
Item,
1512
},
1613
data,
1714
data::EntryRange,
1815
};
1916

17+
/// An item returned by `iter_root_chunks`, allowing access to the `data` stored alongside nodes in a [`Tree`].
18+
pub(crate) struct Node<'a, T: Send> {
19+
pub item: &'a mut Item<T>,
20+
pub child_items: ItemSliceSend<'a, Item<T>>,
21+
}
22+
23+
impl<'a, T: Send> Node<'a, T> {
24+
/// Returns the offset into the pack at which the `Node`s data is located.
25+
pub fn offset(&self) -> u64 {
26+
self.item.offset
27+
}
28+
29+
/// Returns the slice into the data pack at which the pack entry is located.
30+
pub fn entry_slice(&self) -> crate::data::EntryRange {
31+
self.item.offset..self.item.next_offset
32+
}
33+
34+
/// Returns the node data associated with this node.
35+
pub fn data(&mut self) -> &mut T {
36+
&mut self.item.data
37+
}
38+
39+
/// Returns true if this node has children, e.g. is not a leaf in the tree.
40+
pub fn has_children(&self) -> bool {
41+
!self.item.children.is_empty()
42+
}
43+
44+
/// Transform this `Node` into an iterator over its children.
45+
///
46+
/// Children are `Node`s referring to pack entries whose base object is this pack entry.
47+
pub fn into_child_iter(self) -> impl Iterator<Item = Node<'a, T>> + 'a {
48+
let children = self.child_items;
49+
// SAFETY: The index is a valid index into the children array.
50+
// SAFETY: The resulting mutable pointer cannot be yielded by any other node.
51+
#[allow(unsafe_code)]
52+
self.item.children.iter().map(move |&index| Node {
53+
item: unsafe { children.get_mut(index as usize) },
54+
child_items: children.clone(),
55+
})
56+
}
57+
}
58+
2059
pub(crate) struct State<'items, F, MBFN, T: Send> {
2160
pub delta_bytes: Vec<u8>,
2261
pub fully_resolved_delta_bytes: Vec<u8>,
Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::marker::PhantomData;
22

3-
use crate::cache::delta::Item;
4-
53
pub(crate) struct ItemSliceSend<'a, T>
64
where
75
T: Send,
@@ -46,45 +44,3 @@ where
4644
// SAFETY: T is `Send`, and we only ever access one T at a time. And, ptrs need that assurance, I wonder if it's always right.
4745
#[allow(unsafe_code)]
4846
unsafe impl<T> Send for ItemSliceSend<'_, T> where T: Send {}
49-
50-
/// An item returned by `iter_root_chunks`, allowing access to the `data` stored alongside nodes in a [`Tree`].
51-
pub(crate) struct Node<'a, T: Send> {
52-
pub item: &'a mut Item<T>,
53-
pub child_items: ItemSliceSend<'a, Item<T>>,
54-
}
55-
56-
impl<'a, T: Send> Node<'a, T> {
57-
/// Returns the offset into the pack at which the `Node`s data is located.
58-
pub fn offset(&self) -> u64 {
59-
self.item.offset
60-
}
61-
62-
/// Returns the slice into the data pack at which the pack entry is located.
63-
pub fn entry_slice(&self) -> crate::data::EntryRange {
64-
self.item.offset..self.item.next_offset
65-
}
66-
67-
/// Returns the node data associated with this node.
68-
pub fn data(&mut self) -> &mut T {
69-
&mut self.item.data
70-
}
71-
72-
/// Returns true if this node has children, e.g. is not a leaf in the tree.
73-
pub fn has_children(&self) -> bool {
74-
!self.item.children.is_empty()
75-
}
76-
77-
/// Transform this `Node` into an iterator over its children.
78-
///
79-
/// Children are `Node`s referring to pack entries whose base object is this pack entry.
80-
pub fn into_child_iter(self) -> impl Iterator<Item = Node<'a, T>> + 'a {
81-
let children = self.child_items;
82-
// SAFETY: The index is a valid index into the children array.
83-
// SAFETY: The resulting mutable pointer cannot be yielded by any other node.
84-
#[allow(unsafe_code)]
85-
self.item.children.iter().map(move |&index| Node {
86-
item: unsafe { children.get_mut(index as usize) },
87-
child_items: children.clone(),
88-
})
89-
}
90-
}

0 commit comments

Comments
 (0)