Skip to content

Commit 359e807

Browse files
Merge pull request slightlyoutofphase#38 from delamonpansie/emptyslice
Fix overly cautious assert in Index<Range<>> and IndexMut<Range<>>
2 parents 1854f15 + 735013c commit 359e807

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/trait_impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ impl<T, const N: usize> Index<Range<usize>> for StaticVec<T, N> {
516516
/// and if so returns a constant reference to a slice of elements `index.start..index.end`.
517517
#[inline(always)]
518518
fn index(&self, index: Range<usize>) -> &Self::Output {
519-
assert!(index.start < index.end && index.end <= self.length);
519+
assert!(index.start <= index.end && index.end <= self.length);
520520
slice_from_raw_parts(
521521
unsafe { self.ptr_at_unchecked(index.start) },
522522
index.end - index.start,
@@ -530,7 +530,7 @@ impl<T, const N: usize> IndexMut<Range<usize>> for StaticVec<T, N> {
530530
/// and if so returns a mutable reference to a slice of elements `index.start..index.end`.
531531
#[inline(always)]
532532
fn index_mut(&mut self, index: Range<usize>) -> &mut Self::Output {
533-
assert!(index.start < index.end && index.end <= self.length);
533+
assert!(index.start <= index.end && index.end <= self.length);
534534
slice_from_raw_parts_mut(
535535
unsafe { self.mut_ptr_at_unchecked(index.start) },
536536
index.end - index.start,

test/test_staticvec.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,6 +2052,15 @@ fn try_push() {
20522052
assert_eq!(vec2, [1, 2, 3, 3]);
20532053
}
20542054

2055+
#[test]
2056+
fn empty_slice() {
2057+
let mut vec = staticvec![1, 2, 3, 4, 5];
2058+
let s = &vec[0..0];
2059+
assert_eq!(0, s.len());
2060+
let s = &mut vec[0..0];
2061+
assert_eq!(0, s.len());
2062+
}
2063+
20552064
/*
20562065
#[test]
20572066
fn union() {

0 commit comments

Comments
 (0)