History

refactor

Sebastian Thiel committed 6 years ago

Commit deea36c1 parent 83a01022 files · +30 −30Browse files

Unified · Split

2 files
  1. git-odb/src/pack/tree/iter.rs +22 −22
  2. git-odb/src/pack/tree/mod.rs +8 −8
git-odb/src/pack/tree/iter.rs+22−22
@@ -1,14 +1,14 @@
11 use crate::pack::tree::{Item, Tree};
22
33 /// All the unsafe bits to support parallel iteration with write access
4−impl<D> Tree<D> {
4+impl<T> Tree<T> {
55 #[allow(unsafe_code)]
66 /// SAFETY: Called from node with is guaranteed to not be aliasing with any other node.
77 /// Note that we are iterating nodes that we are affecting here, but that only affects the
88 /// 'is_root' field fo the item, not the data field that we are writing here.
99 /// For all details see `from_node_take_entry()`.
10− unsafe fn from_node_put_data(&self, index: usize, data: D) {
11− let items_mut: &mut Vec<Item<D>> = &mut *(self.items.get());
10+ unsafe fn from_node_put_data(&self, index: usize, data: T) {
11+ let items_mut: &mut Vec<Item<T>> = &mut *(self.items.get());
1212 items_mut.get_unchecked_mut(index).data = data;
1313 }
1414
@@ -22,11 +22,11 @@ impl<D> Tree<D> { · 7 unmodified lines
2222 /// alias multiple nodes in the tree.
2323 /// It's safe for multiple threads to hold different chunks, as they are guaranteed to be non-overlapping and unique.
2424 /// If the tree is accessed after iteration, it will panic as no mutation is allowed anymore, nor is
25− unsafe fn from_node_take_entry(&self, index: usize) -> (D, Vec<usize>)
25+ unsafe fn from_node_take_entry(&self, index: usize) -> (T, Vec<usize>)
2626 where
27− D: Default,
27+ T: Default,
2828 {
29− let items_mut: &mut Vec<Item<D>> = &mut *(self.items.get());
29+ let items_mut: &mut Vec<Item<T>> = &mut *(self.items.get());
3030 let item = items_mut.get_unchecked_mut(index);
3131 let children = std::mem::take(&mut item.children);
3232 let data = std::mem::take(&mut item.data);
@@ -35,11 +35,11 @@ impl<D> Tree<D> { · 2 unmodified lines
3535
3636 #[allow(unsafe_code)]
3737 /// SAFETY: As `take_entry(…)` - but this one only takes if the data of Node is a root
38− unsafe fn from_iter_take_entry_if_root(&self, index: usize) -> Option<(D, Vec<usize>)>
38+ unsafe fn from_iter_take_entry_if_root(&self, index: usize) -> Option<(T, Vec<usize>)>
3939 where
40− D: Default,
40+ T: Default,
4141 {
42− let items_mut: &mut Vec<Item<D>> = &mut *(self.items.get());
42+ let items_mut: &mut Vec<Item<T>> = &mut *(self.items.get());
4343 let item = items_mut.get_unchecked_mut(index);
4444 if item.is_root {
4545 let children = std::mem::take(&mut item.children);
@@ -52,9 +52,9 @@ impl<D> Tree<D> { · 6 unmodified lines
5252 }
5353
5454 /// Iteration
55−impl<D> Tree<D> {
55+impl<T> Tree<T> {
5656 /// Return an iterator over chunks of roots. Roots are not children themselves, they have no parents.
57− pub fn iter_root_chunks(&mut self, size: usize) -> Chunks<D> {
57+ pub fn iter_root_chunks(&mut self, size: usize) -> Chunks<T> {
5858 Chunks {
5959 tree: self,
6060 size,
@@ -63,18 +63,18 @@ impl<D> Tree<D> { · 2 unmodified lines
6363 }
6464 }
6565
66−pub struct Node<'a, D> {
67− tree: &'a Tree<D>,
66+pub struct Node<'a, T> {
67+ tree: &'a Tree<T>,
6868 index: usize,
6969 children: Vec<usize>,
70− pub data: D,
70+ pub data: T,
7171 }
7272
73−impl<'a, D> Node<'a, D>
73+impl<'a, T> Node<'a, T>
7474 where
75− D: Default,
75+ T: Default,
7676 {
77− pub fn store_changes_then_into_child_iter(self) -> impl Iterator<Item = Node<'a, D>> {
77+ pub fn store_changes_then_into_child_iter(self) -> impl Iterator<Item = Node<'a, T>> {
7878 #[allow(unsafe_code)]
7979 // SAFETY: The index is valid as it was controlled by `add_child(…)`, then see `take_entry(…)`
8080 unsafe {
@@ -95,17 +95,17 @@ where · 14 unmodified lines
9595 }
9696 }
9797
98−pub struct Chunks<'a, D> {
99− tree: &'a Tree<D>,
98+pub struct Chunks<'a, T> {
99+ tree: &'a Tree<T>,
100100 size: usize,
101101 cursor: usize,
102102 }
103103
104−impl<'a, D> Iterator for Chunks<'a, D>
104+impl<'a, T> Iterator for Chunks<'a, T>
105105 where
106− D: Default,
106+ T: Default,
107107 {
108− type Item = Vec<Node<'a, D>>;
108+ type Item = Vec<Node<'a, T>>;
109109
110110 fn next(&mut self) -> Option<Self::Item> {
111111 if self.cursor == self.tree.one_past_last_seen_root {
git-odb/src/pack/tree/mod.rs+8−8
@@ -21,18 +21,18 @@ pub use iter::{Chunks, Node}; · 20 unmodified lines
2121
2222 pub mod from_offsets;
2323
24−pub struct Item<D> {
24+pub struct Item<T> {
2525 pub offset: u64,
2626 is_root: bool,
27− pub data: D,
27+ pub data: T,
2828 // TODO: figure out average amount of children per node and use smallvec instead
2929 children: Vec<usize>,
3030 }
3131 /// A tree that allows one-time iteration over all nodes and their children, consuming it in the process,
3232 /// while being shareable among threads without a lock.
3333 /// It does this by making the run-time guarantee that iteration only happens once.
34−pub struct Tree<D> {
35− items: UnsafeCell<Vec<Item<D>>>,
34+pub struct Tree<T> {
35+ items: UnsafeCell<Vec<Item<T>>>,
3636 last_added_offset: u64,
3737 one_past_last_seen_root: usize,
3838 }
@@ -42,7 +42,7 @@ pub struct Tree<D> { · 3 unmodified lines
4242 #[allow(unsafe_code)]
4343 unsafe impl<T> Sync for Tree<T> {}
4444
45−impl<D> Tree<D> {
45+impl<T> Tree<T> {
4646 pub fn with_capacity(num_objects: usize) -> Result<Self, Error> {
4747 if num_objects == 0 {
4848 return Err(Error::InvariantNonEmpty);
@@ -63,7 +63,7 @@ impl<D> Tree<D> { · 14 unmodified lines
6363 }
6464 }
6565
66− pub fn add_root(&mut self, offset: u64, data: D) -> Result<(), Error> {
66+ pub fn add_root(&mut self, offset: u64, data: T) -> Result<(), Error> {
6767 // SAFETY: Because we passed the assertion above which implies no other access is possible as per
6868 // standard borrow check rules.
6969 #[allow(unsafe_code)]
@@ -79,7 +79,7 @@ impl<D> Tree<D> { · 9 unmodified lines
7979 Ok(())
8080 }
8181
82− pub fn add_child(&mut self, base_offset: u64, offset: u64, data: D) -> Result<(), Error> {
82+ pub fn add_child(&mut self, base_offset: u64, offset: u64, data: T) -> Result<(), Error> {
8383 // SAFETY: Because we passed the assertion above which implies no other access is possible as per
8484 // standard borrow check rules.
8585 #[allow(unsafe_code)]
@@ -99,7 +99,7 @@ impl<D> Tree<D> { · 13 unmodified lines
9999 Ok(())
100100 }
101101
102− pub fn into_items(self) -> Vec<Item<D>> {
102+ pub fn into_items(self) -> Vec<Item<T>> {
103103 self.items.into_inner()
104104 }
105105 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t