remove Lsn::sub in favor of sub_checked

There is only one place doing subtraction, and it had a manually
implemented check.
This commit is contained in:
Eric Seppanen
2021-04-24 14:01:11 -07:00
parent 01e239afa3
commit d760446053
2 changed files with 10 additions and 12 deletions

View File

@@ -831,8 +831,9 @@ impl PageCache {
loop {
thread::sleep(conf.gc_period);
let last_lsn = self.get_last_valid_lsn();
if last_lsn.0 > conf.gc_horizon {
let horizon = last_lsn - conf.gc_horizon;
// checked_sub() returns None on overflow.
if let Some(horizon) = last_lsn.checked_sub(conf.gc_horizon) {
let mut maxkey = CacheKey {
tag: BufferTag {
rel: RelTag {

View File

@@ -1,7 +1,7 @@
#![warn(missing_docs)]
use std::fmt;
use std::ops::{Add, AddAssign, Sub};
use std::ops::{Add, AddAssign};
use std::path::Path;
use std::str::FromStr;
@@ -18,6 +18,12 @@ impl Lsn {
/// Maximum possible value for an LSN
pub const MAX: Lsn = Lsn(u64::MAX);
/// Subtract a number, returning None on overflow.
pub fn checked_sub<T: Into<u64>>(self, other: T) -> Option<Lsn> {
let other: u64 = other.into();
self.0.checked_sub(other).map(Lsn)
}
/// Parse an LSN from a filename in the form `0000000000000000`
pub fn from_filename<F>(filename: F) -> Result<Self, LsnParseError>
where
@@ -86,15 +92,6 @@ impl fmt::Display for Lsn {
}
}
impl Sub<u64> for Lsn {
type Output = Lsn;
fn sub(self, other: u64) -> Self::Output {
// panic if the subtraction overflows/underflows.
Lsn(self.0.checked_sub(other).unwrap())
}
}
impl Add<u64> for Lsn {
type Output = Lsn;