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

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

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

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);

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);

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);

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);

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);

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")
}

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")
}

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")
}

Clears the queue and resets internal flags

Trait Implementations

Formats the value using the given formatter. Read more

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

Performs the mutable indexing (container[index]) operation. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.