pipecircuit 0.0.2

Correct. Robust. Fast. Portable.
Documentation
use super::{WarpTable, WarpTableInsertWithIdOutcome, WarpTableRow};

#[derive(Debug, Eq, PartialEq)]
#[must_use]
pub enum WarpTableUpsertOutcome<Key, Row> {
    Inserted { key: Key, row: usize },
    Replaced { key: Key, row: usize, previous: Row },
    Full { key: Key, rejected: Row },
}

impl<Row: WarpTableRow, Brand> WarpTable<Row, Brand> {
    /// Inserts an absent caller-supplied key or replaces its existing row.
    pub fn upsert(&mut self, key: Row::Key, row: Row) -> WarpTableUpsertOutcome<Row::Key, Row> {
        match self.insert_with_id(key, row) {
            WarpTableInsertWithIdOutcome::Inserted { key, row } => {
                WarpTableUpsertOutcome::Inserted { key, row }
            }
            WarpTableInsertWithIdOutcome::AlreadyPresent {
                key,
                existing_row,
                rejected: replacement,
            } => WarpTableUpsertOutcome::Replaced {
                key,
                row: existing_row,
                previous: self.replace_at(key, existing_row, replacement),
            },
            WarpTableInsertWithIdOutcome::Full { key, rejected } => {
                WarpTableUpsertOutcome::Full { key, rejected }
            }
        }
    }
}