Rust std study series: VecDeque

Continuing from Rust standard library study series, it’s time for VecDequeVec, there isn’t much to say.
Adouble-endedqueueimplemented with agrowable ring buffer.
The “default” usage of this type as a queue is to usepush_backtoaddto the queue, andpop_fronttoremovefrom the queue.extendandappendpush onto the back in this manner, and iterating overVecDequegoes front to back.Rust std docSimilar to
Vec,VecDequehas amortized \(O(1)\) insert to both ends of the container, but unlikeVec, it has amortized \(O(1)\) removal from both ends. (Recalling fromVecstudy, removal is strictly \(O(1)\) with no shrink factor involved)Similar toVec, indexing is \(O(1).\)
Similar to VecVecDeque<T>
struct VecDeque<T> {
// tail and head are pointers into the buffer. Tail always points
// to the first element that could be read, Head always points
// to where data should be written.
// If tail == head the buffer is empty. The length of the ringbuffer
// is defined as the distance between the two.
tail: usize,
head: usize,
buf: RawVec<T>,
}
// Default Global allocator
struct RawVec<T, A: Alloc = Global> {
ptr: Unique<T>,
cap: usize,
a: A,
}
#[repr(transparent)]
struct Unique<T: ?Sized> {
pointer: *const T,
_marker: PhantomData<T>,
}The same Vec methods, such as with_capacity (note that ring buffer alwaysleaves one space empty), truncate, shrink_to etc. exist and follow the same observations as in Vec study. The notable methods arepush_back and pop_back which involve moving the head pointer and push_front and pop_front which involve moving the tail pointer.retain which acts as the filter method.resize_with with a generator: impl FnMut() -> Tas_slices (and the mut one) which contains, in order the content of the VecDeque.
That’s it for now!