Return zero instead of negative immediate capacity

This commit is contained in:
Julien Cretin
2020-11-03 13:30:39 +01:00
parent 410314b780
commit a024e642d3

View File

@@ -324,7 +324,7 @@ impl<S: Storage> Store<S> {
if self.capacity()?.remaining() < length { if self.capacity()?.remaining() < length {
return Err(StoreError::NoCapacity); return Err(StoreError::NoCapacity);
} }
if self.immediate_capacity()? < length as isize { if self.immediate_capacity()? < usize_to_nat(length) {
self.compact()?; self.compact()?;
} }
Ok(()) Ok(())
@@ -623,7 +623,7 @@ impl<S: Storage> Store<S> {
if self.capacity()?.remaining() < length as usize { if self.capacity()?.remaining() < length as usize {
return Err(StoreError::NoCapacity); return Err(StoreError::NoCapacity);
} }
while self.immediate_capacity()? < length as isize { while self.immediate_capacity()? < length {
self.compact()?; self.compact()?;
} }
Ok(()) Ok(())
@@ -838,16 +838,10 @@ impl<S: Storage> Store<S> {
} }
/// Returns the number of words that can be written without compaction. /// Returns the number of words that can be written without compaction.
/// fn immediate_capacity(&self) -> StoreResult<Nat> {
/// This can be temporarily negative during compaction.
fn immediate_capacity(&self) -> StoreResult<isize> {
let tail = self.tail()?; let tail = self.tail()?;
let end = self.head()? + self.format.virt_size(); let end = self.head()? + self.format.virt_size();
Ok(if tail > end { Ok(end.get().saturating_sub(tail.get()))
-((tail - end) as isize)
} else {
(end - tail) as isize
})
} }
/// Returns the position of the first word in the store. /// Returns the position of the first word in the store.