1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
//! # Queues
//!
//! `queues` provides a number of efficient FIFO Queue data structures for
//! usage in your libraries. These are all implemented on top of rust's `Vector`
//! type.
//!
//! A queue is a linear data structure that commonly defines three methods:
//!
//! 1. `add`: Also called `queue` or `push`, this adds elements to the queue.
//! 2. `remove`: Also called `deque` or `pop`, this removes the _oldest_
//!     element from the queue.
//! 3. `peek`: Show the next element in the queue scheduled for removal.
//!
//! There are a number of variants of queues. In this crate, the available
//! variants are:
//!
//! - `Queue<T>`: A simple FIFO queue with a growable size and no limit on its
//!     capacity.
//! - `Buffer<T>`: A FIFO queue with with a limited capacity. The buffer can
//!     have a growable size (up to the defined capacity), or it can be
//!     initialized at capacity, with empty slots being occupied by default
//!     values.
//! - `CircularBuffer<T>`: Similar to the buffer above, but allowing for
//!     overflow. Any additions to the circular buffer that would exceed its
//!     capacity causes its oldest element to be pushed out.
//!
//! # Quick start
//!
//! Quick guide to getting started with the project:
//!
//! ## Installation
//!
//! ### Library usage
//!
//! To use the library in your project, simply ensure that the `queues` crate
//! has been added to your dependencies in your `Cargo.toml` file.
//!
//! ```yaml
//! [dependencies]
//! queues = "1.0.0"
//! ```
//!
//! In your files, import the crate and use it's members:
//!
//! ```rust
//! # #[macro_use]
//! extern crate queues;
//! use queues::*;
//! # fn main() { }
//! ```
//!
//! ### Source code
//!
//! To get the project up and running:
//!
//! ```bash
//! > cd ${WORKING_DIR}
//! > git clone <this_repo>
//! > cargo build
//! ```
//!
//! ## Testing
//!
//! Run the test suite using `cargo`:
//!
//! ```bash
//! > cd ${PROJECT_FOLDER}
//! > cargo test
//! ```
//!
//! ## Examples
//!
//! The project has a number of examples you can run to see how the library members work.
//!
//! The example names are:
//!
//! - `queue` Queue example
//! - `buf` Buffer example
//! - `cbuf` Circular buffer example
//! - `cbuf_def` Circular buffer with default values example
//!
//! ```bash
//! > cd ${PROJECT_FOLDER}
//! > cargo run --example ${EXAMPLE_NAME}
//! ```
//!
//! # Usage
//!
//! Simple usage is described below:
//!
//! ```rust
//! #[macro_use]
//! extern crate queues;
//!
//! use queues::*;
//!
//! fn main() {
//!     // Create a simple Queue
//!     let mut q: Queue<isize> = queue![];
//!
//!     // Add some elements to it
//!     q.add(1);
//!     q.add(-2);
//!     q.add(3);
//!
//!     // Check the Queue's size
//!     q.size();  // 3
//!
//!     // Remove an element
//!     q.remove();  // Ok(1)
//!
//!     // Check the Queue's size
//!     q.size();  // 2
//!
//!     // Peek at the next element scheduled for removal
//!     q.peek();  // Ok(-2)
//!
//!     // Confirm that the Queue size hasn't changed
//!     q.size();  // 2
//!
//!     // Remove the remaining elements
//!     q.remove();  // Ok(-2)
//!     q.remove();  // Ok(3)
//!
//!     // Peek into an empty Queue
//!     q.peek();  // Raises an error
//!
//!     // Attempt to remove an element from an empty Queue
//!     q.remove();  // Raises an error
//! }
//! ```
//!
//! The examples contain more information on `Buffer` and `CircularBuffer`
//! usage

#![warn(missing_docs)]

