Skip to content

Commit

Permalink
Merge pull request slightlyoutofphase#38 from delamonpansie/emptyslice
Browse files Browse the repository at this point in the history
Fix overly cautious assert in Index<Range<>> and IndexMut<Range<>>
  • Loading branch information
slightlyoutofphase authored Jun 27, 2020
2 parents 1854f15 + 735013c commit 359e807
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/trait_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ impl<T, const N: usize> Index<Range<usize>> for StaticVec<T, N> {
/// and if so returns a constant reference to a slice of elements `index.start..index.end`.
#[inline(always)]
fn index(&self, index: Range<usize>) -> &Self::Output {
assert!(index.start < index.end && index.end <= self.length);
assert!(index.start <= index.end && index.end <= self.length);
slice_from_raw_parts(
unsafe { self.ptr_at_unchecked(index.start) },
index.end - index.start,
Expand All @@ -530,7 +530,7 @@ impl<T, const N: usize> IndexMut<Range<usize>> for StaticVec<T, N> {
/// and if so returns a mutable reference to a slice of elements `index.start..index.end`.
#[inline(always)]
fn index_mut(&mut self, index: Range<usize>) -> &mut Self::Output {
assert!(index.start < index.end && index.end <= self.length);
assert!(index.start <= index.end && index.end <= self.length);
slice_from_raw_parts_mut(
unsafe { self.mut_ptr_at_unchecked(index.start) },
index.end - index.start,
Expand Down
9 changes: 9 additions & 0 deletions test/test_staticvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2052,6 +2052,15 @@ fn try_push() {
assert_eq!(vec2, [1, 2, 3, 3]);
}

#[test]
fn empty_slice() {
let mut vec = staticvec![1, 2, 3, 4, 5];
let s = &vec[0..0];
assert_eq!(0, s.len());
let s = &mut vec[0..0];
assert_eq!(0, s.len());
}

/*
#[test]
fn union() {
Expand Down

0 comments on commit 359e807

Please sign in to comment.