Struct Buffer

Source
pub struct Buffer<T: Clone> { /* private fields */ }
Expand description

A FIFO buffer with a growable size and a capacity limit

§Type parameters

  • T: Any type that implements the Clone trait.

§Examples

let mut buf = Buffer::new(3);

// Add some elements
assert_eq!(buf.add(6), Ok(None));
assert_eq!(buf.add(7), Ok(None));

// Remove an element
assert_eq!(buf.remove(), Ok(6));

// Peek at the next element scheduled for removal
assert_eq!(buf.peek(), Ok(7));

// Check the queue size
assert_eq!(buf.size(), 1);

Implementations§

Source§

impl<T: Clone> Buffer<T>

Source

pub fn new(capacity: usize) -> Buffer<T>

Create a new buffer

§Returns

A new, empty Buffer<T>

§Examples
let buf: Buffer<isize> = Buffer::new(3);
assert_eq!(buf.size(), 0);
Examples found in repository?
examples/buf.rs (line 11)
5fn main() {
6    println!("\nBuffer - typical usage");
7    println!("--");
8
9    println!("\nCreate a new empty buffer:");
10    println!("let mut buf: Buffer<isize> = Buffer::new(3);");
11    let mut buf: Buffer<isize> = Buffer::new(3);
12
13    println!("\nAdd elements to it:");
14    println!("buf.add(1);");
15    println!("> {:?}", buf.add(1));
16    println!("buf.add(-2);");
17    println!("> {:?}", buf.add(-2));
18    println!("buf.add(3);");
19    println!("> {:?}", buf.add(3));
20
21    println!("\nAttempt to add elements when full:");
22    println!("buf.add(-4);  // Should raise an error");
23    println!("> {:?}", buf.add(-4));
24
25    println!("\nCheck the buffer's size:");
26    println!("buf.size();  // Should be 3");
27    println!("> {}", buf.size());
28
29    println!("\nRemove elements from it:");
30    println!("buf.remove();  // Should be Ok(1)");
31    println!("> {:?}", buf.remove());
32
33    println!("\nCheck the buffer's size:");
34    println!("buf.size();  // Should be 2");
35    println!("> {}", buf.size());
36
37    println!("\nPeek at the next element to be removed:");
38    println!("buf.peek();  // Should be Ok(-2)");
39    println!("> {:?}", buf.peek());
40
41    println!("\nCheck the queue's size:");
42    println!("buf.size();  // Should be 2");
43    println!("> {}", buf.size());
44
45    println!("\nRemove more elements from it:");
46    println!("buf.remove();  // Should be Ok(-2)");
47    println!("> {:?}", buf.remove());
48    println!("buf.remove();  // Should be Ok(3)");
49    println!("> {:?}", buf.remove());
50
51    println!("\nPeek at the next element to be removed:");
52    println!("buf.peek();  // Should raise an error");
53    println!("> {:?}", buf.peek());
54
55    println!("\nAttempt to remove elements from it:");
56    println!("buf.remove();  // Should raise an error");
57    println!("> {:?}", buf.remove());
58
59    println!("\n--\n")
60}
Source

pub fn capacity(&self) -> usize

Gets the capacity of the buffer

§Returns

The number of allowed elements in the buffer

§Examples
let mut buf: Buffer<isize> = Buffer::new(5);
assert_eq!(buf.capacity(), 5);

Trait Implementations§

Source§

impl<T: Debug + Clone> Debug for Buffer<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Clone> IsQueue<T> for Buffer<T>

Source§

fn add(&mut self, val: T) -> Result<Option<T>, &str>

Adds an element to a buffer

§Parameters
  • val: Value to add to the buffer
§Returns
  • Ok(None): Element addition was successful
  • Error
§Errors

Returns an error if an attempt is made to add an element to a full buffer

§Examples
use queues::*;

let mut buf: Buffer<isize> = Buffer::new(3);
assert_eq!(buf.add(42), Ok(None));
Source§

fn remove(&mut self) -> Result<T, &str>

Removes an element from the buffer and returns it.

§Returns
  • Ok(T): The oldest element in the buffer
  • Error
§Errors

Returns an error if an attempt is made to remove an element from an empty buffer

§Examples
let mut buf: Buffer<isize> = Buffer::new(3);
buf.add(42);
assert_eq!(buf.remove(), Ok(42));
assert_eq!(buf.size(), 0);
Source§

fn peek(&self) -> Result<T, &str>

Peek at the head of the buffer

§Returns
  • Ok(T): The next element scheduled for removal from the buffer
  • Error
§Errors

Returns an error if an attempt is made to peek into an empty buffer

§Examples
let mut buf: Buffer<isize> = Buffer::new(3);
buf.add(42);
assert_eq!(buf.peek(), Ok(42));
Source§

fn size(&self) -> usize

Gets the size of the buffer

§Returns

The number of elements in the buffer.

§Examples
let mut buf: Buffer<isize> = Buffer::new(3);
assert_eq!(buf.size(), 0);
buf.add(42);
assert_eq!(buf.size(), 1);

Auto Trait Implementations§

§

impl<T> Freeze for Buffer<T>

§

impl<T> RefUnwindSafe for Buffer<T>
where T: RefUnwindSafe,

§

impl<T> Send for Buffer<T>
where T: Send,

§

impl<T> Sync for Buffer<T>
where T: Sync,

§

impl<T> Unpin for Buffer<T>
where T: Unpin,

§

impl<T> UnwindSafe for Buffer<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.