SlidingWindow

Struct SlidingWindow 

Source
pub struct SlidingWindow<T> { /* private fields */ }
Expand description

A simple sliding window implementation in rust. It uses a vector as ring buffer and a sum counter to prevent the requirement of iterating over the entire buffer for each time the total count is required.

This sliding window implementation is not aware of the window movement it keeps track of the windows and can be triggered to tick over.

Implementations§

Source§

impl<T> SlidingWindow<T>
where T: Clone + Copy + Add<Output = T> + Sub<Output = T> + From<u32> + PartialOrd,

Source

pub fn new(size: usize, max: T) -> Self

Creates a new sliding window.

  • size - is the number of slots in the windows
  • max - is the maximum the current window allows
Source

pub fn max(&self) -> T

Source

pub fn set_max(&mut self, max: T) -> &Self

Source

pub fn count(&self) -> T

Returns the current count of the window.

Source

pub fn inc(&mut self) -> Result<T, T>

Tries to increment the window by one, this equals add(1). See add for a description of return values.

§Errors

if we would overshoot the maximum, the error is the number over max.

Source

pub fn add(&mut self, n: T) -> Result<T, T>

Tries to add a given number to the windows count. If we remain below max the result is Ok(<new count>). If we fail and would go over max the result is Err(<count over max>).

§Errors

if we would overshoot the maximum, the error is the number over max.

Source

pub fn empty(&mut self) -> &mut Self

Source

pub fn tick(&mut self) -> &mut Self

Trait Implementations§

Source§

impl<T: Debug> Debug for SlidingWindow<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for SlidingWindow<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for SlidingWindow<T>
where T: RefUnwindSafe,

§

impl<T> Send for SlidingWindow<T>
where T: Send,

§

impl<T> Sync for SlidingWindow<T>
where T: Sync,

§

impl<T> Unpin for SlidingWindow<T>
where T: Unpin,

§

impl<T> UnwindSafe for SlidingWindow<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.