pipecircuit 0.0.4

Correct. Robust. Fast. Portable.
Documentation
use super::dense_key_index::FindOutcome;
use super::inconsistency::invariant_violation;
use super::{WarpTable, WarpTableInconsistency, WarpTableRow, WarpTableRowGetOutcome};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[must_use]
pub enum WarpTableUpdateOutcome<Id, Output> {
    Updated { id: Id, row: usize, output: Output },
    Absent { id: Id },
}

impl<Row: WarpTableRow, Brand> WarpTable<Row, Brand> {
    pub fn update<Output>(
        &mut self,
        id: Row::Id,
        update: impl FnOnce(Row::Mut<'_>) -> Output,
    ) -> WarpTableUpdateOutcome<Row::Id, Output> {
        if self.ids.len() != self.len {
            invariant_violation(WarpTableInconsistency::IdColumnLengthMismatch {
                ids: self.ids.len(),
                rows: self.len,
            });
        }
        let row = match self.primary_index.find(id, &self.ids) {
            Ok(FindOutcome::Present { row, .. }) => row,
            Ok(FindOutcome::Absent) => return WarpTableUpdateOutcome::Absent { id },
            Err(reason) => invariant_violation(WarpTableInconsistency::from(reason)),
        };
        let output = self.update_at(row, update);
        WarpTableUpdateOutcome::Updated { id, row, output }
    }

    pub(super) fn update_at<Output>(
        &mut self,
        row: usize,
        update: impl FnOnce(Row::Mut<'_>) -> Output,
    ) -> Output {
        let stored_id = self
            .ids
            .get(row)
            .unwrap_or_else(|| invariant_violation(WarpTableInconsistency::IdUnavailable { row }));
        let WarpTableRowGetOutcome::Present { row: mutable } =
            Row::get_mut(stored_id, &mut self.columns, &mut self.indexes, row)
                .unwrap_or_else(|reason| invariant_violation(WarpTableInconsistency::from(reason)));
        update(mutable)
    }
}