pipecircuit 0.0.15

Correct. Robust. Fast. Portable.
Documentation
use super::{Selection, SelectionRows};
use crate::storage::warp_table::secondary_index::SecondaryIndexMembership;
use crate::storage::warp_table::{WarpTable, WarpTableRow};

#[must_use]
pub enum SecondaryIndexLookupOutcome<'table, Row: WarpTableRow, Brand, SecondaryKey> {
    Present {
        secondary_key: SecondaryKey,
        selection: Selection<'table, Row, Brand>,
    },
    Absent {
        secondary_key: SecondaryKey,
        selection: Selection<'table, Row, Brand>,
    },
}

impl<'table, Row: WarpTableRow, Brand, SecondaryKey>
    SecondaryIndexLookupOutcome<'table, Row, Brand, SecondaryKey>
{
    pub fn len(&self) -> u64 {
        self.selection().len()
    }

    pub fn is_empty(&self) -> bool {
        self.selection().is_empty()
    }

    pub fn rows(self) -> SelectionRows<'table, Row, Brand>
    where
        Row::Indexes: super::rows::StableSlotResolver,
    {
        self.into_selection().rows()
    }

    pub(super) const fn selection(&self) -> &Selection<'table, Row, Brand> {
        match self {
            Self::Present { selection, .. } | Self::Absent { selection, .. } => selection,
        }
    }

    pub(super) fn into_selection(self) -> Selection<'table, Row, Brand> {
        match self {
            Self::Present { selection, .. } | Self::Absent { selection, .. } => selection,
        }
    }
}

impl<'table, Row: WarpTableRow, Brand, SecondaryKey>
    From<SecondaryIndexLookupOutcome<'table, Row, Brand, SecondaryKey>>
    for Selection<'table, Row, Brand>
{
    fn from(outcome: SecondaryIndexLookupOutcome<'table, Row, Brand, SecondaryKey>) -> Self {
        outcome.into_selection()
    }
}

#[doc(hidden)]
pub fn bind_secondary_lookup<'table, Row, Brand, SecondaryKey>(
    table: &'table WarpTable<Row, Brand>,
    membership: SecondaryIndexMembership<'table, SecondaryKey>,
) -> SecondaryIndexLookupOutcome<'table, Row, Brand, SecondaryKey>
where
    Row: WarpTableRow,
{
    let (secondary_key, slots) = membership.into_parts();
    match slots {
        Some(slots) => SecondaryIndexLookupOutcome::Present {
            secondary_key,
            selection: Selection::borrowed(table, slots),
        },
        None => SecondaryIndexLookupOutcome::Absent {
            secondary_key,
            selection: Selection::empty(table),
        },
    }
}

#[doc(hidden)]
pub fn own_secondary_membership<SecondaryKey>(
    membership: SecondaryIndexMembership<'_, SecondaryKey>,
) -> (
    SecondaryKey,
    Option<crate::storage::warp_table::secondary_index::SlotSet>,
) {
    let (secondary_key, slots) = membership.into_parts();
    (secondary_key, slots.cloned())
}