From a024e642d3671a2a13cdc1ef75e0219c86c41692 Mon Sep 17 00:00:00 2001 From: Julien Cretin Date: Tue, 3 Nov 2020 13:30:39 +0100 Subject: [PATCH] Return zero instead of negative immediate capacity --- libraries/persistent_store/src/store.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/libraries/persistent_store/src/store.rs b/libraries/persistent_store/src/store.rs index 3d38b9c..725d91a 100644 --- a/libraries/persistent_store/src/store.rs +++ b/libraries/persistent_store/src/store.rs @@ -324,7 +324,7 @@ impl Store { if self.capacity()?.remaining() < length { return Err(StoreError::NoCapacity); } - if self.immediate_capacity()? < length as isize { + if self.immediate_capacity()? < usize_to_nat(length) { self.compact()?; } Ok(()) @@ -623,7 +623,7 @@ impl Store { if self.capacity()?.remaining() < length as usize { return Err(StoreError::NoCapacity); } - while self.immediate_capacity()? < length as isize { + while self.immediate_capacity()? < length { self.compact()?; } Ok(()) @@ -838,16 +838,10 @@ impl Store { } /// Returns the number of words that can be written without compaction. - /// - /// This can be temporarily negative during compaction. - fn immediate_capacity(&self) -> StoreResult { + fn immediate_capacity(&self) -> StoreResult { let tail = self.tail()?; let end = self.head()? + self.format.virt_size(); - Ok(if tail > end { - -((tail - end) as isize) - } else { - (end - tail) as isize - }) + Ok(end.get().saturating_sub(tail.get())) } /// Returns the position of the first word in the store.