Skip to content

Commit c684b26

Browse files
committed
give ItemSliceSend a constructor and hide its field
As suggested by @cramertj in our review of unsafe code at Google.
1 parent d75159c commit c684b26

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,14 @@ where
130130
};
131131
size_progress.init(None, progress::bytes());
132132
let size_counter = size_progress.counter();
133-
let child_items = self.child_items.as_mut_slice();
134133
let object_progress = OwnShared::new(Mutable::new(object_progress));
135134

136135
let start = std::time::Instant::now();
137136
in_parallel_with_slice(
138137
&mut self.root_items,
139138
thread_limit,
140139
{
141-
let child_items = ItemSliceSend(std::ptr::slice_from_raw_parts_mut(
142-
child_items.as_mut_ptr(),
143-
child_items.len(),
144-
));
140+
let child_items = ItemSliceSend::new(&mut self.child_items);
145141
{
146142
let object_progress = object_progress.clone();
147143
move |thread_index| {

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
use crate::cache::delta::Item;
22

3-
pub struct ItemSliceSend<T>(pub *mut [T])
3+
pub struct ItemSliceSend<T>(*mut [T])
44
where
55
T: Send;
66

7+
impl<T> ItemSliceSend<T>
8+
where
9+
T: Send,
10+
{
11+
pub fn new(items: &mut [T]) -> Self {
12+
ItemSliceSend(std::ptr::slice_from_raw_parts_mut(items.as_mut_ptr(), items.len()))
13+
}
14+
}
15+
716
/// SAFETY: This would be unsafe if this would ever be abused, but it's used internally and only in a way that assure that the pointers
817
/// don't violate aliasing rules.
918
impl<T> Clone for ItemSliceSend<T>

0 commit comments

Comments
 (0)