[][src]Struct rudac::queue::Circular

pub struct Circular<T> { /* fields omitted */ }

A circular buffer, circular queue, ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams.

Examples

let mut circular_buffer: rudac::queue::Circular<usize> = rudac::queue::Circular::new(1);

circular_buffer.enqueue(1);

match circular_buffer.dequeue() {
    Some(data) => assert_eq!(*data, 1),
    None => panic!("Data must not be empty")
}

Implementations

impl<T> Circular<T>[src]

pub fn new(capacity: usize) -> Circular<T>[src]

Creates a new instance of circular queue

Arguments

  • capacity - capacity of the queue. note that the real capacity is equal to capacity determined by the argument + 1

Examples

let mut circular_buffer: rudac::queue::Circular<usize> = rudac::queue::Circular::new(1);

pub fn size(&self) -> usize[src]

Returns number of items in the queue

Examples

let mut circular_buffer: rudac::queue::Circular<usize> = rudac::queue::Circular::new(1);
assert_eq!(circular_buffer.size(), 0);

circular_buffer.enqueue(1);
assert_eq!(circular_buffer.size(), 1);

pub fn empty(&self) -> bool[src]

Returns wether queue is empty or not. this implies wether size is equal to 0 or not. capacity will stay the same.

Examples

let mut circular_buffer: rudac::queue::Circular<usize> = rudac::queue::Circular::new(1);
assert_eq!(circular_buffer.empty(), true);

circular_buffer.enqueue(1);
assert_eq!(circular_buffer.empty(), false);

pub fn full(&self) -> bool[src]

Returns true if there are no more room for inserting new items.

Examples

let mut circular_buffer: rudac::queue::Circular<usize> = rudac::queue::Circular::new(1);
assert_eq!(circular_buffer.full(), false);

circular_buffer.enqueue(1);
assert_eq!(circular_buffer.full(), true);

pub fn enqueue(&mut self, element: T)[src]

If queue is not full it will insert an element at the end of the queue. If queue is full, oldest item will be discarded and new item will be inserted at the end of the queue.

Arguments

  • element: item to be inserted in the queue

Examples

let mut circular_buffer: rudac::queue::Circular<usize> = rudac::queue::Circular::new(1);

circular_buffer.enqueue(1);

pub fn dequeue(&mut self) -> Option<&T>[src]

Returns and discards the item at the front of the queue. Returns None if there are no items in the queue.

Examples

let mut circular_buffer: rudac::queue::Circular<usize> = rudac::queue::Circular::new(1);

circular_buffer.enqueue(1);

match circular_buffer.dequeue() {
    Some(data) => assert_eq!(*data, 1),
    None => panic!("Data must not be empty")
}

pub fn map(&mut self, transform: fn(_: &T) -> T)[src]

Transforms each element in the queue using the transform function provided

Arguments

  • transform: function that transforms each element of the queue and returns that transformed element

Examples

fn all_caps(text: &String) -> String {
    return text.to_uppercase();
}

let mut circular_buffer: rudac::queue::Circular<String> = rudac::queue::Circular::new(1);

circular_buffer.enqueue(String::from("element"));

circular_buffer.map(all_caps);

match circular_buffer.dequeue() {
    Some(data) => assert_eq!(*data, String::from("ELEMENT")),
    None => panic!("Data must not be None")
}

pub fn map_closure<F>(&mut self, transform: F) where
    F: Fn(&T) -> T, 
[src]

Transforms each element in the queue using the transform closure provided

Arguments

  • transform: closure that transforms each element of the queue and returns that transformed element

Examples

let mut circular_buffer: rudac::queue::Circular<String> = rudac::queue::Circular::new(1);

circular_buffer.enqueue(String::from("element"));

circular_buffer.map_closure(|text: &String| -> String{text.to_uppercase()});

match circular_buffer.dequeue() {
    Some(data) => assert_eq!(*data, String::from("ELEMENT")),
    None => panic!("Data must not be None")
}

pub fn clear(&mut self)[src]

Clears the queue and resets internal flags

Trait Implementations

impl<T: Debug> Debug for Circular<T>[src]

impl<T> Index<usize> for Circular<T>[src]

type Output = T

The returned type after indexing.

impl<T> IndexMut<usize> for Circular<T>[src]

impl<'a, T> IntoIterator for &'a Circular<T>[src]

type Item = &'a T

The type of the elements being iterated over.

type IntoIter = CircularIterator<'a, T>

Which kind of iterator are we turning this into?

Auto Trait Implementations

impl<T> RefUnwindSafe for Circular<T> where
    T: RefUnwindSafe

impl<T> Send for Circular<T> where
    T: Send

impl<T> Sync for Circular<T> where
    T: Sync

impl<T> Unpin for Circular<T> where
    T: Unpin

impl<T> UnwindSafe for Circular<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.