kernel/collections/
queue.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Interface for queue structure.
6
7pub trait Queue<T> {
8    /// Returns true if there are any items in the queue, false otherwise.
9    fn has_elements(&self) -> bool;
10
11    /// Returns true if the queue is full, false otherwise.
12    fn is_full(&self) -> bool;
13
14    /// Returns how many elements are in the queue.
15    fn len(&self) -> usize;
16
17    /// If the queue isn't full, add a new element to the back of the queue.
18    /// Returns whether the element was added.
19    fn enqueue(&mut self, val: T) -> bool;
20
21    /// Add a new element to the back of the queue, poping one from the front if necessary.
22    fn push(&mut self, val: T) -> Option<T>;
23
24    /// Remove the element from the front of the queue.
25    fn dequeue(&mut self) -> Option<T>;
26
27    /// Remove and return one (the first) element that matches the predicate.
28    fn remove_first_matching<F>(&mut self, f: F) -> Option<T>
29    where
30        F: Fn(&T) -> bool;
31
32    /// Remove all elements from the ring buffer.
33    fn empty(&mut self);
34
35    /// Retains only the elements that satisfy the predicate.
36    fn retain<F>(&mut self, f: F)
37    where
38        F: FnMut(&T) -> bool;
39}