openraft 0.10.2

Advanced Raft consensus
Documentation
use crate::OptionalSend;

/// Trait defining the interface for a batch container.
///
/// A `Batch` is a collection that can be created from an iterator,
/// extended with new elements, iterated over, and viewed as a slice.
/// The default implementation is [`InlineBatch`](`super::InlineBatch`),
/// backed by `SmallVec<[T; 1]>`.
///
/// # Sequence contract
///
/// A custom batch must preserve every item, including duplicates, in encounter order:
///
/// - `FromIterator::from_iter` (used by [`Batch::of`]) collects the input sequence as-is.
/// - `Extend::extend` appends the input sequence after all existing items.
/// - `AsRef::as_ref` and the consuming `IntoIterator::into_iter` expose the same sequence.
///
/// Openraft relies on this contract to keep write payloads and their responders paired by position.
pub trait Batch<T>:
    AsRef<[T]> + Extend<T> + IntoIterator<Item = T, IntoIter: OptionalSend> + FromIterator<T> + OptionalSend
{
    /// Creates a `Batch` from any iterable.
    fn of(items: impl IntoIterator<Item = T>) -> Self {
        items.into_iter().collect()
    }

    /// Returns the number of elements.
    fn len(&self) -> usize {
        self.as_ref().len()
    }

    /// Returns `true` if the batch contains no elements.
    fn is_empty(&self) -> bool {
        self.as_ref().is_empty()
    }

    /// Returns a reference to the last element, or `None` if empty.
    fn last(&self) -> Option<&T> {
        self.as_ref().last()
    }
}

impl<T, B> Batch<T> for B where B: AsRef<[T]> + Extend<T> + IntoIterator<Item = T, IntoIter: OptionalSend> + FromIterator<T> + OptionalSend
{}