Skip to content

Commit bd8457f

Browse files
authored
Merge pull request #1076 from sdroege/slice-reserve
glib: Fix inverted boolean conditions when deciding whether to reserve new space
2 parents 4510666 + 8c4b10d commit bd8457f

File tree

3 files changed

+6
-15
lines changed

3 files changed

+6
-15
lines changed

glib/src/collections/ptr_slice.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -707,10 +707,7 @@ impl<T: TransparentPtrType> PtrSlice<T> {
707707
let new_capacity =
708708
usize::next_power_of_two(std::cmp::max(self.len + additional, MIN_SIZE) + 1);
709709
assert_ne!(new_capacity, 0);
710-
711-
if new_capacity <= self.capacity {
712-
return;
713-
}
710+
assert!(new_capacity > self.capacity);
714711

715712
unsafe {
716713
let ptr = if self.capacity == 0 {

glib/src/collections/slice.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -623,10 +623,7 @@ impl<T: TransparentType> Slice<T> {
623623
MIN_SIZE / mem::size_of::<T>(),
624624
));
625625
assert_ne!(new_capacity, 0);
626-
627-
if new_capacity <= self.capacity {
628-
return;
629-
}
626+
assert!(new_capacity > self.capacity);
630627

631628
unsafe {
632629
let ptr = if self.capacity == 0 {
@@ -688,7 +685,7 @@ impl<T: TransparentType> Slice<T> {
688685
#[inline]
689686
pub fn extend_from_slice(&mut self, other: &[T]) {
690687
// Nothing new to reserve as there's still enough space
691-
if self.len + other.len() <= self.capacity {
688+
if self.len + other.len() > self.capacity {
692689
self.reserve(other.len());
693690
}
694691

@@ -709,7 +706,7 @@ impl<T: TransparentType> Slice<T> {
709706
assert!(index <= self.len);
710707

711708
// Nothing new to reserve as there's still enough space
712-
if self.len + 1 <= self.capacity {
709+
if self.len + 1 > self.capacity {
713710
self.reserve(1);
714711
}
715712

@@ -732,7 +729,7 @@ impl<T: TransparentType> Slice<T> {
732729
#[allow(clippy::int_plus_one)]
733730
pub fn push(&mut self, item: T) {
734731
// Nothing new to reserve as there's still enough space
735-
if self.len + 1 <= self.capacity {
732+
if self.len + 1 > self.capacity {
736733
self.reserve(1);
737734
}
738735

glib/src/collections/strv.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,10 +667,7 @@ impl StrV {
667667
let new_capacity =
668668
usize::next_power_of_two(std::cmp::max(self.len + additional, MIN_SIZE) + 1);
669669
assert_ne!(new_capacity, 0);
670-
671-
if new_capacity <= self.capacity {
672-
return;
673-
}
670+
assert!(new_capacity > self.capacity);
674671

675672
unsafe {
676673
let ptr = if self.capacity == 0 {

0 commit comments

Comments
 (0)