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 theClone
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>
impl<T: Clone> Buffer<T>
Sourcepub fn new(capacity: usize) -> Buffer<T>
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}
Trait Implementations§
Source§impl<T: Clone> IsQueue<T> for Buffer<T>
impl<T: Clone> IsQueue<T> for Buffer<T>
Source§fn add(&mut self, val: T) -> Result<Option<T>, &str>
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 successfulError
§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>
fn remove(&mut self) -> Result<T, &str>
Removes an element from the buffer and returns it.
§Returns
Ok(T)
: The oldest element in the bufferError
§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);
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more