/// Defines methods that would be expected on a queue data structure
pub trait IsQueue<T: Clone> {
    /// Adds a new value to a queue
    ///
    /// # Parameters
    /// - `val`: Value to add to the queue
    ///
    /// # Returns
    /// - `Ok(_)`: If the element add was successful.
    ///     - `Some(T)`: If adding an element resulted in the removal of an
    ///         existing one (in the case of a circular buffer, for instance)
    ///     - `None`: Adding an element did not return any value
    /// - `Error`: If the element add was unsuccessful
    ///
    /// # Errors
    /// Attempting to add an element to a full queue that does not allow for
    /// overflow will return an error.
    fn add(&mut self, val: T) -> Result<Option<T>, &str>;

    /// Removes an element from the queue and returns it
    ///
    /// For queues with default values, removing an element will add a new
    /// default value into the queue.
    ///
    /// # Returns
    /// - `Ok(T)`: The oldest element in the queue
    /// - `Error`
    ///
    /// # Errors
    /// Returns an error if an attempt is made to remove an element from
    /// an empty queue
    fn remove(&mut self) -> Result<T, &str>;

    /// Peek at the head of the queue
    ///
    /// # Returns
    /// - `Ok(T)`: The next element scheduled for removal from the queue
    /// - `Error`
    ///
    /// # Errors
    /// Returns an error if an attempt is made to peek into an empty queue
    fn peek(&self) -> Result<T, &str>;

    /// Gets the size of the queue
    ///
    /// # Returns
    /// The number of elements in the queue. Note, this _includes_ default
    /// values when specified, which means that the `size` of a queue with
    /// default values should always be equal to its `capacity`
    fn size(&self) -> usize;
}

/// A simple FIFO queue with a growable size and no limit on its capacity.
///
/// # Type parameters
/// - `T`: Any type that implements the `Clone` trait.
///
/// # Examples
///
/// This example uses the [`queue!`][1] macro to add elements to the queue. Note
/// that the first element in the list of elements passed to the macro is
/// considered the 'oldest'.
///
/// ```
/// # #[macro_use] extern crate queues;
/// # use queues::*;
/// # fn main() {
/// let mut q = queue![3isize, 4, 5];
///
/// // Add an element
/// assert_eq!(q.add(6), Ok(None));
///
/// // Remove some elements
/// assert_eq!(q.remove(), Ok(3));
/// assert_eq!(q.remove(), Ok(4));
///
/// // Peek at the next element scheduled for removal
/// assert_eq!(q.peek(), Ok(5));
///
/// // Check the queue size
/// assert_eq!(q.size(), 2);
/// # }
///
/// [1]: macro.queue.html
/// ```
#[derive(Debug)]
pub struct Queue<T: Clone> {
    queue: Vec<T>,
}

impl<T: Clone> Queue<T> {
    /// Create a new queue
    ///
    /// # Returns
    /// A new, empty `Queue<T>`
    ///
    /// # Examples
    ///
    /// ```
    /// # use queues::*;
    /// let q: Queue<isize> = Queue::new();
    /// assert_eq!(q.size(), 0);
    /// ```
    pub fn new() -> Queue<T> {
        Queue { queue: vec![] }
    }
}

impl<T: Clone> Default for Queue<T> {
    /// Default queue initializer
    ///
    /// # Returns
    /// A new, empty `Queue<T>`
    ///
    /// # Examples
    ///
    /// ```
    /// # use queues::*;
    /// let q: Queue<isize> = Queue::default();
    /// assert_eq!(q.size(), 0);
    /// ```
    fn default() -> Queue<T> {
        Queue { queue: vec![] }
    }
}

impl<T: Clone> IsQueue<T> for Queue<T> {
    /// Adds an element to a queue
    ///
    /// # Parameters
    /// - `val`: Value to add to the queue
    ///
    /// # Returns
    /// `Ok(None)` as the element addition should always be successful
    ///
    /// # Examples
    ///
    /// ```
    /// # use queues::*;
    /// let mut q: Queue<isize> = Queue::new();
    /// assert_eq!(q.add(42), Ok(None));
    /// assert_eq!(q.size(), 1);
    /// ```
    fn add(&mut self, val: T) -> Result<Option<T>, &str> {
        self.queue.push(val);
        Ok(None)
    }

