[RingStack] is a tiny stack implementation which uses circular buffer.
Since [RingStack] is constructed upon a circular buffer, the oldest item automatically dropped as you [push][RingStack::push()] when the number of items has already reached its limit. (Thus [len][RingStack::len()] method saturate with that number of limit.)
And it supports [RingStack::iter()] method which returns Iterator<&T>
.
It provides items one by one with historical order, latest to oldest.
Though [RingStack] currently uses [Vec] as its internals, once it allocates at the timing of [new][RingStack::new()] then additional allocation never happends.
```rust use ringstack::RingStack;
let mut s = RingStack::
s.push(1); s.push(2); asserteq!(s.peek(), Some(&2)); asserteq!(s.pop(), Some(2)); asserteq!(s[0], 1); asserteq!(s.get(0), Some(&1)); assert_eq!(s.get(1), None);
s.push(3);
s.push(4);
let v: Vec
s.push(5);
let v: Vec
asserteq!(s.pop(), Some(5)); asserteq!(s.pop(), Some(4)); asserteq!(s.pop(), Some(3)); asserteq!(s.pop(), None); ```
len()
], [get()
] methods.std::ops::Index
].Change [iter()
] return type
Changed from &Option<T>
into &T
and it iterates only valid elements,
since it returns reference of T
not Option
.
Make RingStack [Debug] derived
Initial Version
The MIT License (MIT)
Copyright (c) 2022 msr1k