Extract Sift{Error, Result} from pure BinaryHeap

This commit is contained in:
Michael Bradley 2025-01-30 20:19:08 -05:00
parent ebfce09c0e
commit 818e83d260
Signed by: MichaelBradley
SSH key fingerprint: SHA256:cj/YZ5VT+QOKncqSkx+ibKTIn0Obg7OIzwzl9BL8EO8
3 changed files with 29 additions and 26 deletions

View file

@ -1,5 +1,7 @@
mod pair;
mod py_item;
mod sift_result;
pub use pair::Pair;
pub use py_item::PyItem;
pub use sift_result::{SiftError, SiftResult};

View file

@ -0,0 +1,26 @@
use std::fmt::{Display, Formatter, Result as fmtResult};
/// Indicates why a sift failed
#[derive(Debug, Clone)]
pub struct SiftError {
/// The index that couldn't be sifted
index: usize,
/// The length of the array
len: usize,
}
impl SiftError {
/// Instantiates a `SiftError`
pub fn new(index: usize, len: usize) -> Self {
Self { index, len }
}
}
impl Display for SiftError {
fn fmt(&self, f: &mut Formatter) -> fmtResult {
write!(f, "Could not sift index {} of {}", self.index, self.len)
}
}
/// Whether a sift operation succeeded
pub type SiftResult = Result<(), SiftError>;

View file

@ -1,32 +1,7 @@
use std::fmt;
use crate::backing::containers::{SiftError, SiftResult};
use super::PureBacking;
/// Indicates why a sift failed
#[derive(Debug, Clone)]
struct SiftError {
/// The index that couldn't be sifted
index: usize,
/// The length of the array
len: usize,
}
impl SiftError {
/// Instantiates a `SiftError`
fn new(index: usize, len: usize) -> Self {
Self { index, len }
}
}
impl fmt::Display for SiftError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Could not sift index {} of {}", self.index, self.len)
}
}
/// Whether a sift operation succeeded
type SiftResult = Result<(), SiftError>;
/// A binary min-heap backed by an array
#[derive(Debug)]
pub struct BinaryHeap<T: PartialOrd + Clone + Send + Sync> {