    /// Removes an element from the queue and returns it
    ///
    /// # Returns
    /// - `Ok(T)`: The oldest element in the queue
    /// - `Error`
    ///
    /// # Errors
    /// Returns an error if an attempt is made to remove an element from
    /// an empty queue
    ///
    /// # Examples
    ///
    /// ```
    /// # use queues::*;
    /// let mut q: Queue<isize> = Queue::new();
    /// q.add(42);
    /// assert_eq!(q.remove(), Ok(42));
    /// assert_eq!(q.size(), 0);
    /// ```
    fn remove(&mut self) -> Result<T, &str> {
        if self.queue.len() > 0 {
            Ok(self.queue.remove(0usize))
        } else {
            Err("The queue is empty")
        }
    }

    /// Peek at the head of the queue
    ///
    /// # Returns
    /// - `Ok(T)`: The next element scheduled for removal from the queue
    /// - `Error`
    ///
    /// # Errors
    /// Returns an error if an attempt is made to peek into an empty queue
    ///
    /// # Examples
    ///
    /// ```
    /// # use queues::*;
    /// let mut q: Queue<isize> = Queue::new();
    /// q.add(42);
    /// assert_eq!(q.peek(), Ok(42));
    /// ```
    fn peek(&self) -> Result<T, &str> {
        match self.queue.first() {
            Some(val) => Ok(val.clone()),
            None => Err("The Queue is empty"),
        }
    }

    /// Gets the size of the queue
    ///
    /// # Returns
    /// The number of elements in the queue
    ///
    /// # Examples
    ///
    /// ```
    /// # use queues::*;
    /// let mut q: Queue<isize> = Queue::new();
    /// assert_eq!(q.size(), 0);
    /// let _ = q.add(42);
    /// assert_eq!(q.size(), 1);
    /// ```
    fn size(&self) -> usize {
        self.queue.len()
    }
}

/// Creates a new `Queue<T>`
///
/// Delegates to the default queue initializer. Note that the elements are
/// added to the queue from left to right, therefore the first element in the
/// list of parameters passed to the macro is considered the 'oldest' element
/// in the queue.
///
/// # Example
/// ```
/// # #[macro_use]
/// # extern crate queues;
/// # use queues::*;
///
/// # fn main() {
/// let q = queue![3isize, 4, 5];
/// assert_eq!(q.peek(), Ok(3));
///
/// let q_empty: Queue<isize> = queue![];
/// assert_eq!(q_empty.size(), 0);
/// # }
/// ```
#[macro_export]
macro_rules! queue {
    () => { Queue::new() };
    ($($x:expr),+) => {
        {
            let mut temp_q = Queue::default();
            $(
                let _ = temp_q.add($x);
            )*
            temp_q
        }
    };
}

/// A FIFO buffer with a growable size and a capacity limit
///
/// # Type parameters
/// - `T`: Any type that implements the `Clone` trait.
///
/// # Examples
///
/// ```
/// # use queues::*;
/// 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);
/// ```
#[derive(Debug)]
pub struct Buffer<T: Clone> {
    queue: Vec<T>,
    capacity: usize,
}

impl<T: Clone> Buffer<T> {
    /// Create a new buffer
    ///
    /// # Returns
    /// A new, empty `Buffer<T>`
    ///
    /// # Examples
    ///
    /// ```
    /// # use queues::*;
    /// let buf: Buffer<isize> = Buffer::new(3);
    /// assert_eq!(buf.size(), 0);
    /// ```
    pub fn new(capacity: usize) -> Buffer<T> {
        Buffer {
            queue: vec![],
            capacity,
        }
    }

    /// Gets the capacity of the buffer
    ///
    /// # Returns
    /// The number of allowed elements in the buffer
    ///
    /// # Examples
    ///
    /// ```
    /// # use queues::*;
    /// let mut buf: Buffer<isize> = Buffer::new(5);
    /// assert_eq!(buf.capacity(), 5);
    /// ```
    pub fn capacity(&self) -> usize {
        self.capacity
    }
}

