nodo 0.18.5

A realtime framework for robotics
Documentation
// Copyright 2025 David Weikersdorfer

use crate::channels::RxRecvError;

/// Trait to pop elements from a collection
pub trait Pop {
    /// Type of popped element
    type Output;

    /// Returns true if there is no element to pop.
    fn is_empty(&self) -> bool;

    /// Removes the next element from the collection. Returns Err if there are no more elements.
    fn pop(&mut self) -> Result<Self::Output, RxRecvError>;

    /// Tries to remove the next element from the collection. Returns None if there are no more
    /// elements.
    fn try_pop(&mut self) -> Option<Self::Output> {
        self.pop().ok()
    }

    /// Tries to pop an element and replace `other` with it.
    fn try_pop_update<'a, 'b>(
        &'a mut self,
        other: &'b mut Option<Self::Output>,
    ) -> &'b mut Option<Self::Output> {
        match self.try_pop() {
            Some(x) => *other = Some(x),
            None => {}
        }
        other
    }
}

impl<'a, T1: Pop, T2: Pop> Pop for (&'a mut T1, &'a mut T2) {
    type Output = (<T1 as Pop>::Output, <T2 as Pop>::Output);

    fn is_empty(&self) -> bool {
        self.0.is_empty() || self.1.is_empty()
    }

    fn pop(&mut self) -> Result<Self::Output, RxRecvError> {
        if self.is_empty() {
            Err(RxRecvError::QueueEmtpy)
        } else {
            Ok((self.0.pop().unwrap(), self.1.pop().unwrap()))
        }
    }
}

impl<'a, T1: Pop, T2: Pop, T3: Pop> Pop for (&'a mut T1, &'a mut T2, &'a mut T3) {
    type Output = (
        <T1 as Pop>::Output,
        <T2 as Pop>::Output,
        <T3 as Pop>::Output,
    );

    fn is_empty(&self) -> bool {
        self.0.is_empty() || self.1.is_empty()
    }

    fn pop(&mut self) -> Result<Self::Output, RxRecvError> {
        if self.is_empty() {
            Err(RxRecvError::QueueEmtpy)
        } else {
            Ok((
                self.0.pop().unwrap(),
                self.1.pop().unwrap(),
                self.2.pop().unwrap(),
            ))
        }
    }
}