Skip to content

Commit 124a19d

Browse files
authored
Merge pull request #468 from Kmeakin/slicevec-cleanup
Slicevec cleanup
2 parents 9025544 + 5d1940b commit 124a19d

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

fathom/src/alloc.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::ops::Deref;
1616
/// - when pushing to multiple slices at once
1717
/// - when element initialization code has the possibility of failure
1818
pub struct SliceVec<'a, Elem> {
19-
next_index: usize,
19+
len: usize,
2020
// SAFETY: The slice `self.elems[..self.next_index]` should only ever
2121
// contain elements initialized with `MaybeUninit::new`.
2222
elems: &'a mut [MaybeUninit<Elem>],
@@ -28,27 +28,49 @@ impl<'a, Elem> SliceVec<'a, Elem> {
2828
/// # Panics
2929
///
3030
/// If the type has drop-glue to be executed.
31-
pub fn new(scope: &'a scoped_arena::Scope<'a>, max_len: usize) -> SliceVec<'a, Elem> {
31+
pub fn new(scope: &'a scoped_arena::Scope<'a>, capacity: usize) -> SliceVec<'a, Elem> {
3232
// NOTE: Ensure that that the element type does not have any drop glue.
3333
// This would be problematic as we have no way of registering the
3434
// drop glue of `Elem` with `scoped_arena::Scope`.
3535
assert!(!std::mem::needs_drop::<Elem>());
3636

3737
SliceVec {
38-
next_index: 0,
39-
elems: scope.to_scope_many_with(max_len, MaybeUninit::uninit),
38+
len: 0,
39+
elems: scope.to_scope_many_with(capacity, MaybeUninit::uninit),
4040
}
4141
}
4242

43+
pub fn len(&self) -> usize {
44+
self.len
45+
}
46+
47+
pub fn is_empty(&self) -> bool {
48+
self.len() == 0
49+
}
50+
51+
pub fn capacity(&self) -> usize {
52+
self.elems.len()
53+
}
54+
55+
pub fn is_full(&self) -> bool {
56+
self.len() >= self.capacity()
57+
}
58+
4359
/// Push an element to the slice builder.
4460
///
4561
/// # Panics
4662
///
4763
/// If the pushing the element would exceed the maximum slice length
4864
/// supplied in [`SliceBuilder::new`].
4965
pub fn push(&mut self, elem: Elem) {
50-
self.elems[self.next_index] = MaybeUninit::new(elem);
51-
self.next_index += 1;
66+
if self.is_full() {
67+
panic!(
68+
"Cannot push onto a full `SliceVec` (capacity is {})",
69+
self.capacity()
70+
)
71+
}
72+
self.elems[self.len] = MaybeUninit::new(elem);
73+
self.len += 1;
5274
}
5375
}
5476

@@ -64,7 +86,7 @@ impl<'a, Elem> Deref for SliceVec<'a, Elem> {
6486
// - `self.next_index` is only incremented in `SliceBuilder::push`, and in that
6587
// case we make sure `self.elems[self.next_index]` has been initialized before
6688
// hand.
67-
unsafe { slice_assume_init_ref(&self.elems[..self.next_index]) }
89+
unsafe { slice_assume_init_ref(&self.elems[..self.len]) }
6890
}
6991
}
7092

@@ -78,7 +100,7 @@ impl<'a, Elem> From<SliceVec<'a, Elem>> for &'a [Elem] {
78100
// - `self.next_index` is only incremented in `SliceBuilder::push`, and in that
79101
// case we make sure `self.elems[self.next_index]` has been initialized before
80102
// hand.
81-
unsafe { slice_assume_init_ref(&slice.elems[..slice.next_index]) }
103+
unsafe { slice_assume_init_ref(&slice.elems[..slice.len]) }
82104
}
83105
}
84106

0 commit comments

Comments
 (0)