impl<T: Clone> IsQueue<T> for Buffer<T> {
    /// 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));
    /// ```
    fn add(&mut self, val: T) -> Result<Option<T>, &str> {
        if self.queue.len() < self.capacity {
            self.queue.push(val);
            Ok(None)
        } else {
            Err("The buffer is full")
        }
    }

    /// 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
    ///
    /// ```
    /// # use queues::*;
    /// let mut buf: Buffer<isize> = Buffer::new(3);
    /// buf.add(42);
    /// assert_eq!(buf.remove(), Ok(42));
    /// assert_eq!(buf.size(), 0);
    /// ```
    fn remove(&mut self) -> Result<T, &str> {
        if self.queue.len() > 0 {
            Ok(self.queue.remove(0usize))
        } else {
            Err("The buffer is empty")
        }
    }

    /// 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
    ///
    /// ```
    /// # use queues::*;
    /// let mut buf: Buffer<isize> = Buffer::new(3);
    /// buf.add(42);
    /// assert_eq!(buf.peek(), Ok(42));
    /// ```
    fn peek(&self) -> Result<T, &str> {
        match self.queue.first() {
            Some(val) => Ok(val.clone()),
            None => Err("The buffer is empty"),
        }
    }

    /// Gets the size of the buffer
    ///
    /// # Returns
    /// The number of elements in the buffer.
    ///
    /// # Examples
    ///
    /// ```
    /// # use queues::*;
    /// let mut buf: Buffer<isize> = Buffer::new(3);
    /// assert_eq!(buf.size(), 0);
    /// buf.add(42);
    /// assert_eq!(buf.size(), 1);
    /// ```
    fn size(&self) -> usize {
        self.queue.len()
    }
}

/// Represents a FIFO `CircularBuffer<T>` data structure.
///
/// This structure is a limited capacity queue, with optional provisions
/// for default values. Under normal circumstances, the `size` of the
/// queue grows until it reaches its `capacity`, at which point any
/// further additions push out its oldest member.
///
/// If default values are specified, then the `size` of the queue
/// is always equal to its `capacity`, with empty slots occupied by the
/// specified default value.
///
/// # Type parameters
/// - `T`: Any type that implements the `Clone` trait.
///
/// # Examples
///
/// ```
/// # use queues::*;
/// # fn main() {
/// let mut cbuf = CircularBuffer::<isize>::new(3);
/// let mut cbuf_def = CircularBuffer::with_default(3, 0isize);
///
/// // Check sizes
/// assert_eq!(cbuf.size(), 0);
/// assert_eq!(cbuf_def.size(), 3);
///
/// // Add elements
/// cbuf.add(6);
/// cbuf_def.add(7);
///
/// // Peek at the next element scheduled for removal
/// assert_eq!(cbuf.peek().unwrap(), 6);
/// assert_eq!(cbuf_def.peek().unwrap(), 0);
/// # }
/// ```
#[derive(Debug)]
pub struct CircularBuffer<T: Clone> {
    queue: Vec<T>,
    capacity: usize,
    default_value: Option<T>,
}

impl<T: Clone> CircularBuffer<T> {
    /// Default `CircularBuffer<T>` initializer
    ///
    /// # Returns
    /// A new, empty `CircularBuffer<T>`
    ///
    /// # Examples
    ///
    /// ```
    /// # use queues::*;
    /// let cbuf: CircularBuffer<isize> = CircularBuffer::new(3);
    /// assert_eq!(cbuf.size(), 0);
    /// assert_eq!(cbuf.capacity(), 3);
    /// ```
    pub fn new(capacity: usize) -> CircularBuffer<T> {
        CircularBuffer {
            queue: vec![],
            capacity,
            default_value: None,
        }
    }

    /// Create a `CircularBuffer<T>` with default values
    ///
    /// # Returns
    /// A new `CircularBuffer<T>` filled with default values
    ///
    /// # Examples
    ///
    /// ```
    /// # use queues::*;
    /// let cbuf_def = CircularBuffer::with_default(3, -1isize);
    /// assert_eq!(cbuf_def.size(), 3);
    /// assert_eq!(cbuf_def.capacity(), 3);
    /// assert_eq!(cbuf_def.peek(), Ok(-1));
    /// ```
    pub fn with_default(capacity: usize, default_value: T) -> CircularBuffer<T> {
        let queue = vec![default_value.clone(); capacity];

        CircularBuffer {
            queue,
            capacity,
            default_value: Some(default_value),
        }
    }

