Struct string_ring::StringRing
source · pub struct StringRing { /* private fields */ }
Expand description
A circular string buffer with a set maximum size.
Strings can be pushed onto the end of the buffer.
If the maximum size would be exceeded, the oldest content will be removed to make space.
The specific strategy for removing old content is defined by Granularity
.
Implementations§
source§impl StringRing
impl StringRing
sourcepub fn new(max_size: usize, granularity: Granularity) -> Self
pub fn new(max_size: usize, granularity: Granularity) -> Self
Creates a new, empty StringRing
with the given settings.
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all content from the buffer and resets it to the initial state.
sourcepub fn as_slices(&self) -> (&[u8], &[u8])
pub fn as_slices(&self) -> (&[u8], &[u8])
Gets a reference to the raw stored data. Note that because this is a circular buffer of bytes, the content may be in two distinct pieces which may not independently be valid UTF-8 sequences. However, the two slices together (in the returned order) always represents a valid UTF-8 string.
sourcepub fn make_contiguous(&mut self) -> &mut str
pub fn make_contiguous(&mut self) -> &mut str
Makes the content of the circular buffer contiguous and returns it as a mutable slice.
sourcepub fn push(&mut self, s: &str)
pub fn push(&mut self, s: &str)
Appends content to the end of the buffer.
If this would cause the buffer to exceed its maximum size, old content is removed to make room (as defined by Granularity
).
It is guaranteed that pushing a
followed by b
is equivalent to pushing a + b
.
In Granularity::Line
mode, this may lead to unexpected results if a
does not end in a new line,
as bytes may need to be discarded from the beginning of b
to reach the end of a line being removed.
This information is stored internally as a simple state machine to ensure stream consistency.