    /// Gets the capacity of the `CircularBuffer<T>`
    ///
    /// # Returns
    /// The number of allowed elements in the buffer
    ///
    /// # Examples
    ///
    /// ```
    /// # use queues::CircularBuffer;
    /// let mut cbuf: CircularBuffer<isize> = CircularBuffer::new(3);
    /// assert_eq!(cbuf.capacity(), 3);
    /// ```
    pub fn capacity(&self) -> usize {
        self.capacity
    }
}

impl<T: Clone> IsQueue<T> for CircularBuffer<T> {
    /// Adds an element to a circular buffer
    ///
    /// # Parameters
    /// - `val`: Value to add to the buffer
    ///
    /// # Returns
    /// - `Ok(Some(T))`: The oldest value in the buffer, in case the addition
    ///     causes an overflow.
    /// - `Ok(None)`: Nothing, if the buffer has room for the added element
    ///
    /// # Examples
    ///
    /// ```
    /// # use queues::*;
    /// let mut cbuf: CircularBuffer<isize> = CircularBuffer::new(3);
    /// let mut cbuf_def = CircularBuffer::with_default(3, 5isize);
    /// assert_eq!(cbuf.add(42), Ok(None));
    /// assert_eq!(cbuf_def.add(42), Ok(Some(5)));
    /// ```
    fn add(&mut self, val: T) -> Result<Option<T>, &str> {
        if self.queue.len() < self.capacity {
            self.queue.push(val);
            Ok(None)
        } else {
            self.queue.push(val);
            Ok(Some(self.queue.remove(0usize)))
        }
    }

    /// Removes an element from the circular buffer and returns it.
    ///
    /// For circular buffers with default values, removing an element will add
    /// a new default value into the buffer.
    ///
    /// # 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
    ///
    /// ```
    /// # use queues::*;
    /// let mut cbuf: CircularBuffer<isize> = CircularBuffer::new(3);
    /// cbuf.add(42);
    /// assert_eq!(cbuf.remove(), Ok(42));
    /// assert_eq!(cbuf.size(), 0);
    ///
    /// let mut cbuf_def = CircularBuffer::with_default(3, 4isize);
    /// cbuf_def.add(42);
    /// assert_eq!(cbuf_def.remove(), Ok(4));
    /// ```
    fn remove(&mut self) -> Result<T, &str> {
        if self.queue.len() > 0 {
            if let Some(val) = self.default_value.clone() {
                self.queue.push(val);
            };
            Ok(self.queue.remove(0usize))
        } else {
            Err("The Buffer is empty")
        }
    }

    /// Peek at the head of the circular 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
    ///
    /// ```
    /// # use queues::*;
    /// let mut cbuf: CircularBuffer<isize> = CircularBuffer::new(3);
    /// cbuf.add(42);
    /// assert_eq!(cbuf.peek(), Ok(42));
    /// ```
    fn peek(&self) -> Result<T, &str> {
        match self.queue.first() {
            Some(val) => Ok(val.clone()),
            None => Err("The Queue is empty"),
        }
    }

    /// Gets the size of the circular buffer
    ///
    /// # Returns
    /// The number of elements in the buffer. Note, this _includes_ default
    /// values, which means that the `size` of a buffer with default values
    /// should always be equal to its `capacity`
    ///
    /// # Examples
    ///
    /// ```
    /// # use queues::*;
    /// let mut cbuf: CircularBuffer<isize> = CircularBuffer::new(3);
    /// assert_eq!(cbuf.size(), 0);
    /// cbuf.add(42);
    /// assert_eq!(cbuf.size(), 1);
    /// ```
    fn size(&self) -> usize {
        self.queue.len()
    }
}